blogger templatesblogger widgets

How to Create Your Own Twitter Auto Retweet and Favorite Bot

You can easily create your own Twitter bot that will automatically retweet whatever you want in twitter, including topics, selected accounts, lists, favorites and more!

How to Create Your Own Twitter Auto Retweet and Favorite Bot Mumbai INDIA
How to Create Your Own Twitter Auto Retweet and Favorite Bot


This tutorial explains how you can easily make a Twitter bot that will automatically favorite and/or retweet tweets that contain particular keywords or #hashtags. You need absolutely no coding knowledge and your Twitter bot will be up and running in few minutes.

Before we get started, you may be wondering why would anyone write a twitter bot that mindlessly favorites or retweets tweets? Yes, bots are often used for spammy behavior but, if used right, they can also help grow your Twitter network. For instance, when people share a link from your website on Twitter, you can favorite that tweet and it will give an hint to the original poster that you are author of that page. A brand may like to retweet tweets that contain positive mentions of their product. The list goes on.

  1. Go to apps.twitter.com and create a new application. Fill in the mandatory fields (name, description, URL) and click the Create button. Next go to Keys and Access Tokens and click the Create my Access Token button. Twitter will generate the Consumer Keys & Access tokens that we will need in the next step.
  2. Click here to copy the Twitter bot script to your Google Drive. Replace the search phrase and Twitter keys that were generated in the previous step.
  3. Go to the Run menu and choose StartBot to initialize your Twitter bot.
That’s it. The bot will run in the background, every 10 minutes, and favorite / retweet matching tweets. It will fave/RT a maximum of 1 tweet per minute. If you wish to stop the bot later, go to Run again and choose StopBot.

Read full step by step guide here: How to Create a Retweet and Favorite Bot for Twitter

How to create a Twitter bot that auto-favorites and retweets tweets containing particular keywords or hashtags. The twitter bot is written in Google Scripts and can be installed in a minute.

Using Twiiter RSS, Google Scripts IFTTT to Create Your Own Twitter Auto Retweet Bot 

What we will be using:

  1. A Twitter account for your bot (or even your own twitter account if you wish to retweet yourself)
  2. Google Scripts (You don’t have to code anything, though)
  3. IFTTT (For retweeting)
How will we be doing it?

Twitter has disabled RSS feeds for profiles, so the major step to do this is obtaining tweets in RSS form. A script from Amit Agarwal, a well known blogger, solves this issue. With a little modification to the script, we create a non-repeating version (i.e, same tweet will not be retweeted multiple times). With the help of IFTTT, we tweet these feeds. Hence, the auto-retweet bot. twitter-activity-retweet



Let’s start, then..

First, you need to create a twitter widget. Log in to your twitter account and click on Settings and select Widgets. Click on Create. Now, you should create a widget with what content you would like to retweet. If you would like to retweet whatever a particular user has tweeted, then click on “User Timeline” and enter that user’s name.  Or if you would like to retweet tweets of a particular group of people, create a list and make a widget of it. If you would like to know about a particular topic on twitter, click on the “Search” tab and enter your search query. Click on “Create Widget” after you finish. In the URL bar, copy the ID of your widget, which will look something like “363815745370191000”.

Converting your Widget to RSS Feed.

This script from Amit Agarwal converts your twitter feed to RSS. However, with minor modifications, the below script can be used for this purpose. To execute this script, go to Google Scripts and open a blank project. Copy and paste the following code. Then, click on Run> getTweets. When asked to Authorize, click on the authorize button. (This is a one time procedure only).

function Twitter_RSS() {
  return; 
}
 
function doGet(e) {
   
  var widgetID = e.queryString? e.queryString : "ERROR_NO_ID_FOUND";
  var cache = CacheService.getPublicCache();
  var id = "Twitter" + widgetID;
  var rss = cache.get(id);
   
  if ( ! rss ) {
    rss = getTweets(widgetID);
    cache.put(id, rss, 120); // Expire in 2 minutes
  }
   
  return ContentService.createTextOutput(rss)
  .setMimeType(ContentService.MimeType.XML);
}
 
 
function getTweets(id) {
     
  try {
             
    var widget, json, tweets, regex, tweet, list, time, url, when, rss, heading, title, link, alltweets, permalink, permatitle; 
     
    title = "Twitter RSS Feed : " + id;
    link  = "http://www.techcovered.org/#" + id;              
    url   = "http://cdn.syndication.twimg.com/widgets/timelines/" + id;
     
    widget  = UrlFetchApp.fetch(url);
    json    = Utilities.jsonParse(widget);   
     
    if ( ! json.body ) {
      return;
    }
     
    list = json.body.replace(/(\r\n|\n|\r)/gm," ")
                    .replace(/\s+/g, " ")
                    .replace(/<div class=\"(h-card|footer|detail-expander|retweet-credit)[^>]*>(.*?)<\/div>/gi, "")
                    .replace(/<time[^>]*>(.*?)<\/time>/gi, "");
     
    regex = new RegExp(/<h1[^>]*>(.*?)<\/h1>/ig);
     
    if ((heading = regex.exec(list)) !== null) {
       
      regex = RegExp(/href="(.*?)"/ig);
      if ((permalink = regex.exec(heading[1])) !== null) {
        link = permalink[1];
      }
       
      regex = RegExp(/title="(.*?)"/ig);
      if ((permatitle = regex.exec(heading[1])) !== null) {
        title = permatitle[1];
      }
    }
     
    var self = ScriptApp.getService().getUrl() + "?" + id;
     
    rss  = '<?xml version="1.0"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
    rss += ' <channel><title>' + title + '</title>';
    rss += ' <link>' + link + '</link>';
    rss += ' <atom:link href="' + self + '" rel="self" type="application/rss+xml" />';
    rss += ' <description>' + title + ' :: RSS Generator available at http://www.labnol.org/?p=28149</description>';
     
    regex = RegExp(/<ol[^>]*>(.*?)<\/ol>/ig);
     
    if ((alltweets = regex.exec(list)) !== null) {
       
      alltweets = alltweets[1].replace(/\s+/g, " ");
       
      var re = /<a class=".*?permalink.*?" href="(.*?)" data-datetime="(.*?)"[^>]*>(.*?)<\/a>(.*?)<p[^>]*>(.*?)<\/p>/gm;
       
      while (tweet = re.exec(alltweets)) {
         
        url   = tweet[1];
        when  = Utilities.formatDate(parseDate(tweet[2]), "UTC", "EEE, d MMM yyyy HH:mm:ss");
 
        tweet = tweet[5].replace(/<\s*(div|span|b|p)[^>]*>/gi, "")
                        .replace(/<\s*\/\s*(div|span|b|p)[^>]*>/gi, "")
                        .replace(/class=".*?"|rel=".*?"|title=".*?"|target=".*?"|data-expanded-url=".*?"|data-query-source=".*?"|dir=".*?"|data-scribe=".*?"/gi, "")
                        .replace(/\s+/g, " ");
        if(tweet.substring(0,2)!="RT") {
        rss += "<item>";
        rss += " <title>"+ tweet.replace(/<a[^>]*>(.*?)<\/a>/gi, "") + "</title>";
        rss += " <pubDate>" + when + " +0000</pubDate>";
        rss += " <guid>" + url + "</guid>";
        rss += " <link>" + url + "</link>";
        rss += " <description><![CDATA[ @"+url.split("/")[3] + ": " + tweet + "]]></description>";
        rss += "</item>";            
        } 
        
      }
             
      rss += "</channel></rss>";
      return rss;
    }
     
  } catch (e) {
    Logger.log(e.toString());
  }
}
 
function parseDate(d) {
 
  var date = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;
  var m = date.exec(d);
  var year   = +m[1];
  var month  = +m[2];
  var day    = +m[3];
  var hour   = +m[4];
  var minute = +m[5];
  var second = +m[6];
   
  return new Date(year, month - 1, day, hour, minute, second);
}

After that, go to Publish> Deploy as a web app. Enter a version number and in the “Who has access to the app” select “Anyone, Even Anonymous”. Click Create. Copy the link in the box. To test your feed, paste this URL in the address bar and in the end, add “?” followed by your twitter widget ID. It will look something like this: 

https://script.google.com/macros/s/AKfycbufejQanpLBnFIelEaq5e774Ya7GIFFd33OE-srjTF8n7Pi3sc/exec?363815745370191000 

In the URL, you will be able to see the twitter feed you asked for. Do note that it is displayed as just code in Chrome, but is displayed as RSS in Firefox and Internet Explorer.

The Final Step

After you have the RSS link, its now time to go IFTTT, the awesome website that you can use to make trigger-action based events and more. Create an account and select the “Create” link on the top. Select “this” and click on the RSS feed icon. In the next step, click on “New Feed Item” and type in the URL you copied. Then, select “that” Next, select Twitter in the next list and click on “Activate” button to link your twitter profile in which you would like to post your retweets. In the next step, click on “Post a tweet”. In the “What’s happening box”?”, type the following: _RT _ Click on “Create Action”.