creating-twitter-bot-head-img.jpeg

Photo by freestocks.org on Unsplash

Nowadays the use of social networks is considered a normal thing, something that is part of our daily life. Maybe most of the people even don’t think about it. They just use them automatically. Just like that.

But some of these people may also ask the question which made me write this article — what do we get from these networks for giving them our personal data, our interests and preferences? What can we use them for?

Well, creating a Twitter bot is not new nor hot topic (not even hard to do), but it’s one of those things I am alluding about. Twitter is a social network that generates and uses enormous data flows from people to people. And the so called bots in this network are actually small programs written in a certain programming language.

One of the most valuable thing we can get from the social networks is the data. And we can do that in an automated way. Here is where these pieces of software, the bots, can help us.

If you google the topic, you will see that one of the most common examples for a Twitter bot is the one that retweets or likes certain tweets, searched by certain criteria. That criteria can be defined by the creator of the bot. The more I was reading on the topic, the more I was thinking about what can I do with such bot. Also I had to make sure that this is not going to spam and annoy other people. Again, the simplest answer is the right one. I have decided to create a bot that will send me private messages with information about tweets related to topics I am interested in. Very shortly after I did the first version (started around 5 in the morning, hence the title of the article :)) I thought myself that I have to do it a bit more useful for the other people as well.

This is how I ended up creating a Twitter bot with Node.js that sends direct messages to his followers with information about tweets relevant to certain search criteria.

Enough talking for now, let’s begin the fun part.

TL;DR

The rest of the article will be a step by step tutorial of how to create a twitter bot and deploy it to Heroku. The bot will be sending private messages to his followers. The messages will contain tweets related to predefined search criteria. You may already be familiar with the term #hashtag. Practically such hashtags are going to be our search criteria or the words by which we will filter the tweets. In my case I have selected to use the following: #js, #javascript, #JavaScript, #JS, #Javascript, #react, #reactjs, #nodejs, #Nodejs. Which means that the bot will send messages with tweets mentioning them. Then we will fetch the IDs of all the followers that the bot has and use them to send direct messages back to them.

If you want to skip right to the code, here is the GitHub repo.

Initial setup Setup the project via npm:

Since this is going to be Node.js based project, it makes sense to use the famous node package manager NPM. First, make sure you have Node.js installed and then you can use npm.

After you are done with the installations you can setup a simple project for our bot. In order to start, type the following in your terminal:

  1. npm init
  2. Answer the questions that npm asks you to create your package.json file (you can skip this and use the defaults answers by adding y parameter like so: npm init -y)
  3. Install the dependencies we need by typing: npm i twit dotenv --save-dev. This will install all the packages we need to create our bot. (‘i’ is short for ‘install’)

Note about packages: both packages are famous in what they do. The first one, dotenv, is for handling the environment variables via .env file. This is where we will store our API keys and tokens. The second one, twit, is a Twitter API client for Node.js, which is providing a nice and easy to use interface for interacting with Twitter API.

Configuration settings