Codementor Events

Creating a Twitter bot with Node.js

Published Apr 05, 2018Last updated Oct 02, 2018
Creating a Twitter bot with Node.js

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.

Recently I’ve created a Twitter bot called Sofia (@digitalsofia) that can answer to your @mention queries, tweet a new word a day with its meaning and part of speech and retweet in response to the tweets with #technology and #design. Setting up the bot wasn’t very difficult but I noticed there weren’t many tutorials out there. Therefore, here, I’m going to show you how to create a twitter bot with the help of Node.js.

Getting started

A Twitter bot is a program that is always running in the background and can perform some sort of automation on the Twitter microblogging service, such as automatically follow Twitter users, automatically retweets in response to tweets that include a certain word or phrase, or posting @replies, etc.

Twitter bots can even do some interesting things. For example, if you tweet questions like “who is the first man on the moon” to @digitalsofia and the bot will @reply with the answer.

What we are building

What we’re going to build here is a simple Twitter bot that sends a search query to the Twitter API, receives a response containing the results of our search, and then retweets the most recent tweet returned. In my case, I set my bot to search for tweets containing either #technology or #design.

Tools we need

Here are the tools we’ll be using to create the bot —

To create applications on your Twitter account, you need to verify it using a mobile phone number. For that, you can use your own phone number or, if you have a Google account, create one using Google Voice. I highly recommend you create a new Twitter account, as bot activity often gets banned from Twitter. You likely don’t want that happening to your personal account.

Create a Twitter application

  1. 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.
  2. 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.

Initialize the Twitter bot

  1. Click here to get the source code and download it to your machine. Make sure the source code includes package.json and bot.js.
  2. In the bot.js file, edit the values of TWITTER_CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN and ACCESS_SECRET— you know them all from the previous steps.
  3. Set the TWITTER_SEARCH_PHRASE and all matching tweets will get processed by the Twitter bot, one at a time.

Test the bot

  1. Open your terminal and go to the directory where you have downloaded the source files — both package.json and bot.js.
  2. Type npm install in the terminal and required packages will be automatically installed on your system, provided you’ve internet connection.
  3. Type npm start or node bot.js to start the bot and if you get the message The bot is 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.

Final words

You can check out my bot on Twitter, and you can find the code on GitHub. If you need help in customizing your Twitter bot, or are trying to build a more complex Twitter bot, contact me via Twitter.

(This article is published here.)

Discover and read more posts from Debashis Barman
get started
post commentsBe the first to share your opinion
Show more replies