Automate your Twitter Engagement and explode your followers

Posted by geshu on Wed, 05/25/2016 - 11:35

In life there are many truths, one of them is „there are only 24 hours in a day“.  So how do you run your daily business and still have time to take advantage of all the new methods of marketing online?  Today we will continue our focus on the Twitter platform, walk through an automated way to engage with other Twitter users and in turn gain more followers for our Twitter account (And if all goes well new customers, readers for our blog etc).  As with all our how to series we will start with understanding the reason and then work through the solution.

Why favorite or retweet?  Good question. 

Firstly retweeting helps you generate content for your own Twitter account.  If you retweet 5 tweets from other people per day and 1 -2 tweets of your own you are generating content for your followers to read.  Dont get me wrong retweets are not as good as your own original content being tweeted but it does keep the flow of content going.

The second thing retweeting accomplishes is having the original person who tweeted the message receive a notification that you retweeted their message.  This method puts your name in front of the other twitter user and can in turn result in a follow from them (the favorite method works the same way).  So through retweeting and also favoriting someones tweet we have the opporunity to grow our follower base.  Pretty sneaky sis.  For reference sake here is a screen of what a twitter user receives when you retweet or favorite their message

 

autofavorite/Screen%20Shot%202016-05-12%20at%209.29.05%20AM.pngautofavorite/Screen%20Shot%202016-05-12%20at%209.29.18%20AM.png

 

So the recipient of either of these messages could click to view what you did and in turn click to your profile and then potentially follow you.

Ok so now we know why we want to do this but how do we do it? 

The first method is 100% manual and in all honesty really time consuming.  You have to go to twitter do some searches and then favorite or retweat each messsage manually.  As we learned from our previous articles this can take some serious time every single day and that time is just for Twitter.  What about The Face-uh-book?  Or the Insta-gwam? 

autofavorite/flava-flav.jpg

Tick tock

tick tock. 

 

 

 

 

The second method and the one we will work through here, together, involves automating the entire process so we can set it up and let it run all on its own for us.

What will the script do?

  1. Search twitter based on our keywords
  2. Randomly favorite/retweet messages

How will we do this?

  1. Create a Twitter App https://dev.twitter.com
  2. Use a google script to run our code (dont worry I will give you this code later on.)

Why google scripts?  Some clients have very small hosting packages with godaddy, 1und1 or other tiny $1 per  month hosting companies that do not allow alot of flexibility for running advanced scripts.  Additionally the limited bandwidth and processor power means oftentimes scripts do not run well next to a website.  So we offload alot of functionality to systems like googles script app service.  It is free and works great across the monstrous Google server network.

So lets dive in and see how this is done

autofavorite/google-script.pngFirst we  go to https://drive.google.com/drive/my-drive

The on the left side navigation we click on new -> more -> Connect more apps and install the google scripts app (if you dont already have it).

Next click new then more then google app script

      

 

autofavorite/new-script.pngThis brings you to google scripts https://script.google.com/macros with a semi blank script page (You are given the default function myfunction.

 

 Dont worry we will just be using copy/paste with a few changes to make this work so no development experience is required.

We will start by adding the Twitter API to the google script (otherwise we cannot make a connection to Twitters API in order to perform our task.  This step is used in all our Twitter google script tutorials so remember it Smarty ).

autofavorite/twitter-library.png

Click Resources then libraries (If you have not already you will need to give this script a title.   Type it in and click save).  If you saved the project then a dialog box opens called „included libraries“. 

 

autofavorite/twitter-library-added.pngIn the find a library form field type paste in the Twitter Lib key as follows MKvHYYdYA4G5JJHj7hxIcoh8V4oX7X1M_ then click select.  In the drop down select the latest version (at the time of publishing this tutorial it was version 21) Once you see it added to the dialog click save.

Next we are going to copy the following code into the script (first delete the default function myfunction {}.

 

 

 

 

// Fill the Twitter Keys and then choose Run -> Start Bot
TWITTER_CONSUMER_KEY    = "Add your Twitter App Consumer Key";
TWITTER_CONSUMER_SECRET = " Add your Twitter App Consumer Secret";
TWITTER_ACCESS_TOKEN    = " Add your Twitter App Access Token ";
TWITTER_ACCESS_SECRET   = "Access Secret";
TWITTER_SEARCH_PHRASE   = "#Drupal -RT";

function Start_Bot() {
  var props = PropertiesService.getScriptProperties();
  props.setProperties({
    TWITTER_CONSUMER_KEY: TWITTER_CONSUMER_KEY,
    TWITTER_CONSUMER_SECRET: TWITTER_CONSUMER_SECRET,
    TWITTER_ACCESS_TOKEN: TWITTER_ACCESS_TOKEN,
    TWITTER_ACCESS_SECRET: TWITTER_ACCESS_SECRET,
    SINCE_TWITTER_ID: 0
  });
  var twit = new Twitterlib.OAuth(props);
  // Test Twitter authorization
  ScriptApp.newTrigger("Autofav_twitterBot")
 .timeBased()
 .everyMinutes(30)
 .create();
}

function Autofav_twitterBot() {
  try {
    var props = PropertiesService.getScriptProperties(),
        twit = new Twitterlib.OAuth(props);

    if (twit.hasAccess()) {
      var tweets = twit.fetchTweets(
        TWITTER_SEARCH_PHRASE, function(tweet) {
          // Skip tweets that contain sensitive content
          if (!tweet.possibly_sensitive) {
            return tweet.id_str;
          }
        }, {
          multi: true,
          lang: "en", // Process only English tweets
          count: 1,   // Process # tweets in a batch  I set it to 1
          since_id: props.getProperty("SINCE_TWITTER_ID")
        });

      if (tweets) {
        props.setProperty("SINCE_TWITTER_ID", tweets);
        for (var i = tweets.length - 1; i >= 0; i--) {
          var retweetnum = Math.floor(Math.random() * 6) + 1;
          if (retweetnum%2 == 0){
            twit.retweet(tweets);
          } else {
            //do nothing
          }
          twit.favorite(tweets);
        }
      }
    }

  } catch (f) {
    Logger.log("Error: " + f.toString());
  }
}

 

Now lets go through the important pieces here step by step.

// Fill the Twitter Keys and then choose Run -> Start Bot
TWITTER_CONSUMER_KEY    = "Add your Twitter App Consumer Key";
TWITTER_CONSUMER_SECRET = " Add your Twitter App Consumer Secret";
TWITTER_ACCESS_TOKEN    = " Add your Twitter App Access Token ";
TWITTER_ACCESS_SECRET   = "Access Secret";

 

These pieces of info you will get from the Dev.twitter.com app settings section.  You will be shown by default your Consumer Key/Secret. But in order for this script to work you will need to get the access token.  The access token allows this script to run the functions against your twitter account without having to log into your twitter account.

TWITTER_SEARCH_PHRASE   = "#hashtag -RT";

Next we need to add our search terminology.  If you want to know more about Twitters Search you can find it here -> (https://dev.twitter.com/rest/public/search).  We use hashtags and do not retweet things already retweeted.  So in the quotes above you see a hashtag with –RT.  This tells the Twitter search to find all tweets with the hashtag #hashtag except retweeted tweets –RT.

Now to the meat an potatos of the script.  You really dont need to know this for running hte script but it never hurts to learn something new heh heh.

  1. Next we create a google app script trigger to run every 30 minutes that calls our main function Autofav_twitterbot
  2.  

  .timeBased()
  .everyMinutes(30)
  .create();
  1. Our Autofav_twitterbot function does all the heavy lifting
  1. First we call the Twitter Library we implemented in the beginning and authenticate with var twit = new Twitterlib.OAuth(props);
  2. If we have access (authenticate correctly) then we search for tweets meeting our original search string      var tweets = twit.fetchTweets(
        TWITTER_SEARCH_PHRASE, function(tweet) {
          // Skip tweets that contain sensitive content
          if (!tweet.possibly_sensitive) {
            return tweet.id_str;
          }
        }
  1. Lastly we go through the tweets and retweet/autofavorite a tweet.  We add a little simple logic here to not always do the same thing.  So we make a random number and then determine if it is even or odd.  If it is even then we retweet it.  If it is odd then we do nothing.  We end our call by favoriting the tweet.

 

for (var i = tweets.length - 1; i >= 0; i--) {
          var retweetnum = Math.floor(Math.random() * 6) + 1;
          if (retweetnum%2 == 0){
            twit.retweet(tweets);
          } else {
            //do nothing
          }
          twit.favorite(tweets);
        }

Lastly we need to start the script (only once since it creates a trigger to run every 30 minutes J).  Click run (in the navigation bar) then click Start_Bot and poof thats all folks.  In just a few lines of code using Google App Scripts we have automated the process of favoriting and retweeting tweets from other people and in so doing added a way to help increase our follower base.

At geshu.com we strive to inform you as well as build the greatest products for your success.  We have built the amazing twitter Manager product that is currently helping thousands of clients grow their twitter followers and we are working on more products to help you play with the marketing big dogs.  To view our current products for Wordpress, Drupal and our standalone software visit our store here  I recommend you subscribe to our newsletter to learn about new products, and new great informative articles like this one  You can subscribe here.

Share it now!