Deploying Python Telegram Bot On Heroku Without Using Flask
Tags: python, telegrambot, heroku, beginners
Published: 2022-08-22 06:01:57

You've already built your telegram bot and it's running perfectly in your local machine, now you want to deploy and run it on Heroku without having to use the flask framework. In this article, I'll give you a step-to-step guide on how to do exactly that.
### Prerequisites
You should have the following:-
<ol>
<li>Heroku account.You can create one at https://signup.heroku.com/ </li>
<li>A telegram bot that is working in your local machine. (If you don't have one, no worries, I'll guide you on how to make a simple one).</li>
</ol>

### Let's get started...
Open where you have the code that runs your telegram bot, in case you don't have one already you can copy the code below for a simple bot that I've created that echoes a text sent to it.

```python
from telegram.ext import *
from telegram import *

TOKEN = 'Your API Key'


def echo_message(update, context):
    update.message.reply_text(f'{update.message.text}')


def main():
    updater = Updater(TOKEN, use_context=True)
    disp = updater.dispatcher
    disp.add_handler(MessageHandler(Filters.regex(r'.*'), echo_message))
    updater.start_polling()
    updater.idle()


if __name__ == '__main__':
    main()

```
###Creating an app on Heroku
Log into Heroku and head over to dashboard >> new >> create a new app. Enter your app name and select your region then click create. For me, I'll use 'testingnotes' as my app name and USA as my region.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufymuw4e5c5l5mo0mycs.png)

Select settings then scroll down to domains and copy the URL for your app that you'll be given, you'll use this later on.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72cb9hz9aga3sxb9d1au.png)
You'll get your URL here:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6m9iunizacwxhxv68vx.png)

###Modifying your telegram bot code
To deploy the telegram bot on Heroku, make the following changes in your python code.

At the top of the code add the following lines:-
```python
import os

TOKEN = os.environ.get('TOKEN')
PORT = int(os.environ.get('PORT',88))
```
The changes made enable you to get the API key and PORT (port which the bot will be listening to) from the environment variables.

At the end of the code remove `updater.start_polling()` and add the following lines:-
```python
    updater.start_webhook(listen="0.0.0.0",
                          port=int(PORT),
                          url_path=TOKEN,
                          webhook_url='app URL that you copied from Heroku + TOKEN)
    updater.idle()
```
Create a profile and name it 'Procfile' without using any extensions and add the following to it: 
```python
web: python3 main.py $PORT
```
'main.py' is the python file that starts the bot.

Create a requirements.txt file by typing the following commands in your virtual environment: 
```python
pip freeze > requirements.txt 
```
The code for your bot should be looking like this now:
```python
from telegram.ext import *
from telegram import *
import os

TOKEN = os.environ.get('TOKEN')

PORT = int(os.environ.get('PORT', 88))

def echo_message(update, context):
    update.message.reply_text(f'{update.message.text}')


def main():
    updater = Updater(TOKEN, use_context=True)
    disp = updater.dispatcher
    disp.add_handler(MessageHandler(Filters.regex(r'.*'), echo_message))
    updater.start_webhook(listen="0.0.0.0",
                          port=int(PORT),
                          url_path=TOKEN,
                          webhook_url='https://testingnotes.herokuapp.com/' + TOKEN)

    updater.idle()


if __name__ == '__main__':
    main()
```

###Deploying
Open the terminal, move to where the project folder is and type the following commands:
```bash
$ Heroku login
$ git init
$ heroku git:remote -a appname
$ git add.
$ git commit -am "make it better"
$ git push Heroku master
```
If the deployment is successful you'll see the following message.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3omm4zknv526p3n08esb.png)
Head to settings then Config Vars. Add 'TOKEN' as the key and then put your API key as the value.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyldb4nni76xk7wyenur.png)
You'll put the API key here:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pasfhj1t3fxx1mg6mvg.png)

Now head back to telegram and interact with your bot. It should be working.