Twitter Bot for Auto-Favoriting and Retweeting Tweets

The Twitter bot, written with Google Apps Script, will auto-favorite and retweet tweets every few minutes. Do include the Twitter API library in your Google Scripts project as discussed in the previous tutorial on creating Twitter bots.

// Written by Amit Agarwal @labnol on 31/07/2015

// Fill the Twitter Keys and then choose Run -> Start Bot

TWITTER_CONSUMER_KEY = '123';
TWITTER_CONSUMER_SECRET = '123';
TWITTER_ACCESS_TOKEN = '123';
TWITTER_ACCESS_SECRET = '123';
TWITTER_SEARCH_PHRASE = 'filter:links labnol.org';

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 Twitter.OAuth(props);

  // Test Twitter authorization

  if (!twit.favorite('628053456071192576')) {
    throw new Error('Please check your Twitter access tokens');
    return;
  }

  ScriptApp.newTrigger('labnol_twitterBot').timeBased().everyMinutes(10).create();
}

function labnol_twitterBot() {
  try {
    var props = PropertiesService.getScriptProperties(),
      twit = new Twitter.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: 5, // Process 5 tweets in a batch
          since_id: props.getProperty('SINCE_TWITTER_ID'),
        }
      );

      if (tweets) {
        props.setProperty('SINCE_TWITTER_ID', tweets[0]);

        for (var i = tweets.length - 1; i >= 0; i--) {
          twit.retweet(tweets[i]);
          twit.favorite(tweets[i]);

          /* Wait between 10 seconds and 1 minute */
          Utilities.sleep(Math.floor(Math.random() * 50000) + 10000);
        }
      }
    }
  } catch (f) {
    Logger.log('Error: ' + f.toString());
  }
}

// Email: amit@labnol.org
// Premium Support: http://ctrlq.org
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.