1
votes

I am working with telegram-bot.

I registered my bot in BotFather, established "server" on Google Cloud, setup webHook.

My bot is working okay, when I send some command to my bot it replies expected way. The thing that I cannot understand is messages from my 'server' (google cloud).

When I analyze it I have a feeling that telegram constantly sends requests to my API and not every message I previosly send but some of them.

This is log:

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'

As you can see, some messages send infinity number of times, and sometimes requests just drop and I got a Traceback KeyError.

I do not understand that behaviour.

1) Why I am getting some same messages ? I am not sending it to my bot myself. Noone else can send it, since there is check for chat_id.

2) Why there are some Traceback with KeyError? I think, according to API-telegram docs, that Webhook allows to send request only when some users send message? And here it is like Telegram is constantly sending request to my server..

@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})

Also, I do not understand, why when I delete Webhook and then set it again, then restart my 'server', I received instantly dozen of messages from my bot..

EDIT ONE

In my telegram_notification() under the section # proccessing I am requesting external APIs

1
are you returning a response to telegram after every request? - Amirsadjad
I am,returning it not after a sent message to my bot, but simply after every second.I see it in my log. I am getting requests to my API without any effort from my side - John

1 Answers

1
votes

I think I got the point here.

The problem was as follows. I had connection with external APIs in telegram_notification(), and when I sent message to my bot, which then led to requesting external API, I expected the response, but external APIs sometimes returned 500 error or 404 error and requests dropped.

I thought that Telegram gives up after reasonable numbers of tries and stopped requesting my Server of Google Cloud, but Telegram kept on trying and sending requests, they dropped and I got nothing in return in telegram_notification(). Because of that I was getting KeyError and Telegram constantly sent requests to the server, until server responses correctly.

Now I fixed the external APIs I got dozens of messages from my bot.

At the current time I see the expected behaviour.