Let's Talk

India

#5 & 6, 2nd Floor, Eshwari Capital,
1st Main Road, Guru Raghavendra Nagar,
JP Nagar 7th phase,
Bengaluru, Karnataka 560078

Email

[email protected]

Follow us

Contact Us

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

Additional image
Automate-Twitter-actions-using-Node.js-and-deploy-it-on-Heroku-thumbnail1

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

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.

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.

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.

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.

Please 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:

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

  • 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 from some other account, you should get the DM from the bot.

And, you have finally done it!

Leave a Comment

Your email address will not be published. Required fields are marked *