Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/bub/channels/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def _message_type(message: Message) -> str:
return "unknown"


def _slice_utf16(text: str, offset: int, length: int) -> str:
"""Slice ``text`` using Telegram Bot API UTF-16 code-unit offsets."""
if offset < 0 or length <= 0:
return ""
encoded = text.encode("utf-16-le")
return encoded[offset * 2 : (offset + length) * 2].decode("utf-16-le", errors="ignore")


class BubMessageFilter(filters.MessageFilter):
GROUP_CHAT_TYPES: ClassVar[set[str]] = {"group", "supergroup"}

Expand Down Expand Up @@ -95,13 +103,14 @@ def filter(self, message: Message) -> bool | dict[str, list[Any]] | None:
return False

def _mentions_bot(self, message: Message, bot_id: int, bot_username: str) -> bool:
content = self._content(message).lower()
raw_content = self._content(message)
content = raw_content.lower()
mentions_by_keyword = "bub" in content or bool(bot_username and f"@{bot_username}" in content)

entities = [*(getattr(message, "entities", None) or ()), *(getattr(message, "caption_entities", None) or ())]
for entity in entities:
if entity.type == "mention" and bot_username:
mention_text = content[entity.offset : entity.offset + entity.length]
mention_text = _slice_utf16(raw_content, entity.offset, entity.length)
if mention_text.lower() == f"@{bot_username}":
return True
continue
Expand Down Expand Up @@ -286,7 +295,7 @@ async def start_typing(self, chat_id: str) -> AsyncGenerator[None, None]:
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
del self._typing_tasks[chat_id]
self._typing_tasks.pop(chat_id, None)

async def _typing_loop(self, chat_id: str) -> None:
while True:
Expand Down Expand Up @@ -349,7 +358,7 @@ def _extract_links(message: Message) -> list[str] | None:
elif entity.type == "url":
offset = getattr(entity, "offset", 0)
length = getattr(entity, "length", 0)
candidate = source_text[offset : offset + length].strip()
candidate = _slice_utf16(source_text, offset, length).strip()
url = candidate or None

if url and url not in links:
Expand Down
58 changes: 58 additions & 0 deletions tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2017,3 +2017,61 @@ async def complete(text: str) -> list[str]:
assert await complete(",model relay:o") == ["relay:o3"]
assert await complete(",model openai:gpt-4o extra") == []
assert fetched == ["available for openai:gpt-4o", "relay"]


@pytest.mark.asyncio
async def test_telegram_parser_extracts_url_after_emoji_utf16_offset() -> None:
parser = TelegramMessageParser()
text = "See 🔥 https://example.com/path"
prefix_utf16_len = len("See 🔥 ".encode("utf-16-le")) // 2
url = "https://example.com/path"
message = SimpleNamespace(
message_id=1,
text=text,
caption=None,
entities=[SimpleNamespace(type="url", offset=prefix_utf16_len, length=len(url))],
caption_entities=None,
from_user=SimpleNamespace(id=7, username="alice", full_name="Alice", is_bot=False),
date=None,
)

_, metadata = await parser.parse(message)

assert metadata["links"] == [url]


def test_bub_message_filter_handles_mention_after_emoji_utf16_offset() -> None:
bot = SimpleNamespace(id=1, username="HelperBot")
text = "🎉 @HelperBot hi"
offset = len("🎉 ".encode("utf-16-le")) // 2
message = SimpleNamespace(
chat=SimpleNamespace(type="group"),
text=text,
caption=None,
entities=[SimpleNamespace(type="mention", offset=offset, length=len("@HelperBot"))],
caption_entities=[],
reply_to_message=None,
get_bot=lambda: bot,
)

assert BubMessageFilter().filter(message) is True


@pytest.mark.asyncio
async def test_telegram_channel_stop_while_typing_does_not_raise_key_error(load_config) -> None:
_load_channel_config(load_config, telegram_value="test-token")
channel = TelegramChannel(lambda message: None)

async def send_chat_action(chat_id: str, action: str) -> None:
return None

channel._app = SimpleNamespace(
bot=SimpleNamespace(send_chat_action=send_chat_action),
updater=None,
stop=_async_return(None),
shutdown=_async_return(None),
)

async with channel.start_typing("42"):
await channel.stop()