Build and deploy a simple YouTube downloader bot in Heroku
What is Heroku?
Heroku is a cloud platform as a service (PaaS) supporting several programming languages. One of the first cloud platforms, Heroku has been in development since June 2007, when it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go.
This article is an extension of “Build and Deploy your first Telegram Bot using Python”. If you haven’t checked it out, please visit:
We have seen how to connect and handle messages using Telegram Bot in previous article. Now, before deploying let us extend the functionality of the previous bot. Let’s make a YouTube downloader, if you need to run/check on your local machine before deploying install this new package and add the below function:
$ pip install youtube-dl
- Line 1–2 initializes YouTube parser from youtube-dl to provide metadata about the link.
- Line 9–20 fetches the downloadable link (is in args variable) and searches for the video’s metadata.
- Line 22–29 handles sending reply to the user with the parsed downloadable link with its quality.
Visit: https://github.com/krsh-37/TelegramBot/tree/Heroku for source code/directory setup.
Directory Setup
Considering everything works fine, now let us go the directory setup needed for deploying in Heroku:
.
├── Procfile
├── bot.py
├── requirements.txt
└── runtime.txt
- Heroku apps include a Procfile that specifies the commands that are executed by the app on startup. Each dyno in your app belongs to one of the declared process types, and it executes the startup command associated with that process type.
- Runtime specifies the version of our python that needs to be run on Heroku.
- Requirements contains the necessary external libraries/packages that are needed/used to run our script (bot.py).
First parameter initializes the type of Dyno, followed by the application name, then the file that we want to execute. Ensure you save `Profile` in the same naming format and do remove file extension if any.
Runtime.txt specifies the version of Program (Here. python 3.6.1).
Example showing requirements file, do not include any in-build package like `os` or `time` package to avoid Build Errors. Visit this repository for reference.
For more information about heroku visit: https://devcenter.heroku.com/categories/reference
Deploying into Heroku
Turn your complete directory setup into a GitHub repository. And signup for a Heroku account. Since it is the first time here we are going to deploy via browser. But using Heroku CLI/Toolbelt is much appreciated for future use.
Step 1: Click on ‘create new app’.
Step 2: In the next provided screen type in your desired app name (this should be unique, later you can access it with appname.herokuapp.com) and create an app.
Step 3: In the dashboard, you will be prompted with 3 options to select the deployment method, we are going with ‘GitHub’ method. Click and authorize your github account to connect with Heroku.
Step 4: After successful integration with GitHub, type in the repository name in the ‘repo name’ box to search and connect.
Step 5: Go to Settings tab, in ‘config vars’ click on ‘Reveal Config Vals’. And type in Variable_Name for secret Tokens/keys as key and API_KEYS as values.
Step 6: In ‘Buildpacks’, select the language in which the script is written (here ‘Python’). Select and save the changes.
Step 7: Now go back to the ‘Deploy’ tab in Dashboard and enable automatic deployment from ‘master’ branch if you want continuous integration from Github repository.
Step 8: Since it is the first time, click on ‘Deploy Branch’ on Manual deploy tab. And head to the activity tab for build progress, if everything is fine, you will be presented with a successful build.
Always check for errors in build logs and solve accordingly. Heroku automatically builds and deploys when any further changes are made in GitHub repository.
Step 9: Go to ‘Resources’ tab, now you can see the `bot` dyno, click on edit option and turn on your free dyno. If you didn’t see Dynos under Resources tab hit hard refresh (ctrl+F5) or check for any error in Build Logs.
Now your bot has been deployed successfully into Heroku. A monthly 555 hours of service is provided for the Free Dyno plan. To upgrade see pricing plan. Always check Build Logs. For better management experience usage of Heroku CLI/toolbelt is advised.
References:
[1] About Heroku
[2] PyTelegramBotApi
[3] Youtube-dl
If you’re passionate about DS/ML, let’s collaborate/learn!
Connect with me on LinkedIn.
Learn, Share & Contribute. Happy Learning !!