To begin with, the idea for creating a telegram python bot came up during Safety & Security Lab Hackathon. Our team created a sample bot to educate public on computer security and protecting yourself online. The bot was running from our personal notebook and utilized telebot library. However, we experienced difficulties when we put on Heroku. As a result, in this blog post we conduct telegram optimizing. We optimize the python telegram bot template from scratch so we could host heroku telegram bot.
First, we need to save python dependencies to host telegram python bot on Heroku. In this tutorial related to telegram optimizing, we use python virtual environment library virtualenv. We install the python library:
Next, we start the virtual environment and activate it:
To save the dependencies for server such as Heroku, we run the following python command:
In case we want to exit python virtual environment:
To create a bot we have to message @BotFather in telegram. The command that creates a bot is /newbot and you have to also name the bot. After that BotFather sends us the HTTP API TOKEN.
First, we install the key python telegram bot library which is python-telegram-bot:
Next, we create a simple python telegram bot that uses /start and /help commands as well as echos user’s message. The script also uses a web-hook for Heroku hosting:
Do not forget to edit the created TOKEN and APP_NAME that you obtain through Heroku.
Besides, we create a Procfile that contains a following line:
Our folder should contain three files: Procifle, the python script (in this case bot.py) and requirements.txt.
Finally, we deploy python telegram bot on Heroku: