DEV Community

Francis Sunday
Francis Sunday

Posted on

Introduction to Twitter Bots

This was my first attempt making twitter bots. I made a very simple twitter bot for this blog, check the Source and also follow @_hakasebot.

Setting Up

The bot was created using the Twit package, which is a Twitter API client for Node.js. Twit needs to connect with my twitter account so first I created a new Twitter Application. After that, I took note of my application's keys:

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

You can find these keys on the Keys and Access Tokens panel in your app's dashboard.

Once these keys are all ready, we create a new Node.js project and initialise the Twit package.

Prerequisites: Node.js, npm, and of course a PC

So you can create a directory and create three files package.json, config.js, and bot.js

In the config.js file, we setup Twit.

//config.js
const Twit = require('twit');
const TH = new Twit({ // Twit Handler
    consumer_key: APPLICATION_CONSUMER_KEY_HERE,
    consumer_secret: APPLICATION_CONSUMER_SECRET_HERE,
    access_token: ACCESS_TOKEN_HERE,
    access_token_secret: ACCESS_TOKEN_SECRET_HERE
});
Enter fullscreen mode Exit fullscreen mode

Basically, @_hakasebot does the following:

  • Listens for Events and keywords
  • Responds to Events
    • Like
    • Retweet
    • Reply

Listens for Events and Keywords

Twitter has a Streaming API, which gives us access to the streams of tweets. @_hakasebot uses two streams from the API:

  • The User Stream, which is a stream of tweets corresponding to a single user.
  • The Public Stream, which is the stream of all public tweets.

With the public stream, @_hakasebot can listen for tweets from any users that contains a defined keyword. This is possible when we create a stream of tweets based on a filter suing the statuses/filter endpoint, and passing an object with the filter parameters. The track parameter is used to filter tweets by keyword, and it accepts a string or an array of keywords to lookout for.

@_hakasebot runs on a filter that searches for mentions of this blog, so we'd implement as so:

const stream = TH.stream('statuses/filter', {
    track: ['hakasebot', 'hakaselabs', 'hakaselabs.github.io']
});
Enter fullscreen mode Exit fullscreen mode

When a stream if open, we can listen and respond to tweets that falls within that stream.

stream.on('tweet', (tweet) => {
    // We do something with that tweet here
});
Enter fullscreen mode Exit fullscreen mode

Responding to Events

We can respond to events by posting a tweet, retweeting, replying, follow a user, etc.
@_hakasebot is able to take three actions currently - like, reply, and retweet.

Liking a tweet

if the tweet was from another account, the bot likes it. To like a tweet, we post to the /favourites/create endpoint, passing the id of the tweet to be favorited.

stream.on('tweet', (tweet) => {
    if (tweet.user.id == _self.id) { // 
        // we'll get back to this 
    }
    TH.post('favourites/create', {
        id: tweet.id_str
    });
});

Enter fullscreen mode Exit fullscreen mode

Replying a tweet

If the tweet was from another user, the bot sends them a reply. We send a reply by posting to the /statuses/update endpoint and passing the id of the tweet we are replying to.

stream.on('tweet', (tweet) => {
    if (tweet.user.id == _self.id) {....}

    TH.post('favourites/create', {
        id: tweet.id_str
    });
    TH.post('statuses/update', {
        status: `@${tweet.user.screen_name} Thanks for sharing :)`,
        in_reply_to_status_id: tweet.id_str 
    });
});
Enter fullscreen mode Exit fullscreen mode

Retweeting

@_hakasebot retweets a tweet if it is found from my stream - That means if the tweet found from the stream is from myself @codehakase, it retweets it. We can retweet by posting to the /statuses/retweet/:id endpoint.

const _self = {
    id: 3354871743,
    screen_name: 'codehakase'
}
...

stream.on('tweet', (tweet) => {
    if (tweet.user.id == _self.id) {
        TH.post('statuses/retweet/:id', {
            id: tweet.id_str
        });
        return;
    }
    ....
});
Enter fullscreen mode Exit fullscreen mode

You can get your twitter account id from here

Deploying the Bot

I used Heroku to host @_hakasebot. Since its a Node.js app, we need to place some information in our package.json file:

  • The main script - The file Node.js would run
  • Dependencies
  • The version of Node.js

My package.json file looks this way:

{
  "name": "hakasebot",
  "version": "1.0.0",
  "description": "A twitter bot for hakaselabs.github.io",
  "main": "bot.js",
  "scripts": {
    "start": "node bot.js"
  },
  "author": "Francis Sunday - codehakase",

  "dependencies": {
    "twit": "^2.2.5",
    "express": "^4.14.0"
  },
  "engines": {
    "node": "7.9.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Great! you now have an idea of how to make bots for Twitter. You can follow @_hakasebot, view the source, to test, use the twitter share button below.

Have you built bots for Twitter? I'd like to know, drop them in the comment section below.

As originally published on My Blog-2017-05-30

Top comments (7)

Collapse
 
ienoobong profile image
Ibanga Enoobong Ime

Just built on in #Kotlin inspired by your post. Thanks so much! Do check it out, star too ;) github.com/IEnoobong/Eno-Twitter-Bot

Collapse
 
amadeot profile image
Amadeo

This is such a great guide!

Collapse
 
codehakase profile image
Francis Sunday

I'm glad you liked it

Collapse
 
johnlukeg profile image
John Luke Garofalo

This is really cool! I love how simple you made it. I'm adding this to my list of projects.

Collapse
 
codehakase profile image
Francis Sunday

I'm glad you liked it.

Collapse
 
jess profile image
Jess Lee

@amadeot think you might be interested in this article!

Collapse
 
codehakase profile image
Francis Sunday

Hey I appreciate, the share! Thanks