DEV Community

Dominic Koech
Dominic Koech

Posted on

Deploying Python Telegram Bot On Heroku Without Using Flask

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:-

  1. Heroku account.You can create one at https://signup.heroku.com/
  2. 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).

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.

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()

Enter fullscreen mode Exit fullscreen mode

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

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
You'll get your URL here:-

Image description

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:-

import os

TOKEN = os.environ.get('TOKEN')
PORT = int(os.environ.get('PORT',88))
Enter fullscreen mode Exit fullscreen mode

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:-

    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()
Enter fullscreen mode Exit fullscreen mode

Create a profile and name it 'Procfile' without using any extensions and add the following to it:

web: python3 main.py $PORT
Enter fullscreen mode Exit fullscreen mode

'main.py' is the python file that starts the bot.

Create a requirements.txt file by typing the following commands in your virtual environment:

pip freeze > requirements.txt 
Enter fullscreen mode Exit fullscreen mode

The code for your bot should be looking like this now:

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()
Enter fullscreen mode Exit fullscreen mode

Deploying

Open the terminal, move to where the project folder is and type the following commands:

$ Heroku login
$ git init
$ heroku git:remote -a appname
$ git add.
$ git commit -am "make it better"
$ git push Heroku master
Enter fullscreen mode Exit fullscreen mode

If the deployment is successful you'll see the following message.

Image description
Head to settings then Config Vars. Add 'TOKEN' as the key and then put your API key as the value.

Image description
You'll put the API key here:-

Image description

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

Top comments (0)