Making a twitter bot is one of the many use cases of using the Twitter API. A bot can be used to enhance your business, assist customers through automated replies, automate retweets for specific terms, and a lot more.
This article is about how I created The Data Science Bot
using Node.js.
Table Of Contents
- Apply for Twitter developer account
- Create app on twitter
- Setup dev environment
- Write code in nodejs
- Deploy to heroku
- How to avoid duplication of retweets
Step 1: Apply for Twitter Developer Account
This is the most important and perhaps the most difficult step according to me.
- Login into Twitter
- Go to developers.twitter.com and click on 'Apply for a Developer account'
- Select the type of application i.e. student/business/hobbyist, etc.
- Mention the purpose of your application. Be as specific as you can over here.
Make sure you have read the Developer Agreement and Policy, Automation rules and The Twitter Rules thoroughly before applying.
Failing to meet these conditions will cause your application to be rejected
Read the full article on How to apply for a twitter developer Account
Step 2: Create app on twitter
Once your twitter developer account access has been approved.
Create an APP on apps.twitter.com.
Fill in the required details.Generate API keys. Click on your apps details and navigate to
Keys and tokens
β οΈ Warning (Don't reveal the API keys in any case).
Step 3: Setup Development Environment
Make sure you have Node.js and npm installed on your PC.
For Ubuntu, install using the following commands
sudo apt install nodejs
sudo apt install npm
For other distributions, click here for installation instructions.
Create a directory named your-botname
.
In my case, I have named it the-datascience-bot
.
To initialize the git environment and install the package twit
using npm
, enter the following commands inside the your-botname
directory:
Congratulations! You have successfully set up your Node.js DEV environment ππ
Step 4: Code
- Firstly, we have to authenticate
twit
. To do this, we will link our twitter app and code using the generated API keys.
Create a file named config.js
.
Put the following code in your config.js
file
config.js
Put your keys copied from your twitter apps dashboard.
- Next, we write the code for the bot in a file
bot.js
.
Here is the code.
bot.js
const config = require('./config')
const twit = require('twit')
const T = new twit(config)
function retweet(searchText) {
// Params to be passed to the 'search/tweets' API endpoint
let params = {
q : searchText + '',
result_type : 'mixed',
count : 25,
}
T.get('search/tweets', params, function(err_search, data_search, response_search){
let tweets = data_search.statuses
if (!err_search)
{
let tweetIDList = []
for(let tweet of tweets) {
tweetIDList.push(tweet.id_str);
//more code here later...
}
// Call the 'statuses/retweet/:id' API endpoint for retweeting EACH of the tweetID
for (let tweetID of tweetIDList) {
T.post('statuses/retweet/:id', {id : tweetID}, function(err_rt, data_rt, response_rt){
if(!err_rt){
console.log("\n\nRetweeted! ID - " + tweetID)
}
else {
console.log("\nError... Duplication maybe... " + tweetID)
console.log("Error = " + err_rt)
}
})
}
}
else {
console.log("Error while searching" + err_search)
process.exit(1)
}
})
}
// Run every 60 seconds
setInterval(function() { retweet('#DataScience OR #DataVisualization'); }, 60000)
Let's break it down.
- We initialize the twit object using our configuration details from
config.js
- The retweet function first calls the
search/tweets
API endpoint for the given search query. In my case, it is '#DataScience OR #DataVisualization' - We pass the following parameters to search API
-
q
: The search query -
result_type
: 'mixed' for latest as well as popular older tweets -
count
: number of tweets to retrieve at once
-
- The retrieved JSON object has the list of tweets having either #DataScience or #DataVisualization in it.
-We then pass each of the tweet IDs to the statuses/retweet/:id
API endpoint, which retweets the tweet.
Test the bot locally using the following command:
node bot.js
Your Twitter timeline should show the retweets.
Try debugging on the console if it doesn't work.
Step 5: Deploy to Heroku
- Create an account on Heroku
- Create an app named 'your-botname' on Heroku
Make sure the name of your local project directory is exactly the same as your Heroku project name.
- Install heroku-cli on your pc
If you have an Ubuntu/Debian based distribution, then use the following command to install heroku-cli
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
For other environments, the installation instructions can be found here
- Create a file named 'Procfile' having the following code
Procfile
worker: node bot.js
- Login to heroku CLI
$ heroku login
This will open a login link on heroku, where you have to enter your password.
- Deploy to Heroku using these commands
One last step
Start the worker
dyno on Heroku.
Go to dashboard.heroku.com/apps and click on your bot app.
Then, click on configure Dynos
Start the worker
dyno and click on confirm
And your bot is Ready! ππ
Step 6: How to avoid duplication of retweets
Add the following code in the for loop where we wrote
//more code here later..
Check duplication
Then outside the loop -
// Utility function - Gives unique elements from an array
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// Get only unique entries
tweetIDList = tweetIDList.filter( onlyUnique )
The utility function onlyUnique
can be written outside the retweet function as well.
This will let our bot avoid duplicate retweets.
Short explanation of the above code
Each tweet has a unique property
id_str
.
If you retweet someone's tweet, it has a differentid_str
.The search API finds both the original tweet and the retweeted ones.
When we call the
statuses/retweet/:id
API endpoint, it retweets all of them.To a novice Twitter user, it looks like your bot has retweeted the same stuff two or more times.
Something like this π
- If tweet text starts with "RT @" then it is a retweeted tweet, with a different
id_str
than the original. We take theid_str
of the original tweet and filter duplicates using theonlyUnique
function
There are a lot more things you can do with the API. Check out the API reference(https://developer.twitter.com/en/docs/api-reference-index) for more details.
Source Code -
The full source code can be accessed here
Sumedh-Patkar / the-datascience-bot
A Twitter bot for retweeting tweets having hashtags #DataScience, #DataVisualization
The Data Science Bot π€
The bot retweets tweets having hashtags #DataScience and #DataVisualization
Here is the link to my bot https://twitter.com/datasciencebot_
Alright, that's it!
This was my first tech article on the Developer community.
Any feedback would be greatly appreciated!
Thanks for reading! π
Top comments (9)
Thank you for this tutorial and the code, I have used it to make my own Bot:
github.com/dowenb/testeroftheday-t...
I have gone a slightly different way to keep the keys safe, using "dotenv" and a .env file locally, and the Heroku built in environment variables store. Otherwise it's pretty might a straight copy.
Where you are holding the keys you could make it a
.json
file instead of usingmodule.exports
with a.js
file. You wouldn't have to change anything in the code except for renaming therequire
from./config
to./config.json
.If I would like to automate something on my personal username and then have a bot on it's own Twitter username, do I apply for the developer account on just my account or the new one as well? I see the option to "create new username" is available when applying on my username but if I can avoid applying the second time, that'd be cool.
Hey. Apologies for replying late.
Yes. You have to apply for a developer account from both the usernames.
Amazing!! ππ Nice Explanationππ. I have a question that Can we also like the tweet using this bot?
Thank you!
Yes, you can. But automated liking of tweets is discouraged and can get your account suspended.
Check out Automation Rules, Section II - D
You could check out the twit docs here
Thanks for your article, it's the most comprehensive one that I've found.
Question from a newb: Would we need to create separate bot.js files for each search we want the bot to do? I'd like to run a similar query against a number of specific Twitter accounts, but I was reading that "Queries can be 512 characters long." developer.twitter.com/en/docs/labs...
I want to create a quote tweet using this project and I want my community to help with this.
please reply who knows how to solve this.