Currently the twitter bot runs on our PC and that’s not what we want. We want to run it on a server online. Heroku is a service that provides this facility for free. Of course there’re some limitation to the free package but you can get their premium packages if you want.
This is the last part of my Twitter Bot tutorial series.
Check out the first two parts here
1. Create a Heroku account
Create an account here. If you create a free account then you can host up to 5 different applications. Once you have your new account created create a new app. Now there’s a whole bunch of things we need to do here. Basically there are 3 different ways we can add our code on heroku
- Through Heroku Command Line
I would recommend the first way but if you aren’t familiar with using command lines then this might be not so simple to grab. However you can just follow along by using the syntax I provide here
2. Heroku Command Line Interface (CLI) and GIT:
Download Heroku Command Line from here or simply click on the link below.
Now we need to download one more thing and that’s GIT – a CLI for GitHub. Download GIT here. Run those two installation files. If you have any trouble installing comment below or you can look for tutorials online, there are plenty.
By now you should have Heroku CLI and GIT installed. To check if you have successfully installed those two open your command line and type
heroku --verion. If you get a reply something like this
heroku-cli/6.14.22-f598c4a (windows-x64) node-v8.4.0 then the app was installed successfully. Likewise type
git --version and you should see the git version.
Please proceed to rest of the tutorial only if you have installed Heroku CLI & Git.
3. Creating a Procfile
A Procfile is needed by Heroku. It does not have any extension like .js or .txt, but only a name ‘Procfile’. So in your Project folder, create a new file and name it as Procfile. Inside this file, write
worker: node bot.js
and save. That’s all we need to do with Procfile.
This Procfile tells heroku to run node bot.js command just like we did on your command line. The ‘worker’ is a type of dyno in heroku which I better not talk about right now.
4. Creating a New App
On your terminal, type
heroku login. Give your email and password and log in. Now, to create a new app type the following command
heroku create <your_app_name>
Your app name should be in lowercase. That’s it! We have a heroku app now. All we need to do is upload our code to our Heroku app that we created just now. On your browser go to www.heroku.com and login. You’ll see a list of all your apps. Click on the app you created just now. Then go to the ‘Deploy’ tab section. If you scroll down you’ll see this :
As I mentioned earlier, there are various methods to deploy your code on heroku. We are using the Heroku CLI because its cooler than using GUI 😀 .
5. Creating a Git Repository and Uploading our Twitter Bot code:
We first need to create a Git Repository of our bot and only then we can upload(push) it to Heroku. Open command line and navigate to your project file and type
This command initializes an empty git repository. Now we must add all the files in our project to the repository. To do so use the following two commands
git add . git commit -m "first commit"
Great! Now before we upload our code, we first need to tell where to actually upload the code. We can do that by using the command below
heroku remote:set -a <your app name>
Now the final step is to upload(push) the code. Use the following command
git push heroku master
6. Final Step – Turning on Worker Dyno
There’s one more things we need to do. Go to your heroku app page on your browser. On the ‘Resources’ tab you should see your dynos like this.
We need to turn on the worker dyno to start our bot. Click on the edit button and disable the web dyno and turn on worker dyno. This is where the Procfile comes into play! Without the Procfile, we wouldn’t have this option to turn on worker dyno.
That’s all! Now we have our Twitter bot running on a web server