A word on Azure
Since I have an Azure account and MSDN subscription my first choice for hosting the bot was Azure. However, I learned something important.
After wasting too much time looking into both web apps and Azure Cloud Services, installing Azure Power Shell, learning to use it, setting up worker roles, deploying, etc. and still not getting the bot to work (unless accessed through a URL) I decided to move on and find something that even someone unfamiliar with can quickly grasp in minutes.
Starting with Heroku
Heroku is a platform that you can use for free for experimenting if you don’t require too many resources. Of course, there are paid plans, depending on your needs. However, for the simple bot I was building there would be no incurring costs.
Pricing is based on dyno hours. Dynos are lightweight Linux containers in which an application runs. A dyno can belong to one of the three possible configurations: web (that receive HTTP traffic from routers), worker (typically background jobs, queueing systems, and timed jobs), and one-off (temporary dynos that can run detached, or with their input/output attached to your local terminal, typically used for administrative tasks, such as database migrations and console sessions). You can get up to 1000 dyno hours per month for free, which is enough to run an application with no costs. Should you need to host more, then you’d have to pay for it.
After creating the account, you should download and install the Heroku CLI (previously known as Heroku Toolbelt). This allows you to create and manage Heroku apps from the command line on Window, MacOS and Debian/Ubuntu.
Creating a Heroku app
After creating an account and signing in you can create applications from the dashboard. You must specify a unique name and a data center where it should be hosted.
Heroku has its own git to store your project sources. You can find instructions under the Deploy page for how to create the project locally and commit it to Heroku’s git repository.
Defining a Procfile
Apart from the bot source files that we created in the previous article (package.json, bot.js, and others that might be necessary), we need to declare a Procfile in the root directory of the app. This is a text file that defines what command should be executed to start the application. The content of this file should be the following (assuming that the entry point of the bot is called bot.js).
worker: node bot.js
This is necessary because the Twitter bot is not a web app, but a worker app that runs on the server and does something periodically in the background, not based on HTTP requests.
After uploading the source files including the Procfile to the heroku git repository, go to the application and under the Resources tab, stop the web dyno and start the worker dyno.
Note: a web dyno is created by default for every application.
And that’s it, the Node app now runs successfuly on the platform in the background.