Telegram 不断向服务器发送请求

Telegram constantly send requests to the server

我正在使用电报机器人。

我在 BotFather 中注册了我的机器人,在 Google Cloud 上建立了 "server",设置了 webHook。

我的机器人工作正常,当我向我的机器人发送一些命令时,它以预期的方式回复。我无法理解的是来自 'server'(google 云)的消息。

当我分析它时,我有一种感觉,电报不断向我的 API 发送请求,而不是我以前发送的每条消息,但其中一些。

这是日志:

2019-04-28 07:47:51 default[20190428t121301]  [2019-04-28 07:47:51,838] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:47:51 default[20190428t121301]  Traceback (most recent call last):    File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app      response = self.full_dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request      rv = self.handle_user_exception(e)    File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception      reraise(exc_type, exc_value, tb)    File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise      raise value    File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request      rv = self.dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request      return self.view_functions[rule.endpoint](**req.view_args)    File "/home/vmagent/app/main.py", line 96, in telegram_notification      chat_id = data['message']['chat']['id']  KeyError: 'message'
2019-04-28 07:47:53 default[20190428t121301]  {'update_id': ...., 'edited_message': {'message_id': 360, 'from': {'id': ..., 'is_bot': False, 'first_name': '...', 'language_code': 'ru'}, 'chat': {'id': ..., 'first_name': '...', 'type': 'private'}, 'date': 1556389741, 'edit_date': 1556389827, 'text': '.......'}}
2019-04-28 07:47:53 default[20190428t121301]  [2019-04-28 07:47:53,191] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:47:53 default[20190428t121301]  Traceback (most recent call last):    File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app      response = self.full_dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request      rv = self.handle_user_exception(e)    File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception      reraise(exc_type, exc_value, tb)    File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise      raise value    File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request      rv = self.dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request      return self.view_functions[rule.endpoint](**req.view_args)    File "/home/vmagent/app/main.py", line 96, in telegram_notification      chat_id = data['message']['chat']['id']  KeyError: 'message'
2019-04-28 07:47:54 default[20190428t121301]  {'update_id': ....., 'edited_message': {'message_id': 360, 'from': {'id': ..., 'is_bot': False, 'first_name': '....', 'language_code': 'ru'}, 'chat': {'id': ...., 'first_name': '....', 'type': 'private'}, 'date': 1556389741, 'edit_date': 1556389812, 'text': 'Сафмуллин Данил: 3400.0,\nРаевская Вероника: 4000.0'}}fa
2019-04-28 07:47:54 default[20190428t121301]  [2019-04-28 07:47:54,123] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:47:54 default[20190428t121301]  Traceback (most recent call last):    File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app      response = self.full_dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request      rv = self.handle_user_exception(e)    File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception      reraise(exc_type, exc_value, tb)    File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise      raise value    File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request      rv = self.dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request      return self.view_functions[rule.endpoint](**req.view_args)    File "/home/vmagent/app/main.py", line 96, in telegram_notification      chat_id = data['message']['chat']['id']  KeyError: 'message'
2019-04-28 07:48:00 default[20190428t121301]  {'update_id': 448395744, 'message': {'message_id': 490, 'from': {'id': ..., 'is_bot': False, 'first_name': '...', 'language_code': 'ru'}, 'chat': {'id': ..., 'first_name': '...', 'type': 'private'}, 'date': 1556430040, 'voice': {'duration': 1, 'mime_type': 'audio/ogg', 'file_id': 'AwADAgADFgMAAjgJKEqOtfGGrxO9KgI', 'file_size': 4400}}}
2019-04-28 07:48:00 default[20190428t121301]  [2019-04-28 07:48:00,395] ERROR in app: Exception on /telegram_notification [POST]
2019-04-28 07:48:00 default[20190428t121301]  Traceback (most recent call last):    File "/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app      response = self.full_dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request      rv = self.handle_user_exception(e)    File "/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception      reraise(exc_type, exc_value, tb)    File "/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise      raise value    File "/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request      rv = self.dispatch_request()    File "/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request      return self.view_functions[rule.endpoint](**req.view_args)    File "/home/vmagent/app/main.py", line 97, in telegram_notification      text = data['message']['text']  KeyError: 'text'

如您所见,有些消息发送无限次,有时请求只是丢弃,我得到一个 Traceback KeyError。

我不明白这种行为。

1) 为什么我会收到一些相同的消息? 我不会自己将它发送给我的机器人。没有其他人可以发送它,因为有 chat_id 的检查。

2) 为什么会出现一些Traceback with KeyError? 我认为,根据 API-telegram 文档,Webhook 只允许在某些用户发送消息时发送请求?这里就像 Telegram 不断向我的服务器发送请求..

@app.route('/telegram_notification', methods=['POST'])
def telegram_notification():
    if request.method == "POST":
        data = request.get_json()
        print(data)
        if data is None:
            return jsonify({'success': True})
        # structure of data
        # {'update_id': <int>,
        # 'message':
        #           {'message_id': 138,
        #           'from':
        #               {'id': <int>,
        #               'is_bot': False,
        #               'first_name': '<str>',
        #               'language_code': 'ru'},
        #           'chat':
        #               {'id': <int>,
        #               'first_name': '<str>',
        #               'type': 'private'},
        #           'date': 1556302822,
        #           'text': 'hi'}
        #  }
        # print(data)
        chat_id = data['message']['chat']['id']
        text = data['message']['text']
        if chat_id == <int>:
            # processing
        return jsonify({'success': True})

另外,我不明白,为什么当我删除 Webhook 然后重新设置它,然后重新启动我的 'server',我立即收到来自我的 bot 的几十条消息..

编辑一个

在我的 telegram_notification() 部分下 # proccessing 我请求外部 APIs

我想我明白了。

问题如下。我在 telegram_notification() 中与外部 API 有联系,当我向我的机器人发送消息时,这导致请求外部 API,我期待响应,但外部 APIs 有时 returned 500 错误或 404 错误并且请求被丢弃。

我认为 Telegram 在经过合理次数的尝试后放弃并停止请求我的 Google 云服务器,但 Telegram 继续尝试和发送请求,他们放弃了,我在 return 中什么也没有在 telegram_notification()。 因此,我收到 KeyError,Telegram 不断向服务器发送请求,直到服务器正确响应。

现在我修复了外部 APIs 我从我的机器人那里收到了很多消息。

目前我看到了预期的行为。