What is Twitter Bot :
It is typically an application which helps to retweet, favorites, on the basis of hashtags and replies to other users if they follow it. In our case, we'll be listening for someone to tweet with a certain hashtag and then retweet something when that happens.
2. You need a Twitter Account
3. Should be familiar with command-line tools for your operating system.
4. A valid phone number that you can verify via SMS
Installation Process :
Go to the Node.js website and download the LTS (long-term support) option according to the OS support. After it downloaded, run the installer process until the end.
It will also install the NPM for you. To confirm that you have installed the process properly, open Command Prompt (For Windows) or Terminal (For Linux, Mac). Run the following commands - node -v and npm -v.
Initialize the project and Install twit module :
Create a folder and inside that folder initialize it with npm init command. Once that is complete, you should see package.json and to simplify our interactions with the powerful Twitter API, we will install twit named module using npm install twit --save.
Creating a Twitter App :
First create or log in to your twitter account, ensure you have verified mobile number as Twitter requires this to register any new applications. After the verification process, head on over to the Twitter Application page (https://developer.twitter.com/en/apps) to start creating a new Twitter application. You need to fill all the details so that you can proceed with this page -
After that give your application a name, description, website (it can be anything really), check the checkbox, and hit "Create your Twitter Application". Once created, head to your bot from this page (https://developer.twitter.com/en/apps) and then click on the tab that says "Keys and Access Tokens".
On this page you can get the 4 things you need for your bot:
- Consumer Key/API Key
- Consumer Secret/API Secret
- Access Token
- Access Token Secret
With all the information you need from Twitter on-hand and must agree to the Developer Agreement. After that, we can begin coding!
Starting the Bot :
Create a file index.js (As you can keep any name), import the twit module and then initialize an instance of it by calling the constructor for it and passing in the credentials you obtained earlier -
Start the Bot using
npmstart or node index.js command and want to know about statuses/update, you can refer to https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update.
In the above example, we have used Streams API of Twitter, we can let Twitter do the filtering of events for us, and then only do something when an event that we care about does occur. Here these all are the endpoints you could connect to:
In this Blog, we have seen how we can use Node.js and the twit module to create a simple Twitter bot that automatically responds to users that follow an account. We can do much more than simply tweet out, including retweeting and liking other tweets. You can refer more about this module at - https://www.npmjs.com/package/twit.
Thanks for reading and Let me know if any Suggestions!