本文介绍了为什么我无法在使用 Flask (Python) 编写的 Telegram bot 上收到任何 POST 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想使用 getUpdates 方法从 Telegram 检索更新,而是使用网络钩子.

I don't want to use getUpdates method to retrieve updates from Telegram, but a webhook instead.

来自 getWebhookInfo 的错误是:

has_custom_certificate: false,
pending_update_count: 20,
last_error_date: 1591888018,
last_error_message: "SSL error {error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed}"

我的代码是:

from flask import Flask
from flask import request
from flask import Response

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def bot():
    if request.method == 'POST':
        return Response('Ok', status=200)
    else:
        return f'--- GET request ----'

if __name__ == "__main__":
    app.run(host='0.0.0.0', port='8443', debug=True, ssl_context=('./contract.crt', '.private.key'))

当我点击 https://www.mydomain.ext:8443/ 时,我可以看到当我在我的电报机器人聊天中写一些东西时,GET 请求到来但不 POST 请求这也是我为电报设置网络钩子的方式,如下所示:

When I hit https://www.mydomain.ext:8443/ I can see GET requests coming but not POST ones when I write something on my telegram-bot chatAlso that's how I set a webhook for telegram as follow:

https://api.telegram.org/botNUMBER:TELEGRAM_KEY/setWebhook?url=https://www.mydomain.ext:8443

结果:

{
  ok: true,
  result: true,
  description: "Webhook was set"
}

有什么建议或我做错了什么吗?

Any suggestion or something wrong I've done?

https://core.telegram.org/bots/api#setwebhook

我想知道问题是否是因为我使用 0.0.0.0 而引起的,原因是如果我使用 127.0.0.0 url/www.无法访问 mydomain.ext

I'm wondering if the problem it's caused because I'm using 0.0.0.0, the reason it's that if I use 127.0.0.0 the url/www.mydomain.ext cannot be reached

更新

ca_certitificate = {'certificate': open('./folder/ca.ca-bundle', 'rb')}
r = requests.post(url, files=ca_certitificate)
print(r.text)

那个印刷品给了我:

{
  "ok": false,
  "error_code": 400,
  "description": "Bad Request: bad webhook: Failed to set custom certificate file"
 }

推荐答案

我有过类似的情况.我正在本地主机上开发机器人(但没有 SSL)并通过 ngrok 将其隧道传输到网络.一开始一切都很好,但是一旦我发现没有 POST 请求即将到来.原来隧道的时间已经过期.我笑了,重新开始隧道.但是请求没有来.原来,我忘记更改 webhook 的地址(它切换每个 ngrok 会话).不要重复我的错误.

I had similar case. I was developing bot on localhost (yet without SSL) and tunneled it to web through ngrok. In beginning all was OK, but once I found no POST-requests are coming. It turned out time of tunneling expired. I laughed and restarted tunneling. But requests weren't coming. It turned out, I forgot to change address of webhook (it switches every ngrok session). Don't repeat my errors.

这篇关于为什么我无法在使用 Flask (Python) 编写的 Telegram bot 上收到任何 POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:11