Twitter bot in Node.Js

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 in order to consume…


This content originally appeared on DEV Community and was authored by Neeraj Sewani

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 in order to consume follow and like events from our account.

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
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)
  • 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.

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.

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}`,
}

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.

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.

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

High five on getting it this far.

Follow part 2 of the tutorial to deploy it on Heroku.


This content originally appeared on DEV Community and was authored by Neeraj Sewani


Print Share Comment Cite Upload Translate Updates
APA

Neeraj Sewani | Sciencx (2021-05-31T12:06:03+00:00) Twitter bot in Node.Js. Retrieved from https://www.scien.cx/2021/05/31/twitter-bot-in-node-js/

MLA
" » Twitter bot in Node.Js." Neeraj Sewani | Sciencx - Monday May 31, 2021, https://www.scien.cx/2021/05/31/twitter-bot-in-node-js/
HARVARD
Neeraj Sewani | Sciencx Monday May 31, 2021 » Twitter bot in Node.Js., viewed ,<https://www.scien.cx/2021/05/31/twitter-bot-in-node-js/>
VANCOUVER
Neeraj Sewani | Sciencx - » Twitter bot in Node.Js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/31/twitter-bot-in-node-js/
CHICAGO
" » Twitter bot in Node.Js." Neeraj Sewani | Sciencx - Accessed . https://www.scien.cx/2021/05/31/twitter-bot-in-node-js/
IEEE
" » Twitter bot in Node.Js." Neeraj Sewani | Sciencx [Online]. Available: https://www.scien.cx/2021/05/31/twitter-bot-in-node-js/. [Accessed: ]
rf:citation
» Twitter bot in Node.Js | Neeraj Sewani | Sciencx | https://www.scien.cx/2021/05/31/twitter-bot-in-node-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.