Skip to content

Commit 25f088b

Browse files
authored
Merge pull request #86 from makespacemadrid/codex/fix-group-chat-responses-for-subgroup
Keep Telegram command replies in forum topics
2 parents 615db82 + 6fc738f commit 25f088b

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

app/web/routes/main_routes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,14 @@ def _answer_and_send(ctx):
945945
),
946946
)
947947
if TELEGRAM_BOT_TOKEN and chat_id and result["kind"] == "send_message":
948-
TelegramClient(TELEGRAM_BOT_TOKEN, str(chat_id), "").send_message(result["text"])
948+
# Commands can arrive inside a forum topic just like natural-language
949+
# messages. Keep their deterministic replies in that same topic rather
950+
# than silently posting them to the supergroup's General topic.
951+
TelegramClient(
952+
TELEGRAM_BOT_TOKEN,
953+
str(chat_id),
954+
str(message_ctx.get("message_thread_id") or ""),
955+
).send_message(result["text"])
949956

950957
return {"ok": True}, 200
951958

docs/APIDOC.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,8 @@ In private chats every non-command message is eligible. In groups, the assistant
646646
responds when a member addresses it with an `@mention` or replies to one of its
647647
messages; other group conversation is ignored. Set `TELEGRAM_BOT_USERNAME` so
648648
mentions can be matched exactly when the bot's Telegram privacy mode is disabled.
649-
Responses stay in the incoming forum topic when `message_thread_id` is present.
649+
Assistant and command responses stay in the incoming forum topic when
650+
`message_thread_id` is present.
650651
Mutating tool calls are never executed immediately: the administrator must send
651652
`/confirm` within `TELEGRAM_CONFIRM_TTL_SECONDS` (default: 300) to execute the
652653
pending action or `/cancel` to discard it. Conversation

tests/test_app_functionality.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,41 @@ def test_telegram_webhook_non_command_message_is_ignored(self):
17001700
conn.close()
17011701
self.assertEqual(total, 0)
17021702

1703+
def test_telegram_webhook_command_reply_stays_in_forum_topic(self):
1704+
from app.web.routes import main_routes
1705+
1706+
clients = []
1707+
1708+
def _fake_send_message(client, message):
1709+
clients.append((client.chat_id, client.thread_id, message))
1710+
return True
1711+
1712+
old_secret = main_routes.TELEGRAM_WEBHOOK_SECRET
1713+
old_token = main_routes.TELEGRAM_BOT_TOKEN
1714+
main_routes.TELEGRAM_WEBHOOK_SECRET = "hook-secret"
1715+
main_routes.TELEGRAM_BOT_TOKEN = "bot-token"
1716+
try:
1717+
with patch.object(main_routes.TelegramClient, "send_message", new=_fake_send_message):
1718+
response = self.client.post(
1719+
"/telegram/webhook/hook-secret",
1720+
json={
1721+
"message": {
1722+
"text": "/help",
1723+
"from": {"username": "admin"},
1724+
"chat": {"id": -100123, "type": "supergroup"},
1725+
"message_thread_id": 42,
1726+
}
1727+
},
1728+
)
1729+
finally:
1730+
main_routes.TELEGRAM_WEBHOOK_SECRET = old_secret
1731+
main_routes.TELEGRAM_BOT_TOKEN = old_token
1732+
1733+
self.assertEqual(response.status_code, 200)
1734+
self.assertEqual(len(clients), 1)
1735+
self.assertEqual(clients[0][:2], ("-100123", "42"))
1736+
self.assertIn("ManaVote bot is running", clients[0][2])
1737+
17031738
def test_telegram_webhook_supports_edited_message_payload(self):
17041739
poll_id = self._latest_poll_id()
17051740
from app.web.routes import main_routes

0 commit comments

Comments
 (0)