mirror of
https://github.com/home-assistant/core.git
synced 2025-08-05 13:45:12 +02:00
Add bot details to Telegram bot events (#148638)
This commit is contained in:
@@ -101,13 +101,26 @@ _LOGGER = logging.getLogger(__name__)
|
||||
type TelegramBotConfigEntry = ConfigEntry[TelegramNotificationService]
|
||||
|
||||
|
||||
def _get_bot_info(bot: Bot, config_entry: ConfigEntry) -> dict[str, Any]:
|
||||
return {
|
||||
"config_entry_id": config_entry.entry_id,
|
||||
"id": bot.id,
|
||||
"first_name": bot.first_name,
|
||||
"last_name": bot.last_name,
|
||||
"username": bot.username,
|
||||
}
|
||||
|
||||
|
||||
class BaseTelegramBot:
|
||||
"""The base class for the telegram bot."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, config: TelegramBotConfigEntry) -> None:
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, config: TelegramBotConfigEntry, bot: Bot
|
||||
) -> None:
|
||||
"""Initialize the bot base class."""
|
||||
self.hass = hass
|
||||
self.config = config
|
||||
self._bot = bot
|
||||
|
||||
@abstractmethod
|
||||
async def shutdown(self) -> None:
|
||||
@@ -134,6 +147,8 @@ class BaseTelegramBot:
|
||||
_LOGGER.warning("Unhandled update: %s", update)
|
||||
return True
|
||||
|
||||
event_data["bot"] = _get_bot_info(self._bot, self.config)
|
||||
|
||||
event_context = Context()
|
||||
|
||||
_LOGGER.debug("Firing event %s: %s", event_type, event_data)
|
||||
@@ -442,6 +457,9 @@ class TelegramNotificationService:
|
||||
event_data[ATTR_MESSAGE_THREAD_ID] = kwargs_msg[
|
||||
ATTR_MESSAGE_THREAD_ID
|
||||
]
|
||||
|
||||
event_data["bot"] = _get_bot_info(self.bot, self.config)
|
||||
|
||||
self.hass.bus.async_fire(
|
||||
EVENT_TELEGRAM_SENT, event_data, context=context
|
||||
)
|
||||
|
@@ -54,7 +54,7 @@ class PollBot(BaseTelegramBot):
|
||||
self, hass: HomeAssistant, bot: Bot, config: TelegramBotConfigEntry
|
||||
) -> None:
|
||||
"""Create Application to poll for updates."""
|
||||
super().__init__(hass, config)
|
||||
super().__init__(hass, config, bot)
|
||||
self.bot = bot
|
||||
self.application = ApplicationBuilder().bot(self.bot).build()
|
||||
self.application.add_handler(TypeHandler(Update, self.handle_update))
|
||||
|
@@ -77,7 +77,7 @@ class PushBot(BaseTelegramBot):
|
||||
# Dumb Application that just gets our updates to our handler callback (self.handle_update)
|
||||
self.application = ApplicationBuilder().bot(bot).updater(None).build()
|
||||
self.application.add_handler(TypeHandler(Update, self.handle_update))
|
||||
super().__init__(hass, config)
|
||||
super().__init__(hass, config, bot)
|
||||
|
||||
self.base_url = config.data.get(CONF_URL) or get_url(
|
||||
hass, require_ssl=True, allow_internal=False
|
||||
|
@@ -96,7 +96,7 @@ def mock_external_calls() -> Generator[None]:
|
||||
max_reaction_count=100,
|
||||
accent_color_id=AccentColor.COLOR_000,
|
||||
)
|
||||
test_user = User(123456, "Testbot", True)
|
||||
test_user = User(123456, "Testbot", True, "mock last name", "mock username")
|
||||
message = Message(
|
||||
message_id=12345,
|
||||
date=datetime.now(),
|
||||
|
@@ -174,6 +174,15 @@ async def test_send_message(
|
||||
assert len(events) == 1
|
||||
assert events[0].context == context
|
||||
|
||||
config_entry = hass.config_entries.async_entry_for_domain_unique_id(
|
||||
DOMAIN, "1234567890:ABC"
|
||||
)
|
||||
assert events[0].data["bot"]["config_entry_id"] == config_entry.entry_id
|
||||
assert events[0].data["bot"]["id"] == 123456
|
||||
assert events[0].data["bot"]["first_name"] == "Testbot"
|
||||
assert events[0].data["bot"]["last_name"] == "mock last name"
|
||||
assert events[0].data["bot"]["username"] == "mock username"
|
||||
|
||||
assert len(response["chats"]) == 1
|
||||
assert (response["chats"][0]["message_id"]) == 12345
|
||||
|
||||
@@ -479,6 +488,16 @@ async def test_polling_platform_message_text_update(
|
||||
|
||||
assert len(events) == 1
|
||||
assert events[0].data["text"] == update_message_text["message"]["text"]
|
||||
|
||||
config_entry = hass.config_entries.async_entry_for_domain_unique_id(
|
||||
DOMAIN, "1234567890:ABC"
|
||||
)
|
||||
assert events[0].data["bot"]["config_entry_id"] == config_entry.entry_id
|
||||
assert events[0].data["bot"]["id"] == 123456
|
||||
assert events[0].data["bot"]["first_name"] == "Testbot"
|
||||
assert events[0].data["bot"]["last_name"] == "mock last name"
|
||||
assert events[0].data["bot"]["username"] == "mock username"
|
||||
|
||||
assert isinstance(events[0].context, Context)
|
||||
|
||||
|
||||
@@ -752,7 +771,7 @@ async def test_send_message_no_chat_id_error(
|
||||
)
|
||||
|
||||
assert err.value.translation_key == "missing_allowed_chat_ids"
|
||||
assert err.value.translation_placeholders["bot_name"] == "Testbot"
|
||||
assert err.value.translation_placeholders["bot_name"] == "Testbot mock last name"
|
||||
|
||||
|
||||
async def test_send_message_config_entry_error(
|
||||
|
Reference in New Issue
Block a user