fixed error caused by POST call with no 'message' inside (for example… (#6038)

* fixed error caused by POST call with no 'message' inside (for example in edited messages)

* avoid assert
This commit is contained in:
Stefano Scipioni 2017-02-20 21:15:45 +01:00 committed by Pascal Vizeli
parent 3d34368e6e
commit 14cf5b884b

View File

@ -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:]),