From 30bed8341afbc695af63867a1196708cf8af53df Mon Sep 17 00:00:00 2001 From: Stefano Scipioni Date: Wed, 1 Mar 2017 12:15:16 +0100 Subject: [PATCH] Telegram webhooks new text event (#6301) * new TELEGRAM_TEXT * telegram command event renamed in 'telegram_command' * fire telegram_text event anyway --- homeassistant/components/telegram_webhooks.py | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/telegram_webhooks.py b/homeassistant/components/telegram_webhooks.py index b403c191925..f952145f822 100644 --- a/homeassistant/components/telegram_webhooks.py +++ b/homeassistant/components/telegram_webhooks.py @@ -24,7 +24,8 @@ REQUIREMENTS = ['python-telegram-bot==5.3.0'] _LOGGER = logging.getLogger(__name__) -EVENT_TELEGRAM_COMMAND = 'telegram.command' +EVENT_TELEGRAM_COMMAND = 'telegram_command' +EVENT_TELEGRAM_TEXT = 'telegram_text' TELEGRAM_HANDLER_URL = '/api/telegram_webhooks' @@ -40,6 +41,7 @@ DEFAULT_TRUSTED_NETWORKS = [ ] ATTR_COMMAND = 'command' +ATTR_TEXT = 'text' ATTR_USER_ID = 'user_id' ATTR_ARGS = 'args' @@ -118,15 +120,24 @@ class BotPushReceiver(HomeAssistantView): 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') + if not data['text']: + _LOGGER.warning('no text') return self.json({}) - pieces = data['text'].split(' ') + if data['text'][:1] == '/': + # telegram command "/blabla arg1 arg2 ..." + pieces = data['text'].split(' ') - request.app['hass'].bus.async_fire(EVENT_TELEGRAM_COMMAND, { - ATTR_COMMAND: pieces[0], - ATTR_ARGS: " ".join(pieces[1:]), + request.app['hass'].bus.async_fire(EVENT_TELEGRAM_COMMAND, { + ATTR_COMMAND: pieces[0], + ATTR_ARGS: " ".join(pieces[1:]), + ATTR_USER_ID: data['from']['id'], + }) + + # telegram text "bla bla" + request.app['hass'].bus.async_fire(EVENT_TELEGRAM_TEXT, { + ATTR_TEXT: data['text'], ATTR_USER_ID: data['from']['id'], }) + return self.json({})