Telegram webhooks new text event (#6301)
* new TELEGRAM_TEXT * telegram command event renamed in 'telegram_command' * fire telegram_text event anyway
This commit is contained in:
parent
d17733427b
commit
30bed8341a
1 changed files with 18 additions and 7 deletions
|
@ -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({})
|
||||
|
|
Loading…
Add table
Reference in a new issue