Learn how you can create your own Twitter bot using Node.js and the new Twitter API. The bot will auto retweet in response to tweets with some particular hashtags. (https://goo.gl/4whEIt)
Here are the tools we’ll be using to create the bot —
- Node.js installed in your machine
- A registered Twitter account
- Create a new account at Twitter that will become your bot. Then go to apps.twitter.com, sign-in with your new Twitter account and create a Twitter application. Give your application a name, description and put any URL in the website field. Keep the callback URL field blank, agree to the terms and submit the form to create your first Twitter application.
- Once the Twitter application has been created, click the Keys and Access Tokens tab and click the Create my Access Token button. Twitter will generate the Consumer Keys and Access tokens that we will need in a later step.
- Click here to get the source code and save it to your machine. Make sure the source code include package.json and bot.js file.
- In the bot.js file edit the values of
TWITTER_CONSUMER_KEY
,CONSUMER_SECRET
,ACCESS_TOKEN
andACCESS_SECRET
— you know them all from the previous steps. - Set the
TWITTER_SEARCH_PHRASE
and all matching tweets will get processed by the Twitter bot, one at a time.
- Open your terminal and go to the directory where you have saved the source files — both package.json and bot.js file.
- Type
npm install
in the terminal and required packages will be installed on your system. - Type
npm start
ornode bot.js
to start the bot and if you get the messageThe bot is happily running…
in the terminal, that’s good. The bot is listening for tweets coming in. You can even hop to your Twitter bot account to check whether the bot is working or not.
Written by Debashis Barman
Licensed under http://creativecommons.org/licenses/by-sa/3.0
Report error and bugs to [email protected]
@santosh It is possible that your bot didn't find any tweets with your hashtags. It is always wise to check if the parent of
id_str
is not undefined.