Automate Twitter actions using Node.js and deploy it on Heroku

I bet you too have thought of automating some aspects of your social media, let’s do some then. With this bot we can DM anyone who follows or likes our tweet and deploy it on Heroku.

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). Add an app to it by going to the following URL

Note: I would recommend to make a separate account to deploy the bot on.

STEP 1:

Go to app and get the following from keys and tokens section.

  • API key, 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 if the bot is 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 can 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 tokens obtained in Section 1.

STEP 2:

Make a .env file to put all the sensitive information 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%2
FnkpApHx1%2B%2FH0%3D5I7kLqCm5ejYNp5XoG8SbR96YoWxP3Po1J1RhyHwgPwj8E4rr8

SERVER_URL=https://fbc5f7e2c77f.ngrok.io

Note: This makes the heading of the section ironic but move ahead anyway, there is still loads to cover. (I have already changed the tokens)

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 have begun 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 likes 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-2 :
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.

Plese see that the 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

Well, high five on getting it this far!

Deploy Twitter bot on Heroku

In the first part of this post, we made a Twitter bot that greets followers and people who like our tweets, dependent on our local machine. Now let’s get rid of it and push it on Heroku.

Heroku bestows the functionality to make a Node app a Worker which would run continuously as opposed to a Web app that would be put to sleep if not visited for a brief period of time.

STEP-0 :
Make a Heroku app, then we can deploy the app using either Heroku CLI or attaching Github to it. Here, I am gonna use Heroku CLI.

STEP-1 :
Add a Procfile to make our app a Worker. Simply make a file with the name Procfile add it to the root and add the following code in it:

worker: npm start

STEP-2 :
Push the app to heroku by following the steps:

  • Install Git form here
  • Install Heroku CLI from here

Commit your changes using Git

cd myapp
git init
Initialized empty Git repository in .git/
git add .
git commit -m “My first commit”

  • Then create a Heroku app using heroku create twitter-bot-2021, this would also add a remote to your repository. Check the remote using git remote -v. This should show a Herko remote.
  • Now open the app by login in to the Heroku in order to put environment variables there as we cannot push .env file for security reasons.
  • Get the app URL by opening up the app using open app option from inside the app dashboard.
  • Then go to settings and click on Reveal Config Vars and insert the environment variables present in your .env file.
  • Now, finally deploy the code to Heroku using git push heroku master: main. If you are on the main branch in your local git repo then simply git push heroku main.
  • Checkout these docs related to deployment in case you are stuck somewhere.
  • Check the dashboard of the app, it should show a successful build in green.
  • Now, finally check the bot by following it form some other account, you should get the DM from the bot.

And, you have finally done it!