-
Notifications
You must be signed in to change notification settings - Fork 52.1k
feat(telegram): config-driven callback→text routes for inline keyboards #43949
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
Open
tjkang
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
tjkang:callback-text-routes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| """Tests for config-driven Telegram callback→text routes (extra.callback_text_routes).""" | ||
|
|
||
| from types import SimpleNamespace | ||
| from typing import cast | ||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from gateway.config import PlatformConfig | ||
| from gateway.platforms.telegram import TelegramAdapter | ||
|
|
||
| ROUTES = { | ||
| "log:breakfast:default": "Yes, had the usual ✅", | ||
| "log:breakfast:custom": "Had something different ✏️", | ||
| } | ||
|
|
||
|
|
||
| def _make_adapter(routes=ROUTES): | ||
| extra = {"callback_text_routes": routes} if routes is not None else {} | ||
| config = PlatformConfig(enabled=True, token="test-token", extra=extra) | ||
| adapter = TelegramAdapter(config) | ||
| adapter._bot = AsyncMock() | ||
| adapter._app = MagicMock() | ||
| adapter.handle_message = AsyncMock() | ||
| return adapter | ||
|
|
||
|
|
||
| def _make_callback(data: str): | ||
| chat = SimpleNamespace( | ||
| id=12345, | ||
| type="private", | ||
| title=None, | ||
| full_name="Alice", | ||
| is_forum=False, | ||
| ) | ||
| prompt_message = SimpleNamespace( | ||
| chat=chat, | ||
| chat_id=12345, | ||
| text="Did you have your usual breakfast?", | ||
| message_id=777, | ||
| message_thread_id=None, | ||
| is_topic_message=False, | ||
| ) | ||
| from_user = SimpleNamespace(id="49334209", first_name="Alice", full_name="Alice") | ||
| query = AsyncMock() | ||
| query.data = data | ||
| query.message = prompt_message | ||
| query.from_user = from_user | ||
| return SimpleNamespace(callback_query=query), query | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_routed_callback_injects_mapped_text_with_full_context(monkeypatch): | ||
| monkeypatch.setenv("GATEWAY_ALLOW_ALL_USERS", "true") | ||
| adapter = _make_adapter() | ||
| update, query = _make_callback("log:breakfast:default") | ||
|
|
||
| await adapter._handle_callback_query(update, None) | ||
|
|
||
| query.answer.assert_awaited_once() | ||
| query.edit_message_text.assert_awaited_once() | ||
| handle_message = cast(AsyncMock, adapter.handle_message) | ||
| handle_message.assert_awaited_once() | ||
| event = handle_message.call_args.args[0] | ||
| assert event.text == "Yes, had the usual ✅" | ||
| assert event.source.chat_id == "12345" | ||
| assert event.source.user_id == "49334209" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_routed_callback_strips_buttons_and_shows_choice(monkeypatch): | ||
| monkeypatch.setenv("GATEWAY_ALLOW_ALL_USERS", "true") | ||
| adapter = _make_adapter() | ||
| update, query = _make_callback("log:breakfast:custom") | ||
|
|
||
| await adapter._handle_callback_query(update, None) | ||
|
|
||
| kwargs = query.edit_message_text.call_args.kwargs | ||
| assert kwargs["reply_markup"] is None | ||
| assert "Alice" in kwargs["text"] | ||
| assert "Had something different" in kwargs["text"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_unknown_callback_data_is_not_routed(monkeypatch): | ||
| monkeypatch.setenv("GATEWAY_ALLOW_ALL_USERS", "true") | ||
| adapter = _make_adapter() | ||
| update, query = _make_callback("log:unknown:choice") | ||
|
|
||
| await adapter._handle_callback_query(update, None) | ||
|
|
||
| handle_message = cast(AsyncMock, adapter.handle_message) | ||
| handle_message.assert_not_awaited() | ||
| query.answer.assert_not_awaited() | ||
| query.edit_message_text.assert_not_awaited() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_unauthorized_user_is_rejected(monkeypatch): | ||
| monkeypatch.delenv("GATEWAY_ALLOW_ALL_USERS", raising=False) | ||
| adapter = _make_adapter() | ||
| adapter._is_callback_user_authorized = MagicMock(return_value=False) | ||
| update, query = _make_callback("log:breakfast:default") | ||
|
|
||
| await adapter._handle_callback_query(update, None) | ||
|
|
||
| handle_message = cast(AsyncMock, adapter.handle_message) | ||
| handle_message.assert_not_awaited() | ||
| answer_kwargs = query.answer.call_args.kwargs | ||
| assert "not authorized" in answer_kwargs.get("text", "") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_edit_failure_still_routes_text(monkeypatch): | ||
| monkeypatch.setenv("GATEWAY_ALLOW_ALL_USERS", "true") | ||
| adapter = _make_adapter() | ||
| update, query = _make_callback("log:breakfast:default") | ||
| query.edit_message_text.side_effect = RuntimeError("message too old") | ||
|
|
||
| await adapter._handle_callback_query(update, None) | ||
|
|
||
| handle_message = cast(AsyncMock, adapter.handle_message) | ||
| handle_message.assert_awaited_once() | ||
| event = handle_message.call_args.args[0] | ||
| assert event.text == "Yes, had the usual ✅" | ||
|
|
||
|
|
||
| def test_invalid_routes_config_is_ignored(): | ||
| adapter = _make_adapter(routes="not-a-mapping") | ||
| assert adapter._callback_text_routes == {} | ||
|
|
||
|
|
||
| def test_oversized_and_empty_entries_are_dropped(): | ||
| adapter = _make_adapter( | ||
| routes={ | ||
| "k" * 65: "over the 64-byte callback_data limit", | ||
| "log:empty": " ", | ||
| "log:ok": "fine", | ||
| 42: "non-string key", | ||
| } | ||
| ) | ||
| assert adapter._callback_text_routes == {"log:ok": "fine"} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking: current main no longer contains
gateway/platforms/telegram.py; Telegram now runs fromplugins/platforms/telegram/adapter.py(migration476d8d9cc). Please port this initialization and the associated handler changes to the active plugin adapter.