Forums

HTTP/1.1" 500 setting a flask webhook to retrieve update from api.telegram.org

Hello, I am experiencing the above issue in setting up a flask webhook for a telegram bot. As it is, my code works quite well locally. I read every post on forum about not et update from flask webhook and I did not find a solution. It is not matter of url I think since locally works great. I know as far as free account is concern, I need to use a proxy in order to connect to a white list url, which I did. I do not get any error in logs which I saw is pretty common in this kind of situation with a flask webhook. Could you help me?

Thank you for your time.

Best, Baam

Your post is unclear about what is returning the 500 error. Is it your web app? Another site that your web app is trying to access? Something else?

Hello Glenn, Sorry about that. I will try to be more specific. Yes, it is a web app built with flask framework. With flask I have created a route for a webhook which allows only POST requests which will be processed after. The route is set as https://<username>.pythonanywhere.com/<random_50char_string>

@app.route('/'+TOKEN, methods=['POST'])
    def webhook():

        ### process telegram request
        json_string = request.get_data().decode('utf-8')

        update = Update.de_json(json.loads(json_string), bot)
        dp.process_update(update)
        logger.debug(f'\nWEBHOOK POST MESSAGE\n{json_string}\n###########\n')
        return 'ok', 200

In my web app I set

If __name__=="__main__":
        main()

Now, the access server logs return the above result every time I send a command to my bot from a telegram client. Any idea about why?

Have you checked the error logs of your web app?

Hello pafk, as I said, no error logs showed in web app error logs.

Are you sure you're looking in the right place? The error logs for your site are accessible from the "Web" page inside PythonAnywhere, and the most recent error will be at the bottom of the file. Each line in there has a timestamp at the start of it, and when I look at the equivalent logs from our admin interface I can see tracebacks corresponding to each access to your site that returned a 500 error code according to the access logs.

Yes, you got the point, but why? I think I have set up everything correctly now. The bot works perfectly on my local machine with ngrok. In here I correctly set up the proxy as documentation and removed app.run() from the run_app.py file. Since you are an admin, feel free also to take a look to my code because I do not know where the issue is. I have tried also to force https with flask_sslfy but nothing. Now I get 403 error which I saw could be fixed setting up the proxy_url, which I did, but it's stil not working.

Your web app is not returning a 500 error because of anything about telegram and 403s. It is failing because you are not importing something correctly. Read the exceptions in your error log and fix the errors that you actually have.

Hello glenn, thank you for the help. I did not have access to these logs any more. However, I think that I messed up with the package structure of my application. I will review it. In meanwhile, are you able to add photon.komoot.io to the whitelist? Many thanks again.

SOLVED:

if __name__=="__main__":
   main()

is incorrect since the run_app.py file which contains the flask app will not be named as "__main__" but with its own name (run_app). You can easily verify it with

logger.debug(f'APP NAME:\n{__name__}')
if __name__=="__main__":
       main()

and check logs. Hope to be helpful to someone!

Glad to hear that you made it!