DEV Community

Sarah Williams
Sarah Williams

Posted on

BEEP BOOP! How to make a Twitter Bot (Part 3)

Hey Friends! Welcome back!

In Part 1 of our series, I showed you how to get started with building a bot by applying for access to Twitter's API. In Part 2, I showed you how to build out and test your bot in your dev environment.

In our 3rd and final part of this series, I'll show you how to get your bot deploy to Heroku and get it automated!

Up until this point we've been playing around in the dev environment. Now it's time for us to go live and put our little bot out into the world! Let's get started!

Step 1: Create a Heroku Account

If you don't have one already, click here and create a Heroku account.

Step 2: Install Heroku and Create an App

There are a couple of different ways to install Heroku. I installed it via snap, which seems to be the easiest way to do it. Go ahead and type this into your terminal:

sudo snap install --classic heroku

If this way doesn't work, there are other ways to install it. Go to the Heroku CLI page for more instructions on how to get it installed.

Once you have it installed, go back to your Heroku dashboard, click new at the top right to create an app. Fill out the required info (ignore the pipeline option) and then hit Create App.

Alt Text

Step 3: Prepare to Deploy: Move Access Keys to Heroku

Time to get ready to deploy! To do this first we need to move our keys.

To do this, go to your dashboard and under the new app you just created, click on the Settings tab.

Alt Text

From there, select the Config Vars button. You will then see a field with KEY and VALUE. For the keys, you will add the access key name convention and in the VALUE part you will add your actual keys.

Alt Text

Next, change out your config.js file and add this:

module.exports = {
  consumer_key: process.env.BOT_CONSUMER_KEY,  
  consumer_secret: process.env.BOT_CONSUMER_SECRET,
  access_token_key: process.env.BOT_ACCESS_TOKEN,  
  access_token_secret: process.env.BOT_ACCESS_TOKEN_SECRET

Don't forget to hit Save!

Step 4: Deploy to Heroku!

Alright, we're about to deploy! Are you ready? Of course you are!

First things first, go into your terminal and type this

touch Procfile

Once this file is created, add this to your file and save

worker: node bot.js

What is this file anyway? This is a config file that will tell Heroku to run your bot when it's deployed.

Next we can start our deployment to Heroku! First if you haven't logged into it from your terminal type this:

heroku login

This will open a browser window so that you can authenticate to Heroku. Once this is completed, return to your terminal and it should be logged in.

Then type this:

git init

Next, you need to add the app you're going to deploy to. Go to your deploy tab under your app and copy the remote add command. It should look like this:

heroku git:remote urlhere

Alright, now we can start staging our files to be committed! If you haven't noticed already, Heroku uses git commands. So let's add our files and commit them:

git add .
git commit -m "Initial Commit"

Alright, now for the final step: deployment! Type this:

git push heroku master

A bunch of cool things will start to happen, but basically the files will start copying to Heroku and Heroku gets it working on the server.

Hooray! We got our bot on the server, but we're not finished yet.

Step 5: Verify that the app works in Heroku

We need to make sure that our bot is properly working now on the server. To verify, type this command in the terminal:

heroku run node bot.js

This will run the node command on the Heroku server. It should return the same response as it was in the dev environment. If it doesn't, check your files, make sure everything save correctly and try redeploying. If it does, hooray! Your bot is working on Heroku.

Step 6: Schedule the app to run

Alright, now for the final steps to make our bot automate!

Go back to Heroku and click on Configure Add-Ons. Under the Add-Ons section you will see a search bar. Type in "Heroku Scheduler" and the option should come up to select. You can use the free option and then click on Provision.

Once added click on the Heroku Scheduler and it will take you to it's menu. Click on Create Job and the following option will appear:

Alt Text

Fill out the following fields as pictured and then hit save. After 10 minutes, your bot should start to auto-tweet!

Important Info on reducing Tweet Spamming

Be sure to monitor your bot account for the first hour to see how often tweets are hitting the timeline. You want to make sure that your bot isn't spamming your followers and that it isn't going to hit its tweet limit too quickly. If you find that your bot is retweeting a lot (say about 100 within the first hour) here are some tips to help reduce the tweet frequency:

1) Decrease the scheduler to have it run less often

You can have the scheduler tweet every hour or less to reduce spam

2) Niche down on you search query

You can "niche down" by making your search queries more specific in order to reduce the amount of tweets. For example, instead of having the bot retweet posts with #jobs have it retweet #jobs and #SoftwareEngineering.

3) Decrease the search results count

The best practice is to have the retweet count to be set to 10 tweets. If it still looks at though your bot is spamming, you can reduce the tweets to a lower number to space out the tweets a bit more over time.

And there you have it! Now your bot is retweeting it's tweet automagically!

I hope you enjoyed these tutorials 😊

You can see both my bots in action on Twitter @cooltechrobot and @whosehiringbot

Feel free to follow me on Twitter!

You can view the code for my bot on GitHub:

Top comments (3)

callumcosta profile image

HI Sarah tried to reach you on twitter. I built this bot however I keep hitting the same error "Running node bot.js on ⬢ creatorsunited... up, run.7891 (Free)
[ { code: 215, message: 'Bad Authentication data.' } ]"

checked the keys rebuilt the app on twitter and creds on heroku. any ideas?

sivasankaramalan profile image
Sivasankaramalan Gunasekarasivam

Hey, any luck with this?

sivasankaramalan profile image
Sivasankaramalan Gunasekarasivam

@nerdyandnatural can you please help me with this issue

[ { code: 215, message: 'Bad Authentication data.' } ]