add telegram_bot service: delete_message (#8153)

* add telegram_bot service: delete_message

* better validating for `last` message_id option
This commit is contained in:
Eugenio Panadero 2017-06-22 15:03:11 +02:00 committed by Pascal Vizeli
parent ca71d34076
commit 07293e8d1e
2 changed files with 43 additions and 3 deletions

View File

@ -70,6 +70,7 @@ SERVICE_EDIT_MESSAGE = 'edit_message'
SERVICE_EDIT_CAPTION = 'edit_caption'
SERVICE_EDIT_REPLYMARKUP = 'edit_replymarkup'
SERVICE_ANSWER_CALLBACK_QUERY = 'answer_callback_query'
SERVICE_DELETE_MESSAGE = 'delete_message'
EVENT_TELEGRAM_CALLBACK = 'telegram_callback'
EVENT_TELEGRAM_COMMAND = 'telegram_command'
@ -115,19 +116,22 @@ SERVICE_SCHEMA_SEND_LOCATION = BASE_SERVICE_SCHEMA.extend({
})
SERVICE_SCHEMA_EDIT_MESSAGE = SERVICE_SCHEMA_SEND_MESSAGE.extend({
vol.Required(ATTR_MESSAGEID): vol.Any(cv.positive_int, cv.string),
vol.Required(ATTR_MESSAGEID):
vol.Any(cv.positive_int, vol.All(cv.string, 'last')),
vol.Required(ATTR_CHAT_ID): vol.Coerce(int),
})
SERVICE_SCHEMA_EDIT_CAPTION = vol.Schema({
vol.Required(ATTR_MESSAGEID): vol.Any(cv.positive_int, cv.string),
vol.Required(ATTR_MESSAGEID):
vol.Any(cv.positive_int, vol.All(cv.string, 'last')),
vol.Required(ATTR_CHAT_ID): vol.Coerce(int),
vol.Required(ATTR_CAPTION): cv.template,
vol.Optional(ATTR_KEYBOARD_INLINE): cv.ensure_list,
}, extra=vol.ALLOW_EXTRA)
SERVICE_SCHEMA_EDIT_REPLYMARKUP = vol.Schema({
vol.Required(ATTR_MESSAGEID): vol.Any(cv.positive_int, cv.string),
vol.Required(ATTR_MESSAGEID):
vol.Any(cv.positive_int, vol.All(cv.string, 'last')),
vol.Required(ATTR_CHAT_ID): vol.Coerce(int),
vol.Required(ATTR_KEYBOARD_INLINE): cv.ensure_list,
}, extra=vol.ALLOW_EXTRA)
@ -138,6 +142,12 @@ SERVICE_SCHEMA_ANSWER_CALLBACK_QUERY = vol.Schema({
vol.Optional(ATTR_SHOW_ALERT): cv.boolean,
}, extra=vol.ALLOW_EXTRA)
SERVICE_SCHEMA_DELETE_MESSAGE = vol.Schema({
vol.Required(ATTR_CHAT_ID): vol.Coerce(int),
vol.Required(ATTR_MESSAGEID):
vol.Any(cv.positive_int, vol.All(cv.string, 'last')),
}, extra=vol.ALLOW_EXTRA)
SERVICE_MAP = {
SERVICE_SEND_MESSAGE: SERVICE_SCHEMA_SEND_MESSAGE,
SERVICE_SEND_PHOTO: SERVICE_SCHEMA_SEND_FILE,
@ -147,6 +157,7 @@ SERVICE_MAP = {
SERVICE_EDIT_CAPTION: SERVICE_SCHEMA_EDIT_CAPTION,
SERVICE_EDIT_REPLYMARKUP: SERVICE_SCHEMA_EDIT_REPLYMARKUP,
SERVICE_ANSWER_CALLBACK_QUERY: SERVICE_SCHEMA_ANSWER_CALLBACK_QUERY,
SERVICE_DELETE_MESSAGE: SERVICE_SCHEMA_DELETE_MESSAGE,
}
@ -271,6 +282,9 @@ def async_setup(hass, config):
elif msgtype == SERVICE_ANSWER_CALLBACK_QUERY:
yield from hass.async_add_job(
partial(notify_service.answer_callback_query, **kwargs))
elif msgtype == SERVICE_DELETE_MESSAGE:
yield from hass.async_add_job(
partial(notify_service.delete_message, **kwargs))
else:
yield from hass.async_add_job(
partial(notify_service.edit_message, msgtype, **kwargs))
@ -436,6 +450,20 @@ class TelegramNotificationService:
"Error sending message",
chat_id, text, **params)
def delete_message(self, chat_id=None, **kwargs):
"""Delete a previously sent message."""
chat_id = self._get_target_chat_ids(chat_id)[0]
message_id, _ = self._get_msg_ids(kwargs, chat_id)
_LOGGER.debug("Delete message %s in chat ID %s", message_id, chat_id)
deleted = self._send_msg(self.bot.deleteMessage,
"Error deleting message",
chat_id, message_id)
# reduce message_id anyway:
if self._last_message_id[chat_id] is not None:
# change last msg_id for deque(n_msgs)?
self._last_message_id[chat_id] -= 1
return deleted
def edit_message(self, type_edit, chat_id=None, **kwargs):
"""Edit a previously sent message."""
chat_id = self._get_target_chat_ids(chat_id)[0]

View File

@ -225,3 +225,15 @@ answer_callback_query:
show_alert:
description: Show a permanent notification.
example: true
delete_message:
description: Delete a previously sent message.
fields:
message_id:
description: id of the message to delete.
example: '{{ trigger.event.data.message.message_id }}'
chat_id:
description: The chat_id where to delete the message.
example: 12345