skip to Main Content

I am trying to deploy and run a telegram bot on heroku.
I am using python 3 and the library python-telegram-bot.

My .py class called test.py and the git project called telegram_bot.
I also created a heroku pipeline (includes a project called telegram-bot-1) and connect that to my git. (so every time i puch new commits, heroku start building the project)

these are all my codes in git project (includes test.py, Dockerfile, and herouku.yml):

import os
import sys

from telegram.ext import Updater, CommandHandler

TOKEN = "515148657:dvGVfrSVU78SfvdEtvfZf25EvefvEftnU-8"

def run(updater):
    PORT = int(os.environ.get("PORT", "8443"))
    HEROKU_APP_NAME = "telegram_bot" 
    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(bot, update):
    user = update.message.from_user
    update.message.reply_text("Hello dear {} :).format(user['first_name']))


def get_handler(bot, update):
    stock_name = update.effective_message["text"]
    update.message.reply_text("you said: {}".format(stock_name))


if __name__ == '__main__':
    updater = Updater(TOKEN)
    updater.dispatcher.add_handler(CommandHandler("start", start_handler))
    updater.dispatcher.add_handler(CommandHandler("get", get_handler))

    run(updater)
FROM python:3.7

RUN pip install python-telegram-bot

RUN mkdir /app
ADD . /app
WORKDIR /app

CMD python /app/test.py
build:
  docker:
    web: Dockerfile

all my files are in one folder.

but the logs after auto building on heroku are:

2019-06-01T14:32:57.000000+00:00 app[api]: Build started by user [email protected]
2019-06-01T14:33:55.792474+00:00 app[api]: Deploy 50b099c1 by user [email protected]
2019-06-01T14:33:55.792474+00:00 app[api]: Release v40 created by user [email protected]
2019-06-01T14:33:55.000000+00:00 app[api]: Build succeeded

and the code doesn’t run. so the bot doesn’t work.

what should i do to make it correct?

thanks.

2

Answers


  1. you missed run config in heroku.yml file.

    After provided build phase

    build:
      docker:
        web: Dockerfile
    

    you have to add something like

    run:
      web: python /app/test.py
    
    Login or Signup to reply.
  2. If someone prefers webhooks, which are really easy to set-up, instead of containers to host their bot on Heroku, check it out here:

    https://github.com/python-telegram-bot/python-telegram-bot/wiki/Webhooks#heroku

    They are also more efficient than polling and also hassle-free.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search