Let's Create a Twitter Bot using Node.js and Heroku (3/3)
Tags: node, twitter, javascript, tutorial
Published: 2020-02-05 20:08:57

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](https://dev.to/developer_buddy/let-s-create-a-twitter-bot-using-node-js-and-heroku-1-3-43kb) and [Part 2](https://dev.to/developer_buddy/let-s-create-a-twitter-bot-using-node-js-and-heroku-2-3-22g3). 

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](https://heroku.com). If you have a Github account you'll be able to link the two accounts. 

![Heroku Home Page](https://github.com/agyin3/images/blob/master/Twitter%20Bot%20Blog/Screen%20Shot%202020-02-03%20at%204.08.19%20PM.png?raw=true)

# 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](https://github.com/agyin3/images/blob/master/Twitter%20Bot%20Blog/Screen%20Shot%202020-02-03%20at%204.12.19%20PM.png?raw=true)

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

![Heroku App Description Page](https://github.com/agyin3/images/blob/master/Twitter%20Bot%20Blog/Screen%20Shot%202020-02-03%20at%204.18.12%20PM.png?raw=true)


# 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](https://devcenter.heroku.com/articles/heroku-cli) 

# 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 <your app name>

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
```

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
```
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](https://github.com/agyin3/images/blob/master/Twitter%20Bot%20Blog/addon-config.png?raw=true)

Do a search for <strong>Heroku Scheduler</strong> and add it to your app. 

![Heroku Scheduler](https://github.com/agyin3/images/blob/master/Twitter%20Bot%20Blog/heroku-scheduler.png?raw=true)

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](https://github.com/agyin3/images/blob/master/Twitter%20Bot%20Blog/heroku-scheduler-settings.png?raw=true)

You'll notice that I added <strong>node bot.js</strong> 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](https://twitter.com/coolnatureshots). You can also find the GitHub repo for it [here](https://github.com/agyin3/tweetbot-photo)