Develop and Deploy your Telegram Bot
Preparation
- Python 3(for develop)
- pipenv(for dependency management)–Optional
- ngrok(for testing)
- Heroku(for deploying)
- BotFather (to get your bot)
Development
1 | $ pipenv install --three python-telegram-bot flask gunicorn requests |
Parameter explaination:
–three use python3
python-telegram-bot Telegram Bot API wrapper.
flask Web framework. Using for building webhook API.
gunicorn Python WSGI HTTP server for UNIX. Using for deploying web server.
requests HTTP client library.
After installation, you should have this two file in your project directory:
1 | Project Directory |
for your bot token:
1 | $ touch config.ini |
config.ini
1 | [TELEGRAM] |
create your bot file.
1 | $ touch main.py |
1 | import configparser |
Start hosting your bot
1 | $ pipenv run python3 main.py |
Testing Tool
1 | $ ngrok http 5000 |
{token} =
https://api.telegram.org/bot{token}/setWebhook?url={webhook_url}
1 | { |
Now your bot should be online.
Deploy
Write a file Procfile
1 | web: gunicorn main:app --log-file - |
Your project should look like this:
1 | Project Directory |
Initialize git repository
1 | $ git init |
create new branch and switch to that branch
1 | $ git checkout -b production |
add repo folders into git
1 | $ git add . |
commit
1 | $ git commit -m "Deploying to Heroku" |
Log in Heroku account
1 | $ heroku login |
Add remote Heroku repository
1 | $ heroku git:remote -a {your_heroku_app_name} |
Push to Heroku repository
1 | $ git push heroku HEAD:master |