Photon
Photon

Reputation: 55

Deploying Python Telegram Bot on Heroku

I am trying to deploy my python telegram bot on heroku. Everything builds fine and heroku said its deployed successfully. However, when I tried the bot in telegram, it does not work. I have attached the deployment code below. Can someone help please? Thanks. My Procfile contains this: web: python3 encouragements.py

`import os
TOKEN = "Token"
PORT = int(os.environ.get('PORT', '5000'))
updater = Updater("Token")

updater.start_webhook(listen="0.0.0.0",
                    port=PORT,
                    url_path="Token")
updater.bot.setWebhook("https://xxx.herokuapp.com/" + "Token")
updater.idle()`

Upvotes: 1

Views: 1504

Answers (1)

Henryk
Henryk

Reputation: 11

I found this article helpful when deploying a Telegram bot on Heroku: Creating Telegram Bot and Deploying it to Heroku (make sure the code is up to date by comparing it to the docs guide to Version 12.0)

Based on the article provided above, I've tried to reproduce your case with the following setup:

encouragements.py:

from telegram.ext import Updater, CommandHandler, CallbackContext
from telegram import Update
import os

TOKEN = ""
HEROKU_APP_NAME=""

# def run(updater):
    # updater.start_polling()
def run(updater):
    PORT = int(os.environ.get("PORT", "8443"))
    updater.start_webhook(listen="0.0.0.0",
                          port=PORT,
                          url_path=TOKEN)
    updater.bot.set_webhook("https://{}.herokuapp.com/{}".format(HEROKU_APP_NAME, TOKEN))

def start_handler(update: Update, context: CallbackContext):
    update.message.reply_text("Hello from Python!\nPress /random to get random number")


if __name__ == '__main__':
    updater = Updater(TOKEN, use_context=True)
    updater.dispatcher.add_handler(CommandHandler("start", start_handler))
    run(updater)

Pipfile:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
python-telegram-bot = "*"

[requires]
python_version = "3.7"

Procfile:

web: python encouragements.py

And telegram bot is indeed responding to /start message. I followed this article when deploying to Heroku: https://devcenter.heroku.com/articles/getting-started-with-nodejs

I would also recommend to check what is happening on Heroku's side:

heroku logs -t --app <your-heroku-app-name>

Logs should tell you if your token is in fact correct, if your dependencies where properly loaded, and if your code does not produce any error during runtime.

Let me know if it worked :)

Upvotes: 1

Related Questions