Twitter Bot for Auto-Favoriting and Retweeting Tweets

T
Published in: Google Apps Script - Twitter

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
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Twitter

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube