Scaling Heroku Application using Python and Telegram Bots

Raghuvansh Tahlan
Analytics Vidhya
Published in
3 min readAug 23, 2021

Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. Their platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market. Heroku provides free as well as paid tiers. It started in 2007 with only Ruby as a supported language now; it supports many languages such as Ruby, Python, Java, Scala, PHP etc. Based on my personal experience, when I used it to deploy Python based Flask Web Application, the learning curve for Heroku is steep. It requires some knowledge of Git too. But when you have deployed your first application, then everything is at ease.

Dynos

The containers used at Heroku are called “dynos.”

Heroku is a scalable platform where dynos provide scalability. Dynos are isolated, virtualized Linux containers that are designed to execute code based on a user-specified command. Dynos are the building blocks that power any Heroku app, from simple to sophisticated.

In brief, dynos run the app; if the number of dynos running is 0, the application is inactive. Else if the number of dynos is greater than 0, then the application is active.

By the end of this guide, I intend to lay the foundations of how we can use Python to control Heroku Application using Heroku API and relay updates on Telegram. Using these foundational skills, one can build a custom Telegram bot that relays updates and takes commands from the users.

Generating Heroku API

Navigate to “Account Settings” and click on “Reveal” to find your API Key. Copy it someplace safe.

Notification using Telegram Bot

To send messages using Telegram Bot “Bot_token” and “Chat_id” is necessary. Follow along the guide to generate one for your account.

Python code

Initiation of Python Class

Firstly, we import all the necessary libraries like “requests” to make API calls, “base64” to encode the Heroku API, etc.

Then we are initializing a python class with variables like Heroku app name, Heroku API key, Telegram bot Token and Telegram chat id.

The “generate_basekey” function will encode the API and create a header. This header will be used to verify our requests at receiving end.

The function “Telegram_Notify” is a helper function. Other functions will use this function to send updates via Telegram bots.

The two functions “get_current_dyno_quantity” and “get_current_dyno_quantity_notify” are similar in nature with one difference, i.e. They both return the current number of dynos, but “get_current_dyno_quantity_notify” will send a message to Telegram chat, but the other will not.

The “scale” function does the actual work of scaling, i.e. increasing and decreasing the number of numbers for the application.

If the request is successful, it will then notify using Telegram Message.

The “restart” function is a convenient one. It is most useful when some updates are made to the application or we are facing issues.

--

--

Raghuvansh Tahlan
Analytics Vidhya

Passionate about Data Science. Stock Market and Sports Analytics is what keeps me going. Writer at Analytics Vidhya Publication. https://github.com/rvt123