From aa3d30f7b7ea54aa2101d6bf9af44e050e0c3242 Mon Sep 17 00:00:00 2001 From: Stephane Desneux Date: Sat, 15 Jan 2022 21:31:28 +0100 Subject: [PATCH] Telegram: enhanced bot owner detection to avoid exception (#517) --- apprise/plugins/NotifyTelegram.py | 18 ++++---- test/test_plugin_telegram.py | 71 ++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/apprise/plugins/NotifyTelegram.py b/apprise/plugins/NotifyTelegram.py index 3d9d718e..d7a0f2ee 100644 --- a/apprise/plugins/NotifyTelegram.py +++ b/apprise/plugins/NotifyTelegram.py @@ -483,15 +483,15 @@ class NotifyTelegram(NotifyBase): # "text":"/start", # "entities":[{"offset":0,"length":6,"type":"bot_command"}]}}] - if 'ok' in response and response['ok'] is True \ - and 'result' in response and len(response['result']): - entry = response['result'][0] - _id = entry['message']['from'].get('id', 0) - _user = entry['message']['from'].get('first_name') - self.logger.info('Detected Telegram user %s (userid=%d)' % ( - _user, _id)) - # Return our detected userid - return _id + if response.get('ok', False): + for entry in response.get('result', []): + if 'message' in entry and 'from' in entry['message']: + _id = entry['message']['from'].get('id', 0) + _user = entry['message']['from'].get('first_name') + self.logger.info( + 'Detected Telegram user %s (userid=%d)' % (_user, _id)) + # Return our detected userid + return _id self.logger.warning( 'Failed to detect a Telegram user; ' diff --git a/test/test_plugin_telegram.py b/test/test_plugin_telegram.py index 13028331..20bfdda9 100644 --- a/test/test_plugin_telegram.py +++ b/test/test_plugin_telegram.py @@ -349,9 +349,30 @@ def test_plugin_telegram_general(mock_post, mock_get): mock_post.return_value.content = dumps({ "ok": True, "result": [{ + "update_id": 645421319, + # Entry without `message` in it + }, { + # Entry without `from` in `message` + "update_id": 645421320, + "message": { + "message_id": 2, + "chat": { + "id": 532389719, + "first_name": "Chris", + "type": "private" + }, + "date": 1519694394, + "text": "/start", + "entities": [{ + "offset": 0, + "length": 6, + "type": "bot_command", + }], + } + }, { "update_id": 645421321, "message": { - "message_id": 1, + "message_id": 2, "from": { "id": 532389719, "is_bot": False, @@ -428,6 +449,54 @@ def test_plugin_telegram_general(mock_post, mock_get): assert obj.notify(title='hello', body='world') is False assert len(obj.targets) == 0 + # Do the test again, but with ok not set to True + mock_post.return_value.content = dumps({ + "ok": False, + "result": [{ + "update_id": 645421321, + "message": { + "message_id": 2, + "from": { + "id": 532389719, + "is_bot": False, + "first_name": "Chris", + "language_code": "en-US" + }, + "chat": { + "id": 532389719, + "first_name": "Chris", + "type": "private" + }, + "date": 1519694394, + "text": "/start", + "entities": [{ + "offset": 0, + "length": 6, + "type": "bot_command", + }], + }}, + ], + }) + + # No user will be detected now + obj = plugins.NotifyTelegram(bot_token=bot_token, targets=None) + # No user detected; this happens after our firsst notification + assert len(obj.targets) == 0 + assert obj.notify(title='hello', body='world') is False + assert len(obj.targets) == 0 + + # An edge case where no results were provided; this will probably never + # happen, but it helps with test coverage completeness + mock_post.return_value.content = dumps({ + "ok": True, + }) + + # No user will be detected now + obj = plugins.NotifyTelegram(bot_token=bot_token, targets=None) + # No user detected; this happens after our firsst notification + assert len(obj.targets) == 0 + assert obj.notify(title='hello', body='world') is False + assert len(obj.targets) == 0 # Detect the bot with a bad response mock_post.return_value.content = dumps({}) obj.detect_bot_owner()