From 14cf5b884b32707cf97b180937976b7b0cb0088c Mon Sep 17 00:00:00 2001 From: Stefano Scipioni Date: Mon, 20 Feb 2017 21:15:45 +0100 Subject: [PATCH] =?UTF-8?q?fixed=20error=20caused=20by=20POST=20call=20wit?= =?UTF-8?q?h=20no=20'message'=20inside=20(for=20example=E2=80=A6=20(#6038)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fixed error caused by POST call with no 'message' inside (for example in edited messages) * avoid assert --- homeassistant/components/telegram_webhooks.py | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/telegram_webhooks.py b/homeassistant/components/telegram_webhooks.py index 30f7935d40b..b403c191925 100644 --- a/homeassistant/components/telegram_webhooks.py +++ b/homeassistant/components/telegram_webhooks.py @@ -104,21 +104,26 @@ class BotPushReceiver(HomeAssistantView): try: data = yield from request.json() - data = data['message'] - - if data['from']['id'] not in self.users: - _LOGGER.warning("User not allowed") - return self.json_message('Invalid user', HTTP_BAD_REQUEST) - - if data['text'][0] != '/': - _LOGGER.warning('no command') - return self.json({}) - except (KeyError, IndexError): + except ValueError: + _LOGGER.error("Received telegram data: %s", data) return self.json_message('Invalid JSON', HTTP_BAD_REQUEST) + # check for basic message rules + data = data.get('message') + if not data or 'from' not in data or 'text' not in data: + return self.json({}) + + if data['from'].get('id') not in self.users: + _LOGGER.warning("User not allowed") + return self.json_message('Invalid user', HTTP_BAD_REQUEST) + _LOGGER.debug("Received telegram data: %s", data) + if not data['text'] or data['text'][:1] != '/': + _LOGGER.warning('no command') + return self.json({}) pieces = data['text'].split(' ') + request.app['hass'].bus.async_fire(EVENT_TELEGRAM_COMMAND, { ATTR_COMMAND: pieces[0], ATTR_ARGS: " ".join(pieces[1:]),