HOW TO DEPLOY RUBY BOT LIVE ON HEROKU

First and foremost, I’d like to tell us what the term ‘Software Deployment’ is all about.

To a layman’s understanding, Software Deployment is simply put as all of the activities that make a software system available for use.

Software deployment is, therefore the procedure of making software ready for launch.

It is the process of delivering completed software to the client who ordered it or rolling out the software to consumers.

It is also the mechanism through which applications, modules, updates, and patches are delivered from developers to users.

Software deployment should only take place after thorough testing to ensure that all the flaws and bugs have been identified and fixed, but this is not our subject matter here.

Be aware that deployment is not the last stage of the software development life cycle. The reason for this is that it is almost impossible to catch all bugs and flaws during testing. Maintenance is the final step of the life cycle, and this is when the remaining fixes will be delivered. It is also when additional features or functions might be introduced, and updates to the software made.

Software Deployment Process

The software deployment processes entail:

- Preparation

- Testing

- Deployment

Preparation

In the preparation stage, developers ought to gather all of the code that will be deployed along with any other libraries, configuration files, or resources needed for the application to function. Together, these items can be packaged as a single software release. Verifying that the host server is correctly configured and running smoothly should also be considered here.

Testing

Before an update is pushed to the live environment, it should be deployed to a test server where it will be subjected to a pre-configured set of automated tests. Developers should review the results and correct any bugs or errors before deploying the update to the live environment.

Deployment

Once an application or an update has been fully tested, it can be deployed to the live environment. Developers may run a set of scripts to update relevant databases before changes can go live. The final step is to check for bugs or errors that could occur on the live server to ensure the best possible customer experience for users.

STEPS TO DEPLOYING RUBY BOT LIVE ON HEROKU

NB: Before we let the cat out of the bag, please take note of the following:

I’ll be using my Telegram Bot (built with Ruby) as a case study :)

Ensure your bot starts and runs locally.

The steps below could also work for other Ruby bots (and not Telegram bot only) as well.

STEP I:

Create a file named Procfile on the root directory of the project and add the following codes as follows:

In Procfile file;

web: ruby my_app.rb -p $PORT

worker: bundle exec ruby bin/main.rb

Within the project root directory, create another file called my_app.rb and add the following codes as follows:

STEP II:

In my_app.rb file;

require 'sinatra'

get '/' do

redirect 'http://bot_url_from_BotFather', 303

end

NB: http://bot_url_from_BotFather is your Telegram bot url from Botfather. In case you’ve forgotten, you can obtain your Telegram bot url from https://telegram.me/BotFather.

STEP III:

Go to Gemfile and add the snippets below:

In Gemfile, add gem sinatra;

gem 'sinatra'

STEP IV:

Then run bundle install

This command installs all dependencies.

Now, let’s pause and take note of the following snippets:

web: ruby my_app.rb -p $PORT checks whether the app has been sent to the web. If the app is sent to the web, then it means the app has been deployed to Heroku.

worker: bundle exec ruby bin/main.rb checks whether the bot is sent to the worker. If the bot is sent worker, then it means the bot has been started automatically.

Remember to replace ruby bin/main.rb with whatever command that starts your bot locally. ruby bin/main.rb does the trick on my case.

redirect 'http://bot_url_from_BotFather', 303 => this line of course redirects the bot to Telegram.

STEP V:

Now, let’s create an app on Heroku…

Run heroku create <name_of_your_bot> from your terminal.

NB: <name_of_your_bot> is actually optional. It’s required if you want your bot to have a unique url on Heroku since Heroku randomly assigns a weird url. Hahaha.

<name_of_your_bot> could be anything but ensure the words are separated with a hyphen (-). An example is comic-telegram-bot.

STEP VI:

Then run git push heroku master to push your bot to Heroku.

Run git push heroku master:<current_branch> instead if you’re currently not on the master branch.

<current_branch> is the name of your current branch.

STEP VII:

Once you’ve pushed successfully, run heroku ps to view your app's dyno settings i.e to check if the app is sent to the web and if the bot is sent to the worker.

Remember: If the app is sent to the web, that means the app has been deployed to Heroku and if the bot sent the worker, then it means the bot has been started automatically. If everything works out fine, then both web and worker should be equal to 1.

If the web isn’t 1 and the worker isn’t also 1, run heroku ps:scale web=1 worker=1 to fix it.

STEP VIII:

Remember to go to settings on your Heroku dashboard and add the ENV variable if there’s any.

For instance, in my case, I have TOKEN = ‘my_telegram_API_key’ in dot.env file. So I’d want to login into Heroku. Then I’d click on the app name and go to settings on my dashboard. Then I’d click on Reveal Config Vars and of course, add TOKEN as the KEY and my_telegram_API_key as the VALUE. Then click on Add.

STEP IX:

Once the ENV variable is added successfully, go ahead and click on Open app. This should redirect you to Telegram, ensure you click Open when it’s prompted.

Now, you can interact with your bot live and have fun without having to start it locally.

NB: You do not have to add ENV variable if your API key is not encrypted. In other words, it’s needless trying to add ENV variable in Heroku if your API key is hardcoded.

Take note of this also: The bot goes to sleep after some time, like 1 or 2 hours or so. To avert this issue, login into heroku and click on Open app. This way, the bot would be live again.

There’s actually a way to go around this, I’ll definitely upadate this article as soon as I found a solution.

If you wish you can check out my Telegram bot here and the source code is here.

For questions, suggestions, and contributions, reach out to me on…

LinkedIn Twitter GitHub

Email: jamezjaz@gmail.com

Full-Stack Developer — Ruby | Ruby on Rails | JavaScript | React & Redux