Skip to content

Commit 8ec483b

Browse files
authored
Fix Telegram bot bug where message is sent to wrong recipient (home-assistant#154658)
1 parent bf14cac commit 8ec483b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

homeassistant/components/telegram_bot/bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ async def send_message(
578578
"Error sending message",
579579
params[ATTR_MESSAGE_TAG],
580580
text,
581+
target=target,
581582
parse_mode=params[ATTR_PARSER],
582583
disable_web_page_preview=params[ATTR_DISABLE_WEB_PREV],
583584
disable_notification=params[ATTR_DISABLE_NOTIF],

tests/components/telegram_bot/test_telegram_bot.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,3 +1445,31 @@ async def test_set_message_reaction(
14451445
is_big=True,
14461446
read_timeout=None,
14471447
)
1448+
1449+
1450+
async def test_send_message_multi_target(
1451+
hass: HomeAssistant,
1452+
mock_broadcast_config_entry: MockConfigEntry,
1453+
mock_external_calls: None,
1454+
) -> None:
1455+
"""Test send message for entries with multiple chat_ids."""
1456+
1457+
mock_broadcast_config_entry.add_to_hass(hass)
1458+
await hass.config_entries.async_setup(mock_broadcast_config_entry.entry_id)
1459+
await hass.async_block_till_done()
1460+
1461+
# we have 2 whitelisted chat ids in the config subentries: 123456 and 654321
1462+
# 123456 is the default target since it is the first in the list
1463+
# This test checks that the message is sent to the right target
1464+
1465+
response = await hass.services.async_call(
1466+
DOMAIN,
1467+
SERVICE_SEND_MESSAGE,
1468+
{ATTR_TARGET: 654321, ATTR_MESSAGE: "test_message"},
1469+
blocking=True,
1470+
return_response=True,
1471+
)
1472+
1473+
await hass.async_block_till_done()
1474+
assert response["chats"][0]["chat_id"] == 654321
1475+
assert response["chats"][0]["message_id"] == 12345

0 commit comments

Comments
 (0)