1

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()`
3
  • Looks ok, do you get any error at startup? Double check your 'token' (must match the one provided by the BotFather. maybe post the code with your handlers Commented May 22, 2020 at 16:54
  • If I replace the code above with "updater.start_polling()" and run it locally it, it works. So pretty sure the token is correct. Should the token above be a string? Commented May 23, 2020 at 0:20
  • The token is a pretty long string, you get this via the BotFather. Commented May 23, 2020 at 7:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I tried it, but I am getting an error from heroku logs when deploying (Note that the token name is not the actual token in the following error, I have changed it and am using a random one to illustrate: url_path=851239789:AAEL_0ccRKimOVinAGewqdda13hgbrHEGA1a) ^ SyntaxError: invalid syntax
Hmm, seems to be a python-related issue if it says "SyntaxError", is it the complete log you pasted? can you provide full log with stack trace etc? Are you using same format method when injecting HEROKU_APP_NAME, TOKEN to Heroku URL? Have a look at this post: stackoverflow.com/questions/24237111/…
I tried that link and got really weird results. When i deleted that url line, the logs threw back the same error even though the line isnt there. This is how i did the deply: 1) heroku login 2) heroku create encouragements 3) git push heroku master 4) heroku ps:scale web=1. To obtain the logs, i used: heroku logs --tail. Also, is there a way to attach a picture? so i can show the logs
well, you can just paste them or attach GitHub gist or something :) @Photon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.