Twitter bot in Node.Js
Tags: javascript, node, twitter, beginners
Published: 2021-05-31 12:06:03

#### I bet you have wondered to automate some stuff on your social media, let's do some then.

With this bot we are going to DM anyone who follows us or likes our tweet. Here we are going to make use of the [Account Activity API](https://developer.twitter.com/en/docs/twitter-api/enterprise/account-activity-api/overview) in order to consume _follow_ and _like_ events from our account.
<br>

# Section-0: Environment Setup

__STEP-0:__ Make a Twitter account( or use an existing one) and add an app to that by going to the following [URL](https://developer.twitter.com/en/portal/projects-and-apps)
_P.S. I would recommend to make a separate account to deploy the bot on._ 

__STEP-1:__ Go to app and get following from _keys and tokens_ section.  
- API key, secret, Bearer Token, Access token and secret.
- Enable 3 legged authentications to add _callback URL_ for _webhook_.
(A _callback URL_ is required to register a webhook in order to consume events that would happen on our app, read more about it [here](https://developer.twitter.com/en/docs/twitter-api/enterprise/account-activity-api/overview))
- Set up the dev environment by going to _Products -> Dev Environments_ form the sidebar and name all of them __dev__.
- Give _Read, Write, and Direct Messages_ permissions form the __App permissions__ section.

__STEP-2:__ Install the following dependencies to use the Twitter APIs in Node.js
- twitter-lite
- dot-env
- twitter-webhook
- express
- body-parser

__STEP-3:__ Download and install `ngrok` to get a HTTPS URL for our local ports and to check the bot working before deploying it to Heroku.
<br>

# Section-1: Securing API keys

__STEP-0:__ First, make _bot.js_ file as entry point to our app. Then, by making use of `twitter-lite` npm package we would interact with Twitter endpoints.

__STEP-1:__ Now, open `ngrok` and get a HTTPS URL for the local port 3004 and paste the URL in the __.env__ file along with the tokens obtained in __Section 1__.

__STEP-2:__ Make a _.env_ file to put all the sensitive info form _Section-0_
```
API_KEY=xIrIqvysinxgrSPm0Ir1D0cIF
API_SECRET_KEY=DjIMhoxJlRCxbgtw1zXdiGuKE4IjmjlAmnvB6orus24jbzmNof
ACCESS_TOKEN=1017764619337646080-3FrdsqhBLgVGDRAPEiy2a3fI7bY8Tv
ACCESS_TOKEN_SECRET=sRRfI6osPcvnwoHTY8UIA8y2hsI40kMltauL4xspAuMfr
ENVIRONMENT=dev
ROUTE=/callback-url
BEARER_TOKEN=AAAAAAAAAAAAAAAAAAAAACXW7gAAAAAA%2BbYioHfLTHR7Mf%2FnkpApHx1%2B%2FH0%3D5I7kLqCm5ejYNp5XoG8SbR96YoWxP3Po1J1RhyHwgPwj8E4rr8
SERVER_URL=https://fbc5f7e2c77f.ngrok.io
```
_P.S. This makes the heading of the section ironic but anyway move ahead, there is lots to cover. Have already changed the tokens BTW._

__STEP-3:__ Make a __config.js__ for reading environment variables.
```js
module.exports.twitter = {
  consumer_key: `${process.env.API_KEY}`,
  consumer_secret: `${process.env.API_SECRET_KEY}`,
  access_token_key: `${process.env.ACCESS_TOKEN}`,
  access_token_secret: `${process.env.ACCESS_TOKEN_SECRET}`,
}

module.exports.webhooks = {
  serverUrl: `${process.env.SERVER_URL}`,
  route: `${process.env.ROUTE}`,
  consumerKey: `${process.env.API_KEY}`,
  consumerSecret: `${process.env.API_SECRET_KEY}`,
  accessToken: `${process.env.ACCESS_TOKEN}`,
  accessTokenSecret: `${process.env.ACCESS_TOKEN_SECRET}`,
  environment: `${process.env.ENVIRONMENT}`,
}
module.exports.webhooksUserActivity = {
  accessToken: `${process.env.ACCESS_TOKEN}`,
  accessTokenSecret: `${process.env.ACCESS_TOKEN_SECRET}`,
}
```
  
<br>
# Section-2: Code

__STEP-0:__ In the following code we get the _user id_ of our account and then register a webhook using `twitter-webhooks` package to receive _follow_ and _ favorite tweet_ events on our account. Paste the following code in __bot.js__.

```js
require('dotenv').config()

const express = require('express')
const Twitter = require('twitter-lite')
const twitterWebhooks = require('twitter-webhooks')

const { twitter, webhooks, webhooksUserActivity } = require('./config')

const twitterLiteClient = new Twitter({ ...twitter })

const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))


const userActivityWebhook = twitterWebhooks.userActivity({
  ...webhooks,
  app,
})

;(async function () {
  //  get user id
  const result = await twitterLiteClient.get('users/lookup', {
    screen_name: '<twitter id of your app>',
  })
  const userId = result[0].id_str

  //     get webhooks
  const webhooksResult = await userActivityWebhook.getWebhooks()

  if (webhooksResult.environments[0].webhooks.length !== 0) {
    //  unregister earlier webhook
    const webhookId = webhooksResult.environments[0].webhooks[0].id
    await userActivityWebhook.unregister({
      webhookId,
    })
  }

  //Register your webhook url - just needed once per URL
  await userActivityWebhook.register()

  //Subscribe for a particular user activity
  userActivityWebhook
    .subscribe({
      ...webhooksUserActivity,
      userId,
    })
    .then(function (userActivity) {
      userActivity
        .on('follow', data => console.log(userActivity.id + ' - follow'))
        .on('favourite', data =>
          console.log(userActivity.id + 'favourite')
        )
    })
    .catch(console.error)
})()
app.listen(process.env.PORT || 3004)
```

__STEP-1:__ Now, once we have attached the webhooks and started listening for the events, let's do some activity on our account using the `twitter-lite` package. Here I am greeting the new follower or the user who like a tweet by sending them a DM.

```js
userActivityWebhook
    .subscribe({
      ...webhooksUserActivity,
      userId,
    })
    .then(function (userActivity) {
      userActivity
        .on('follow', async data => {
          const followerName = data.source.screen_name
          const followerId = data.source.id

          console.log(`\n ${followerName} followed you \n`)
          try {
            await twitterLiteClient.post('direct_messages/events/new', {
              event: {
                type: 'message_create',
                message_create: {
                  target: {
                    recipient_id: followerId,
                  },
                  message_data: {
                    text: `Hey ${followerName}! Thanks for following. You are awesome`,
                  },
                },
              },
            })
          } catch (err) {
            console.error(err)
          }
        })
        .on('favorite', async data => {
          console.log(JSON.stringify(data))
          const followerName = data.user.screen_name
          const followerId = data.user.id_str

          console.log(`\n ${followerName} liked a tweet\n`)

          try {
            await twitterLiteClient.post('direct_messages/events/new', {
              event: {
                type: 'message_create',
                message_create: {
                  target: {
                    recipient_id: followerId,
                  },
                  message_data: {
                    text: `Hey ${followerName}! Thanks for liking the tweet`,
                  },
                },
              },
            })
          } catch (err) {
            console.error(err)
          }
        })
    })
    .catch(console.error)
```

__STEP-3:__ Run `node bot.js` to run it locally and do follow the bot from your personal account and you would get a DM form the bot.

P.S.: Sad part is that with a free account only 5 DMs can be sent in a 24 hour window. But a plethora of other things can be done with a much larger cap, here is the [API reference](https://developer.twitter.com/en/docs/twitter-api/api-reference-index)

High five on getting it this far.
![](https://media.giphy.com/media/6q29hxDKvJvPy/giphy.gif)

_[Follow part 2 of the tutorial](https://dev.to/neer17/deploy-twitter-bot-on-heroku-gnk) to deploy it on Heroku._