Add send_poll to telegram bot (#68666)

This commit is contained in:
Anders Liljekvist 2022-06-29 16:08:58 +02:00 committed by GitHub
parent 8dd5f25da9
commit f5d8487768
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 0 deletions

View file

@ -80,6 +80,12 @@ ATTR_VERIFY_SSL = "verify_ssl"
ATTR_TIMEOUT = "timeout"
ATTR_MESSAGE_TAG = "message_tag"
ATTR_CHANNEL_POST = "channel_post"
ATTR_QUESTION = "question"
ATTR_OPTIONS = "options"
ATTR_ANSWERS = "answers"
ATTR_OPEN_PERIOD = "open_period"
ATTR_IS_ANONYMOUS = "is_anonymous"
ATTR_ALLOWS_MULTIPLE_ANSWERS = "allows_multiple_answers"
CONF_ALLOWED_CHAT_IDS = "allowed_chat_ids"
CONF_PROXY_URL = "proxy_url"
@ -96,6 +102,7 @@ SERVICE_SEND_VIDEO = "send_video"
SERVICE_SEND_VOICE = "send_voice"
SERVICE_SEND_DOCUMENT = "send_document"
SERVICE_SEND_LOCATION = "send_location"
SERVICE_SEND_POLL = "send_poll"
SERVICE_EDIT_MESSAGE = "edit_message"
SERVICE_EDIT_CAPTION = "edit_caption"
SERVICE_EDIT_REPLYMARKUP = "edit_replymarkup"
@ -184,6 +191,19 @@ SERVICE_SCHEMA_SEND_LOCATION = BASE_SERVICE_SCHEMA.extend(
}
)
SERVICE_SCHEMA_SEND_POLL = vol.Schema(
{
vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [vol.Coerce(int)]),
vol.Required(ATTR_QUESTION): cv.string,
vol.Required(ATTR_OPTIONS): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_OPEN_PERIOD): cv.positive_int,
vol.Optional(ATTR_IS_ANONYMOUS, default=True): cv.boolean,
vol.Optional(ATTR_ALLOWS_MULTIPLE_ANSWERS, default=False): cv.boolean,
vol.Optional(ATTR_DISABLE_NOTIF): cv.boolean,
vol.Optional(ATTR_TIMEOUT): cv.positive_int,
}
)
SERVICE_SCHEMA_EDIT_MESSAGE = SERVICE_SCHEMA_SEND_MESSAGE.extend(
{
vol.Required(ATTR_MESSAGEID): vol.Any(
@ -246,6 +266,7 @@ SERVICE_MAP = {
SERVICE_SEND_VOICE: SERVICE_SCHEMA_SEND_FILE,
SERVICE_SEND_DOCUMENT: SERVICE_SCHEMA_SEND_FILE,
SERVICE_SEND_LOCATION: SERVICE_SCHEMA_SEND_LOCATION,
SERVICE_SEND_POLL: SERVICE_SCHEMA_SEND_POLL,
SERVICE_EDIT_MESSAGE: SERVICE_SCHEMA_EDIT_MESSAGE,
SERVICE_EDIT_CAPTION: SERVICE_SCHEMA_EDIT_CAPTION,
SERVICE_EDIT_REPLYMARKUP: SERVICE_SCHEMA_EDIT_REPLYMARKUP,
@ -399,6 +420,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await hass.async_add_executor_job(
partial(notify_service.send_location, **kwargs)
)
elif msgtype == SERVICE_SEND_POLL:
await hass.async_add_executor_job(
partial(notify_service.send_poll, **kwargs)
)
elif msgtype == SERVICE_ANSWER_CALLBACK_QUERY:
await hass.async_add_executor_job(
partial(notify_service.answer_callback_query, **kwargs)
@ -847,6 +872,34 @@ class TelegramNotificationService:
timeout=params[ATTR_TIMEOUT],
)
def send_poll(
self,
question,
options,
is_anonymous,
allows_multiple_answers,
target=None,
**kwargs,
):
"""Send a poll."""
params = self._get_msg_kwargs(kwargs)
openperiod = kwargs.get(ATTR_OPEN_PERIOD)
for chat_id in self._get_target_chat_ids(target):
_LOGGER.debug("Send poll '%s' to chat ID %s", question, chat_id)
self._send_msg(
self.bot.send_poll,
"Error sending poll",
params[ATTR_MESSAGE_TAG],
chat_id=chat_id,
question=question,
options=options,
is_anonymous=is_anonymous,
allows_multiple_answers=allows_multiple_answers,
open_period=openperiod,
disable_notification=params[ATTR_DISABLE_NOTIF],
timeout=params[ATTR_TIMEOUT],
)
def leave_chat(self, chat_id=None):
"""Remove bot from chat."""
chat_id = self._get_target_chat_ids(chat_id)[0]

View file

@ -678,6 +678,60 @@ send_location:
selector:
text:
send_poll:
name: Send poll
description: Send a poll.
fields:
target:
name: Target
description: An array of pre-authorized chat_ids to send the location to. If not present, first allowed chat_id is the default.
example: "[12345, 67890] or 12345"
selector:
object:
question:
name: Question
description: Poll question, 1-300 characters
required: true
selector:
text:
options:
name: Options
description: List of answer options, 2-10 strings 1-100 characters each
required: true
selector:
object:
is_anonymous:
name: Is Anonymous
description: If the poll needs to be anonymous, defaults to True
selector:
boolean:
allows_multiple_answers:
name: Allow Multiple Answers
description: If the poll allows multiple answers, defaults to False
selector:
boolean:
open_period:
name: Open Period
description: Amount of time in seconds the poll will be active after creation, 5-600.
selector:
number:
min: 5
max: 600
unit_of_measurement: seconds
disable_notification:
name: Disable notification
description: Sends the message silently. iOS users and Web users will not receive a notification, Android users will receive a notification with no sound.
selector:
boolean:
timeout:
name: Timeout
description: Timeout for send poll. Will help with timeout errors (poor internet connection, etc)
selector:
number:
min: 1
max: 3600
unit_of_measurement: seconds
edit_message:
name: Edit message
description: Edit a previously sent message.