The word “bot” brings up a lot of different connotations, depending on what experience you have with the term and with life. For people immerse in science fiction it brings to mind complex robots and futurism. For marketers it brings to mind software that spams their sites. For others it just means a piece of software used to automate something tedious.
In reality, bots are very simple; they’re just software, and generally not even particularly complex software. Sure, something like the Google web crawling bots are complex, but they have a bunch of different functions built in. Something simpler, like a Twitter bot, really doesn’t have a lot to it. Let’s take a look at what it takes to make one, eh?
Twitter Bot Functionality
A Twitter bot doesn’t need to do a lot. A simple retweet bot just needs a feature list that looks something like this:
- The ability to authenticate itself as a user, or to hook into an account via the Twitter app integration.
- The ability to read tweets on a broad scale.
- The ability to detect the presence of certain hashtags or keywords in the tweets it reads.
- The ability to retweet tweets that contain the hashtags or keywords specified.
- The ability to detect whether it has retweeted a given post – essentially verification to make sure it doesn’t get stuck in a loop retweeting the messages it tweets.
Five items, most of which are easy to do, that’s not bad. Now, the easiest way to do this is with Google Scripts, and you don’t even need to know how to code to do it. If you want to get into more advanced functionality, though, you may want to dig into code. Or not! It’s entirely up to you.
Defining Search Strings
The first step is to figure out what you want the target of the bot to be. You can change this later to modify the bot, but it helps to have an idea of how complex a task it will be right off the bat. You will want to format this a you would with Twitter’s search bar, because that’s what the bot will be using. That means using search operators that work on Twitter. Some examples include:
- “www.URL.com min_retweets:5 OR min_faves:5 -RT” This string will search for tweets that include your URL of choice, and only tweets that have 5 or more favorites or retweets. This removes retweets themselves from the search results and only gives you tweets that have at least some engagement already. The –RT bit also serves to remove manual retweets.
- “#Keyword –RT –filter:links” This string will search for tweets that include your hashtag of choice, with the filter to remove tweets that include links, because it would be redundant with the previous operator. The –RT, again, removes manual retweets.
- “#Keyword near:”Seattle,WA” within:20mi” This string will search for tweets that include your hashtag of choice, but only those tweets that were made by people within 20 miles of the geographic location listed, in this case Seattle. It doesn’t have the –RT filter, so it can identify tweets originally posted by out-of-area folks but retweeted within the target area.
You can mix and match these to your heart’s content. There are other filters as well. What you should do is go to Twitter’s search and just play around with variables and filters until you come up with a search query that gives you what you want to see and nothing more. Copy those strings so you can use them later.
Coding a Bot
The first thing you should do, before starting to make a bot, is register a new Twitter account. It doesn’t matter what profile information it has, you’re just using it for testing. This is because Twitter has some very strict rules about how bots can act on their site. If you’re trying to test a bot with your primary account and the bot crosses a line, suddenly your primary account is banned and there’s nothing you can do. Avoid the problem by creating a dummy account that doesn’t matter if it’s banned.
What are Twitter’s rules about automation and botting? You can read the details here, but I’ll summarize the salient points.
- Automation that does nothing but post about trending topics is seen as detrimental to the user experience and thus is prohibited.
- Automation that retweets is often considered spam and is prohibited in bulk, but limited retweeting is permitted for apps Twitter approves.
- Automation that sends direct messages is prohibited unless the recipient agrees to receive the messages.
- Automation that likes tweets in bulk is prohibited.
Unfortunately, an app that does nothing but retweet content in a bulk manner is against the rules. Our app plans to be more targeted and less haphazard with retweeting, but it’s still potentially against the rules. You’re skirting a line by making such a bot, so exercise caution and don’t use it with an account you really care about.
You will also need to have a Google Drive set up, to manage your code and to use Google Scripts to set up this bot. You can use other development environments if you’re familiar with them, of course, but at that point you probably don’t need to be reading this guide.
Now, what you’re going to have to do is go to the Twitter app developer section at apps.twitter.com and make a new project application. You’ll need to specify a name, description, and URL for the app, and click to create it.
Once you’ve created an app, click the “keys and access tokens” section and click to create an access token. This will generate a key for you, which you will need to keep on hand.
The bot we’re creating here is a variation on the bot created by Amit Agarwal, which can be found with a quick Google search. You will see at the top a lot of content in brown and a little bit in black. The brown is comments; they’re effectively invisible. The black is the actual code. The lines at the top are lines you need to edit. You see where it has lines for your consumer and access keys? Add your data there. You will also need to add in the search string you made in the first step.
If you want, you can scroll down and read the rest of the code. You shouldn’t change anything unless you know what you’re doing, though. The bot essentially checks Twitter every five minutes for new content that fits the search string you set up. When it detects some, it will retweet and favorite it. It also has a built-in semi-random delay in between actions, between 10 seconds and 1 minute, to make its actions look a little less like a bot. Regular actions on a timer are easily detected; adding a random timer helps minimize that risk.
Now, in your Google Drive, just click run and start the bot. That’s it! It’s set up to run and will run until such time as you turn it off. To turn it off, go back to the Drive and click run and turn off the bot.
An Alternative Bot Method
This next method is also based on one of Amit’s scripts, but it’s a little more roundabout. As a consequence, it’s also harder to detect as a bot, assuming you’re not using it in a way that’s really obviously a bot.
This time what you’re going to do is log in to Twitter and create a widget. To use one of your search query strings, click on the “search” tab of the widget creator and enter it there. Create the widget, and copy the URL. One part of the URL will be a long numeric string; copy that as the ID of your widget.
Next you will need to fetch Amit’s script, found here. What this script does is converts your Twitter widget – in this case an embedded version of the search query results – into an RSS feed. This is important, because Twitter stopped supporting tweet-to-RSS conversions, which is why we need to use this third party script. Note that the script in the link above might not work properly since a few recent Twitter changes. If it doesn’t, check this link and use the script there.
As before, once the script is in your Google Drive, you need to run it. It will ask to authorize on your Twitter account. Allow it to authorize and you’re good to go. Once the script has run, you will need to click publish and click deblow as a web app. Allow everyone to see it, click to create it, and copy the link it gives you. This is the link that will lead to the RSS feed, except it’s just displaying your feed. What you need to do is add a ? to the end of the URL and plug in the Twitter widget ID you copied earlier. This will execute the code on the widget, rather than your authorized feed.
This has all converted your search query into an RSS feed of results. That alone doesn’t serve our retweet purposes, though. Next we need to set up something that takes RSS input and retweets it. To do that, we’ll use IFTTT for a bit of macro automation.
Once you’ve registered for IFTTT, you can create a new recipe. Click to select an RSS input and feed in the URL of your widget-feed. Click next and click Twitter as the output. IFTTT will ask you what action is happening via Twitter here; you should click “post a tweet” and add “_RT _” in the what’s happening box.
Now what will happen is the script will run, which runs your Twitter search. When a new result appears, it is published to this custom RSS feed. When something new appears in this RSS feed, your IFTTT recipe bot will manually retweet that tweet. It’s recommended that you use the –RT filter in your search string to avoid retweeting your bot over and over.
This third method is a little more robust, but also requires you to know a little bit of coding. Once again, you’re going to want to start off by making a Twitter account for your bot, because running it from your main account will cause issues. On this new account, create a user list named cool-people and add your normal account to the list.
Next, log in with your new account and go through the process of making a new app at the apps.twitter.com page. You will want to set the access level to read and write, make sure the callback URL is blank, and create your access token. As before, copy down the access token keys and assorted data.
The code we’re using for this bot was posted on Github by Bryan Braun, though you may need to use Git to fetch the code.
The Github is here, and the code needed to git it is
- get clone https://github.com/bryanbraun/twitter-listbot.git
- cd twitter-listbot
Now you’ll need to open the index.js file in any text editor that doesn’t append meta data. That is, something like Wordpad or Notepad++, not Microsoft Word. You will need to change the me: XXXXX entry to the username of your bot account minus the @, and make sure the myList field is the same name as your list above. You don’t actually need to name it cool-people, that’s just what the default is.
Now the bot will retweet the tweets of the people in the list. This isn’t precisely the same as a search field retweet bot, but you can use it in conjunction with another bot to curate the tweets you want it to see.
At this point you have some complex coding to do. You need to plug in your access token information to the new bot, and you’ll need to run it using nodeJS. It’ll open a window that looks like it’s stuck doing nothing, which just means its listening for new tweets. Make a tweet on your normal account, and the bot account should see and retweet it.
You have some options here, including the ability to put the bot on a server to run so you’re not forced to leave your computer running and connected to the internet in order to keep the bot alive. You can read more about the authentication and the server hosting here.
All told, at least one of these methods should get you a bot you’re willing to use. Just remember to minimize how much spam-like activity it does. If you’re worried about spam, turn the bot off for a while, or increase the delay between posts so it’s less frequent. You might also consider increasing the engagement thresholds for a retweet, in case the 5 likes threshold is too low.