A few months ago I posted how to "create a twitter bot in 3 minutes". Since then Twitter API changed so I decided that its time for an update. I also saw a post by @omarhashimoto - "How to build a simple Twitter bot in 17 lines of code" which motivated me to reduce my code to maximum 17 lines, I could archive 13 lines of code by not displaying error messages but the truth is that sometimes more code is actually better. Alright, time to start!
What we are coding?
We want to create a Twitter bot that would automatically like all tweets that include keywords specified by us. We will be using Stream API which means that all the likes and listening will be happening in real time.
What you need?
Setup
After you finish installing Node.js, open terminal and navigate to your project folder.
Type npm init
and complete the setup. Next step is to install our dependences, we are using Twitter for Node.js so type in npm i twitter --save
. The last thing we need is Twitter app, you can create your Twitter app by visiting https://apps.twitter.com. Once you create your app, all you need is 4 keys that you are going to use for connection between the app and your code:
- Consumer Key
- Consumer Secret
- Access Token Key
- Access Token Secret
now create new JavaScript file, touch bot.js
Coding
First we want to import our Twitter library, then we have to set up connection with our app. You should enter the keys from your Twitter app.
const Twitter = require('twitter');
const client = new Twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
Now we need to specify our stream listening preferences, as you can see I added two keywords that I want to track "#IlikeBots" and "keyword2". You can add as many as you like, you can also use hashtags or just normal words it doesn't really matter. It's good to know that Twitter will limit your likes per hour so adding extremely popular keywords would exhaust your limit really quick.
const stream = client.stream('statuses/filter', {track:'#IlikeBots, keyword2'});
All the magic happens here, we are receiving 'data' object from the stream, every single 'data' object consists of all informations about the tweet that matches our keyword. The middle part is where we do the Like action, all we need is the tweet id which we are taking from our 'data' object. Once the like happens we are printing ID of the tweet and the content. If something goes wrong we are going to throw the error.
stream.on('data', (event) => {
client.post('favorites/create', {id:event.id_str}, (error, response) => {
if(error) throw error;
console.log('Tweet ID: '+response.id_str+' Liked! - "'+response.text+'"')
});
});
Before we start reading and liking the tweets, we want to make sure that we can read error codes, this is very important because fixing something without knowing what the problem is would be pure waste of our time. If we get for example error CODE 429 we would know that our app works just fine and the real problem is just Twitter API limit. You can take a look at all error codes in the Twitter API documentation..
stream.on('error', (error) => {
throw error;
});
Update by @mrm8488 from comment section:
It would be good to handle the error on: "stream.on('error'...". If you throw the error as it is the upper function, the error will reach the event loop and the application will crash.
A quick solution would be:
stream.on("error", error => console.error(error));
Time to run our code, type node bot.js
in the terminal. Tweet something using your keywords, you should be able to see feedback in the terminal.
Final Code
const Twitter = require('twitter');
const client = new Twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
const stream = client.stream('statuses/filter', {track:'#IlikeBots, #keyword2'});
stream.on('data', (event) => {
client.post('favorites/create', {id:event.id_str}, (error, response) => {
if(error) throw error;
console.log('Tweet ID: '+response.id_str+' Liked! - "'+response.text+'"')
});
});
stream.on('error', (error) => {
throw error;
});
Top comments (6)
Hi, Martin! First of all, congrats for the post!
It would be good to handle the error on: "stream.on('error'...". If you throw the error as it is the upper function, the error will reach the event loop and the application will crash.
A quick solution would be:
thanks! my only concern would be, if we have the app still running after for example reaching API requests limit for the next 10 hours, are we risking Twitter ban? I think we would also have to implement counter+timer. What you think?
It depends on the Twitter API. But you are not requesting anything to its REST API. You are subscribed to an event. So maybe they manage how much data they can give you.
right, there are limits but its something like 1% above threshold, I updated the post, cheers!
You know that is not 17 lines of code because you need to add the dependencies, but what matter ¯_(ツ)_/¯
let's not be that strict 😅