BEEP BOOP! How to make a Twitter Bot (Part 3)
Tags: twitter, node, bot, heroku
Published: 2019-11-08 02:43:56

---
title: BEEP BOOP! How to make a Twitter Bot (Part 3)
published: true
description:
tags: twitter, node, bot, heroku
---
Hey Friends! Welcome back!

In <a href="https://dev.to/nerdyandnatural/beep-boop-how-to-make-a-twitter-bot-part-1-4nhm" target="_blank"> Part 1</a> of our series, I showed you how to get started with building a bot by applying for access to Twitter's API. In <a href="https://dev.to/nerdyandnatural/beep-boop-how-to-make-a-twitter-bot-part-2-37pi" target="_blank">Part 2</a>, 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!

<h3>Step 1: Create a Heroku Account</h3>

If you don't have one already, <a href="https://heroku.com" target="_blank">click here</a> and create a Heroku account.

<h3>Step 2: Install Heroku and Create an App</h3>

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 <a href="https://devcenter.heroku.com/articles/heroku-cli#download-and-install" target="_blank">Heroku CLI page</a> 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](https://thepracticaldev.s3.amazonaws.com/i/ub5whxk7o1cjsvs8uf5m.PNG)

<h3>Step 3: Prepare to Deploy: Move Access Keys to Heroku</h3>

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](https://thepracticaldev.s3.amazonaws.com/i/116oii62diw5aar3e8in.PNG)

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](https://thepracticaldev.s3.amazonaws.com/i/bisv8wic31hcofck1ljt.png)

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!

<h3>Step 4: Deploy to Heroku!</h3>

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.

<h3>Step 5: Verify that the app works in Heroku</h3>

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.

<h3>Step 6: Schedule the app to run</h3>

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](https://thepracticaldev.s3.amazonaws.com/i/xl4ep43leqrh0685p9ob.PNG)

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

<h4>Important Info on reducing Tweet Spamming</h4>

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 <a href="https://twitter.com/cooltechrobot" target="_blank">@cooltechrobot</a> and <a href="https://twitter.com/whosehiringbot" target="_blank">@whosehiringbot</a>

Feel free to <a href="https://twitter.com/nerdyandnatural" target="_blank">follow me on Twitter!</a>

You can view the code for my bot on GitHub:

{% github nerdyandnatural/twitter-bot no-readme%}