diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index cb22487ba6314..e591e68778ca0 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -67,6 +67,7 @@ ATTR_USERNAME = "username" ATTR_VERIFY_SSL = "verify_ssl" ATTR_TIMEOUT = "timeout" +ATTR_MESSAGE_TAG = "message_tag" CONF_ALLOWED_CHAT_IDS = "allowed_chat_ids" CONF_PROXY_URL = "proxy_url" @@ -91,6 +92,7 @@ EVENT_TELEGRAM_CALLBACK = "telegram_callback" EVENT_TELEGRAM_COMMAND = "telegram_command" EVENT_TELEGRAM_TEXT = "telegram_text" +EVENT_TELEGRAM_SENT = "telegram_sent" PARSER_HTML = "html" PARSER_MD = "markdown" @@ -136,6 +138,7 @@ vol.Optional(ATTR_KEYBOARD): vol.All(cv.ensure_list, [cv.string]), vol.Optional(ATTR_KEYBOARD_INLINE): cv.ensure_list, vol.Optional(ATTR_TIMEOUT): cv.positive_int, + vol.Optional(ATTR_MESSAGE_TAG): cv.string, }, extra=vol.ALLOW_EXTRA, ) @@ -500,6 +503,7 @@ def _make_row_inline_keyboard(row_keyboard): ATTR_REPLY_TO_MSGID: None, ATTR_REPLYMARKUP: None, ATTR_TIMEOUT: None, + ATTR_MESSAGE_TAG: None, } if data is not None: if ATTR_PARSER in data: @@ -514,6 +518,8 @@ def _make_row_inline_keyboard(row_keyboard): params[ATTR_DISABLE_WEB_PREV] = data[ATTR_DISABLE_WEB_PREV] if ATTR_REPLY_TO_MSGID in data: params[ATTR_REPLY_TO_MSGID] = data[ATTR_REPLY_TO_MSGID] + if ATTR_MESSAGE_TAG in data: + params[ATTR_MESSAGE_TAG] = data[ATTR_MESSAGE_TAG] # Keyboards: if ATTR_KEYBOARD in data: keys = data.get(ATTR_KEYBOARD) @@ -540,12 +546,22 @@ def _send_msg(self, func_send, msg_error, *args_msg, **kwargs_msg): out = func_send(*args_msg, **kwargs_msg) if not isinstance(out, bool) and hasattr(out, ATTR_MESSAGEID): chat_id = out.chat_id - self._last_message_id[chat_id] = out[ATTR_MESSAGEID] + message_id = out[ATTR_MESSAGEID] + self._last_message_id[chat_id] = message_id _LOGGER.debug( "Last message ID: %s (from chat_id %s)", self._last_message_id, chat_id, ) + + event_data = { + ATTR_CHAT_ID: chat_id, + ATTR_MESSAGEID: message_id, + } + message_tag = kwargs_msg.get(ATTR_MESSAGE_TAG) + if message_tag is not None: + event_data[ATTR_MESSAGE_TAG] = message_tag + self.hass.bus.async_fire(EVENT_TELEGRAM_SENT, event_data) elif not isinstance(out, bool): _LOGGER.warning( "Update last message: out_type:%s, out=%s", type(out), out diff --git a/homeassistant/components/telegram_bot/services.yaml b/homeassistant/components/telegram_bot/services.yaml index 8e42b25c0caf9..0d9eefa759356 100644 --- a/homeassistant/components/telegram_bot/services.yaml +++ b/homeassistant/components/telegram_bot/services.yaml @@ -30,6 +30,9 @@ send_message: inline_keyboard: description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with associated callback data. example: '["/button1, /button2", "/button3"] or ["Text button1:/button1, Text button2:/button2", "Text button3:/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]' + message_tag: + description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}' + example: "msg_to_edit" send_photo: description: Send a photo. @@ -67,6 +70,9 @@ send_photo: inline_keyboard: description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with associated callback data. example: '["/button1, /button2", "/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]' + message_tag: + description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}' + example: "msg_to_edit" send_sticker: description: Send a sticker. @@ -101,6 +107,9 @@ send_sticker: inline_keyboard: description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with associated callback data. example: '["/button1, /button2", "/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]' + message_tag: + description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}' + example: "msg_to_edit" send_video: description: Send a video. @@ -138,6 +147,9 @@ send_video: inline_keyboard: description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with associated callback data. example: '["/button1, /button2", "/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]' + message_tag: + description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}' + example: "msg_to_edit" send_document: description: Send a document. @@ -175,6 +187,9 @@ send_document: inline_keyboard: description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with associated callback data. example: '["/button1, /button2", "/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]' + message_tag: + description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}' + example: "msg_to_edit" send_location: description: Send a location. @@ -200,6 +215,9 @@ send_location: inline_keyboard: description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with associated callback data. example: '["/button1, /button2", "/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]' + message_tag: + description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}' + example: "msg_to_edit" edit_message: description: Edit a previusly sent message.