Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegram Bot: Add support for reacting to messages #127604

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions homeassistant/components/telegram_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
ATTR_MSGID = "id"
ATTR_PARSER = "parse_mode"
ATTR_PASSWORD = "password"
ATTR_REACTION = "reaction"
ATTR_REPLY_TO_MSGID = "reply_to_message_id"
ATTR_REPLYMARKUP = "reply_markup"
ATTR_SHOW_ALERT = "show_alert"
Expand All @@ -90,6 +91,7 @@
ATTR_ANSWERS = "answers"
ATTR_OPEN_PERIOD = "open_period"
ATTR_IS_ANONYMOUS = "is_anonymous"
ATTR_IS_BIG = "is_big"
ATTR_ALLOWS_MULTIPLE_ANSWERS = "allows_multiple_answers"
ATTR_MESSAGE_THREAD_ID = "message_thread_id"

Expand All @@ -112,6 +114,7 @@
SERVICE_EDIT_MESSAGE = "edit_message"
SERVICE_EDIT_CAPTION = "edit_caption"
SERVICE_EDIT_REPLYMARKUP = "edit_replymarkup"
SERVICE_SET_MESSAGE_REACTION = "set_message_reaction"
SERVICE_ANSWER_CALLBACK_QUERY = "answer_callback_query"
SERVICE_DELETE_MESSAGE = "delete_message"
SERVICE_LEAVE_CHAT = "leave_chat"
Expand Down Expand Up @@ -246,6 +249,18 @@
extra=vol.ALLOW_EXTRA,
)

SERVICE_SCHEMA_SET_MESSAGE_REACTION = vol.Schema(
{
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_REACTION): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_IS_BIG, default=False): cv.boolean,
},
extra=vol.ALLOW_EXTRA,
)

SERVICE_SCHEMA_ANSWER_CALLBACK_QUERY = vol.Schema(
{
vol.Required(ATTR_MESSAGE): cv.template,
Expand Down Expand Up @@ -280,6 +295,7 @@
SERVICE_EDIT_MESSAGE: SERVICE_SCHEMA_EDIT_MESSAGE,
SERVICE_EDIT_CAPTION: SERVICE_SCHEMA_EDIT_CAPTION,
SERVICE_EDIT_REPLYMARKUP: SERVICE_SCHEMA_EDIT_REPLYMARKUP,
SERVICE_SET_MESSAGE_REACTION: SERVICE_SCHEMA_SET_MESSAGE_REACTION,
SERVICE_ANSWER_CALLBACK_QUERY: SERVICE_SCHEMA_ANSWER_CALLBACK_QUERY,
SERVICE_DELETE_MESSAGE: SERVICE_SCHEMA_DELETE_MESSAGE,
SERVICE_LEAVE_CHAT: SERVICE_SCHEMA_LEAVE_CHAT,
Expand Down Expand Up @@ -456,6 +472,8 @@ def _render_template_attr(data, attribute):
await notify_service.answer_callback_query(
context=service.context, **kwargs
)
elif msgtype == SERVICE_SET_MESSAGE_REACTION:
await notify_service.set_message_reaction(context=service.context, **kwargs)
elif msgtype == SERVICE_DELETE_MESSAGE:
await notify_service.delete_message(context=service.context, **kwargs)
else:
Expand Down Expand Up @@ -841,6 +859,32 @@ async def answer_callback_query(
context=context,
)

async def set_message_reaction(
self, chat_id, reaction, is_big=False, context=None, **kwargs
):
"""Set the bot's reaction for a given message."""
chat_id = self._get_target_chat_ids(chat_id)[0]
message_id, inline_message_id = self._get_msg_ids(kwargs, chat_id)
params = self._get_msg_kwargs(kwargs)
_LOGGER.debug(
"Set reaction to message %s in chat ID %s to %s with params: %s",
message_id or inline_message_id,
chat_id,
reaction,
params,
)
await self._send_msg(
self.bot.set_message_reaction,
"Error setting message reaction",
params[ATTR_MESSAGE_TAG],
chat_id,
message_id,
reaction=reaction,
is_big=is_big,
read_timeout=params[ATTR_TIMEOUT],
context=context,
)

async def send_file(
self, file_type=SERVICE_SEND_PHOTO, target=None, context=None, **kwargs
):
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/telegram_bot/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"edit_replymarkup": {
"service": "mdi:pencil"
},
"set_message_reaction": {
"service": "mdi:emoticon"
},
"answer_callback_query": {
"service": "mdi:check"
},
Expand Down
22 changes: 22 additions & 0 deletions homeassistant/components/telegram_bot/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,28 @@ edit_replymarkup:
selector:
object:

set_message_reaction:
fields:
message_id:
required: true
example: 54321
selector:
text:
chat_id:
required: true
example: 12345
selector:
text:
reaction:
required: true
example: ["👍"]
selector:
object:
is_big:
required: false
selector:
boolean:

answer_callback_query:
fields:
message:
Expand Down
22 changes: 22 additions & 0 deletions homeassistant/components/telegram_bot/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,28 @@
}
}
},
"set_message_reaction": {
"name": "Set message reaction",
"description": "Sets the bot's emoji reaction to a message.",
"fields": {
"message_id": {
"name": "Message ID",
"description": "Id of the message to react to."
},
"chat_id": {
"name": "Chat ID",
"description": "Id of the chat containing the message."
},
"reaction": {
"name": "Reaction",
"description": "Emoji reaction to use."
},
"is_big": {
"name": "Is big",
"description": "Whether the reaction animation should be big, defaults to False."
}
}
},
"answer_callback_query": {
"name": "Answer callback query",
"description": "Responds to a callback query originated by clicking on an online keyboard button. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert.",
Expand Down