Home > other >  problems with webhook telegrambot and flask in heroku
problems with webhook telegrambot and flask in heroku

Time:10-29

I am wanting to create a bot for telegram and a web application from which I can edit and store the commands for said bot. The problem arises when I deploy in heroku, since I cannot run the bot and the web application at the same time. I think I am having problems with the webhook. This is my code.

app.py

import os
import sys

import telegram
from telegram.ext import Updater, updater
from telegram.ext.commandhandler import CommandHandler
from resources.commands import *
from flask import Flask, render_template, request

token = "Telegram bot token"
heroku_app_name = "Heroku app name"

bot = telegram.Bot(token= token)
app=Flask(__name__)

updater = Updater(bot.token, use_context= True)

@app.route('/home')
def home():
    return render_template('home.html')    

@app.route('/')
def webhook():
    updater.start_webhook(listen="0.0.0.0",
                    port=int(os.environ.get('PORT', 5000)),
                    url_path=token,
                    webhook_url="https://{}.herokuapp.com/{}".format(os.environ.get("HEROKU_APP_NAME"), token))
    return "!", 200

if __name__ == '__main__':
    webhook()
    app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))

dp = updater.dispatcher

dp.add_handler(CommandHandler("hello", hellothere))    

def hellothere(update, context):
    update.message.reply_text("Hello There!!")

home.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <link rel="icon" href="data:,">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <title>Bot</title>
    </head>
    <body>
        <h1>Hello world!!</h1>
    </body>
</html>

Procfile

web: python app.py

Log

2021-10-27T16:49:56.962690 00:00 heroku[web.1]: Starting process with command `python app.py`

2021-10-27T16:49:59.445699 00:00 app[web.1]: 2021-10-27 16:49:59,445 - apscheduler.scheduler - INFO - Scheduler started,

2021-10-27T16:49:59.800346 00:00 app[web.1]:  * Serving Flask app 'app' (lazy loading)

2021-10-27T16:49:59.800372 00:00 app[web.1]:  * Environment: production

2021-10-27T16:49:59.800373 00:00 app[web.1]:    WARNING: This is a development server. Do not use it in a production deployment.

2021-10-27T16:49:59.800387 00:00 app[web.1]:    Use a production WSGI server instead.

2021-10-27T16:49:59.800399 00:00 app[web.1]:  * Debug mode: on

2021-10-27T16:49:59.817520 00:00 app[web.1]: Traceback (most recent call last):

2021-10-27T16:49:59.817521 00:00 app[web.1]:   File "/app/app.py", line 401, in <module>

2021-10-27T16:49:59.817724 00:00 app[web.1]:     app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))

2021-10-27T16:49:59.817727 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 920, in run

2021-10-27T16:49:59.817951 00:00 app[web.1]:     run_simple(t.cast(str, host), port, self, **options)

2021-10-27T16:49:59.817960 00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/werkzeug/serving.py", line 984, in run_simple

2021-10-27T16:49:59.818196 00:00 app[web.1]:     s.bind(server_address)

2021-10-27T16:49:59.818260 00:00 app[web.1]: OSError: [Errno 98] Address already in use

2021-10-27T16:50:00.329639 00:00 heroku[web.1]: State changed from starting to up

2021-10-27T16:50:11.000000 00:00 app[api]: Build succeeded

Any idea?

CodePudding user response:

comment out the line app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))

you will not have a /home handler but the webhook will probably work listening on the Heroku supplied port

CodePudding user response:

You cannot have 2 processes on the same port (see error) and you cannot use 2 different ports on the same Heroku Dyno.

You could use the Flask APP as REST frontend and, instead of the Telegram Webhook, you can use the polling approach (which does not need to bind a port)

  • Related