带Webhook和nginx的坏网关Python-Telegram-bot

发布于 2025-02-08 17:56:00 字数 1607 浏览 4 评论 0原文

我正在尝试使用Python-Telegram-bot和Nginx设置Webhook。我面对一个问题,我的机器人不会从电报中收到消息。我还试图从邮递员那里进行获取/发布查询,并且总是会遇到“ 502 Bad Gateway”错误。我还启动了NetStat,以监视我的电报机器人连接的端口5000,但它总是空的。似乎Webhook根本没有启动。

我的nginx default.conf文件看起来如下:

upstream django {
server gunicorn:8000;
}

server { 
    listen 80; 
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri; 
} 

server {
    listen 443 ssl;
    ssl on;
    server_name example.com www.example.com; 
    ssl_certificate fullchain.pem;
    ssl_certificate_key privkey.pem;

    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;

    location /TELEGRAM_TOKEN {
        proxy_pass http://0.0.0.0:5000/TELEGRAM_TOKEN/;
    }

    location /static/ {            
        alias /static/;                   
    }  

    location / {                                
        proxy_pass http://example.com:8000; 
    } 
}

和我的电报客户端代码:

updater = Updater(api_token)

updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, custom_command))

jq = updater.job_queue
job_minute = jq.run_repeating(callback_minute, interval=5)

#updater.start_polling()
updater.start_webhook(listen="0.0.0.0", port=5000, url_path=api_token,
                      webhook_url=f'https://example.com/{api_token}')
updater.idle()

我也有django选项,但我从未见过有关如何使用django调整Webhook的任何教程或文档为了我的问题。

有人对解决我的问题有任何想法吗?

I'm trying to set up a webhook with python-telegram-bot and Nginx. I am faced with a problem, my bot doesn't get messages from telegram. I also tried to make GET/POST queries from the postman and I always get a "502 Bad Gateway" error. I also launched the netstat to monitor port 5000 where my telegram bot connects but it is always empty. It seems like webhook doesn't launch at all.

My Nginx default.conf file looks like the following:

upstream django {
server gunicorn:8000;
}

server { 
    listen 80; 
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri; 
} 

server {
    listen 443 ssl;
    ssl on;
    server_name example.com www.example.com; 
    ssl_certificate fullchain.pem;
    ssl_certificate_key privkey.pem;

    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;

    location /TELEGRAM_TOKEN {
        proxy_pass http://0.0.0.0:5000/TELEGRAM_TOKEN/;
    }

    location /static/ {            
        alias /static/;                   
    }  

    location / {                                
        proxy_pass http://example.com:8000; 
    } 
}

And my telegram client code:

updater = Updater(api_token)

updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, custom_command))

jq = updater.job_queue
job_minute = jq.run_repeating(callback_minute, interval=5)

#updater.start_polling()
updater.start_webhook(listen="0.0.0.0", port=5000, url_path=api_token,
                      webhook_url=f'https://example.com/{api_token}')
updater.idle()

I also have Django options for the Nginx server but I've never seen any tutorial or documentation on how to tune the webhook with Django and it can be the reason for my problems.

Have anyone any idea about solving my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2025-02-15 17:56:00

callMestag,您使用以下代码的建议帮助我解决了问题:

print(bot(api_token).get_webhook_info()))

我在nginx config file

first:ssl on of on nginx config file

它给了我一个错误的连接拒绝,我删除了

second

我将其更改

    location /TELEGRAM_TOKEN {
        proxy_pass http://0.0.0.0:5000/TELEGRAM_TOKEN/;
    }

为此

    location /TELEGRAM_TOKEN {
        proxy_pass http://example.com:5000/TELEGRAM_TOKEN/;
    }

,并且Webhook现在正在工作。

谢谢。

CallMeStag, your advice to use the following code helped me solve the issue:

print(Bot(api_token).get_webhook_info()))

I had 2 problems with the Nginx config file

First: ssl on

It gave me an error connection refused and I deleted it

Second

I changed this

    location /TELEGRAM_TOKEN {
        proxy_pass http://0.0.0.0:5000/TELEGRAM_TOKEN/;
    }

to this

    location /TELEGRAM_TOKEN {
        proxy_pass http://example.com:5000/TELEGRAM_TOKEN/;
    }

and the webhook is now working.

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文