Forums

Having Problem While Using webhook for running a Telegram Bot

Hello I provided a bot using the webhook feature and creating a flask_app.py. I ran the flask_app.py and no error message was appeared. but when I reload the flask_app.py, the bot doesn't work, Despite the fact that the bot is working in normal mode (no webhook) in vs code properly.

Can you give me some advice? I did not delete TOKEN and you can examine it.

Here is the flask_app.py I created:

from flask import Flask, request
import urllib3
import telepot
from telepot.loop import MessageLoop
import time

proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
    'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))

secret = "[REDACTED]"
bot = telepot.Bot('[REDACTED]')
bot.setWebhook("https://frdgolestan.pythonanywhere.com/{}".format(secret), max_connections=1)

app = Flask(__name__)

def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)

    if content_type == 'text':
        command = msg['text']
        if command == '/start':
            bot.sendMessage(chat_id, 'Hello.')

I appreciate your help in advance.

[edit by admin: formatting]

What for you mean by "the bot doesn't work"?

I mean I do not receive any message from the bot after starting with /start. As you see at the code, the bot must send 'Hello.' but it does not.

Are you receiving the webhook request in your web app? You should be able to see it in your access log.

Please I'm having the same issue, and yes I'm receiving the same the webhook request on my web app. Please how to resolve this

Could you give some more details? You say that you're receiving the webhook request, but the post above is related to a case where the webhooks were not arriving.

I am not receiving any response on the bot in telegram when I type Hello or Start. I do not know why this is not working. It was working before I started the webhook steps.

Is the request processed correctly? You can add more logging to your web app to see if the response is sent and if it contains what it should, etc.

I followed the steps here https://blog.pythonanywhere.com/148/ but it does not work, I've tried two times. Please how do I get this resolved??

Did you add any additional logging?

No I didn't because I'm using a free account now. Is this a problem?

Check your website's access logs to see if requests are coming in to your webhook URL. If they are, check whether there is anything in the website's error log.

Yes, when I checked the access log I see request came in. but no response back on telegram at all and I see some access errors like this

2025-01-07 19:27:33,641: If you're seeing an import error and don't know why, 2025-01-07 19:27:33,641: we have a dedicated help page to help you debug: 2025-01-07 19:27:33,641: https://help.pythonanywhere.com/pages/DebuggingImportError/ 2025-01-07 19:27:33,641: *********

Please help

Did you read the linked page?

Also there should be an error message below the lines you provided. What is the actual error?

Did you read the linked page?

Also there should be an error message below the lines you provided. What is the actual error?

Yes, this is the error message

2025-01-07 19:26:10,130: Error running WSGI application
2025-01-07 19:26:10,134: ModuleNotFoundError: No module named 'flask'
2025-01-07 19:26:10,134:   File "/var/www/lanre4450_pythonanywhere_com_wsgi.py", line 16, in <module>
2025-01-07 19:26:10,134:     from flask_app import app as application  # noqa
2025-01-07 19:26:10,135: 
2025-01-07 19:26:10,135:   File "/home/lanre4450/mysite/flask_app.py", line 1, in <module>
2025-01-07 19:26:10,135:     from flask import Flask, request

Pleas help

[edit by admin: formatting]

It looks like you've configured your website to use Python 3.6, which is a very old Python version. Try using Python 3.10 and see if that works any better.