Set up a heroku account

Simply create a free account and download the heroku Command Line Interface. No need to put in any credit card information.

Using the Twitter API

Building a twitter bot is actually pretty easy to achieve, at least easier than I would have expected. Twitter provides a REST API and a Streaming API for free. The REST API provides you a wide variety of functionality such as searching and filtering tweets, tweeting and retweeting, favoriting, following people, sending direct messages etc. With the Streaming API you can open a connection to one of the streaming endpoints and get tweets or listen to events in realtime, which is exactly what we need to get notified when someone follows us. If you are interested, you may have a look at the twitter API documentation.

Our bot will be very simple and comprise only a few lines of code. Really, the only thing we have to is to listen to events via a stream and send a direct message once a user followed us. Sounds easy enough, but to make it even simpler we will use twit, which is an npm package that allows us to easily interact with the twitter API via javascript.

Coding the Twitter Bot

With twit, we can set up a twitter client as follows:

Do never put any kind of credentials in your source code. We will pass the credentials as environment variables, which we are going to configure for heroku later on.

Next thing we want to do is write some helper functions to verify, whether our credentials are valid:

And for sending a direct message to a user:

Important!!! We are always going to use the string version id_str of user and tweet ids, since javascript is not able to cope with large integer values thus will fail to represent the ids as numbers.

To send a direct message we have to specify the id of the recipient and a text, which is our nice greeting message:

You can adjust the template to insert your greeting message. Furthermore you can send emojis (line 6) if you know their utf-16 code. Check out this site to look up codes for emojis.

Now that we have everything in place, we can open a stream to the user endpoint:

Once we ensured that our credentials are valid, we are going to add a listener to the stream to catch any follow event. The follow event fires when some user follows us and when we are following someone else. Obviously we only want to send a greeting message in the first case, thus we are checking whether the user id of the originator of that event (source) is not our own account_id. Conveniently, the id of our account is returned from the account/verify_credentials endpoint.

And that’s it already. We can now push our code to heroku.

Deploying the Bot to Heroku

If you have cloned the repository you can simply cd into the cloned folder, otherwise make sure to initialize a new git repository in your working directory:

git init

Then we can create a heroku app from the CLI. This will kindly add a remote with the alias heroku to your heroku app repository as well:

heroku app:create mytwitterbot1234

Next thing we want to do is to configure heroku to use our twitter API credentials. To set environment variables from the CLI, use the following commands and fill in your credentials:

heroku config:set consumer_key=xxx

heroku config:set consumer_secret=xxx

heroku config:set access_token=xxx

heroku config:set access_token_secret=xxx

You can now commit all your changes, such as editing the greeting message and push the code to heroku, which will build and run your app:

git add .

git commit -m "changed the greeting message"

git push heroku master

Configuring a worker dyno

By default any app deployed to heroku will run in a web dyno, which means, that you can hit it with http requests from the outside. Obviously we do not want that, we just want our bot to run as a background task. Therefore we will shut down the web dyno and spin up a worker dyno for our app instead:

heroku ps:scale web=0 worker=1

In order to spawn a worker dyno, you will need to push a Procfile to your heroku repository as well. Just specify the following startup command in this file and you should be ready to go:

worker: node index.js

If everything worked out, your bot should be running. You can test your bot by following: justapoliteguy, who will follow you back. Check if your greeting message was sent. You can retry as many times as you wish by unfollowing and following again.

Troubleshooting

In case your bot does not seem to work, you can open the heroku dashboard of your app and navigate to View logs by clicking the More button in the upper right corner: