DEV Community

Cover image for Let's Create a Twitter Bot using Node.js and Heroku (3/3)
Buddy Agyin 🦁
Buddy Agyin 🦁

Posted on

Let's Create a Twitter Bot using Node.js and Heroku (3/3)

Welcome to the third and final installment of creating a twitter bot. In this post, I'll show you how to automate your bot using Heroku.

If you haven't had the chance yet, check out Part 1 and Part 2.

After this, you will have your own fully automated Twitter bot. Let's jump in.

1. Setup Heroku Account

You'll want to sign up for a Heroku account. If you have a Github account you'll be able to link the two accounts.

Heroku Home Page

2. Create Your App

Once you're all set up with your account, you'll have to create an app.

In the top right corner, you'll see a button that says 'New' Click on that and select 'Create New App'

Heroku Create New App

That should take you to another page where you'll have to name your app.

Heroku App Description Page

3. Install Heroku

You can install Heroku a few different ways depending on your OS. If you want to use the CLI to install it, enter the following code in your terminal

sudo snap install --classic heroku

If that didn't work for you, you can find other ways of installing Heroku to your device here

4. Prepare For Deployment

Open up your terminal and cd into your tweetbot folder. Once inside run this code to log in to your Heroku account.

heroku login

You'll have the option to log in either through the terminal or webpage.

If you haven't deployed to Github run the following code. If you have you can skip this part

git init

Now you'll want to connect to Heroku's remote git server. Run this code in your terminal.

Be sure to replace <your app name> with the name of your Heroku's app name

heroku git:remote -a

Almost there!!! You just want to setup our access keys on Heroku's server.

You can do this directly in terminal quite easily. Run the following code to get it setup.

You're actually just going to be copying it over from your .env file

heroku config:set CONSUMER_KEY=XXXXXXXXXXXXXXXXXXXXXXXXX
heroku config:set CONSUMER_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
heroku config:set ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
heroku config:set ACCESS_TOKEN_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Enter fullscreen mode Exit fullscreen mode

Sweet! Now we are going to create a Procfile to configure the process we want Heroku to run.

touch Procfile

Once you have this file created open it up and add the following code inside

worker: node bot.js

Now you just need to commit and push your files up to the Heroku server.

Run this last bit of code in your terminal

git add .
git commit -m "add all files"
git push heroku master
Enter fullscreen mode Exit fullscreen mode

Time to test out our bot now that it's on Heroku. In your terminal, run the following:

heroku run worker

You should see your terminal output 'Retweet Successful' and 'Favorite Successful'

If you're getting some type of error message, be sure to double-check your code and your deployment.

5. Time To Automate

All that's left is getting our bot to run on a schedule. I really like the Herkou Scheduler add to handle this.

Go back to your overview page on Heroku and select configure add-ons

Heroku Configure Add-on

Do a search for Heroku Scheduler and add it to your app.

Heroku Scheduler

Now click on Heroku Scheduler to open up the settings in a new window.

For this example, I'm going to configure mine to run every 10 minutes. You can change this to run every hour or less if you'd prefer.

Heroku Scheduler Settings

You'll notice that I added node bot.js under the Run Command section. You'll want to do the same so Heroku knows which command to run for your bot.

There you have it!!! You have now successfully created your own automated twitter bot.

If you'd like to check mine out you can at @coolnatureshots. You can also find the GitHub repo for it here

Top comments (2)

Collapse
 
jaredgib profile image
Jared Gibb • Edited

Hey! I just got mine live! Thank you!!!

Collapse
 
developer_buddy profile image
Buddy Agyin 🦁

That's awesome to hear man! I'm glad you were able to find it useful :) Feel free to share you're bot and/or code, I'd love to check it out