Skip to content

Commit 001dcfd

Browse files
fix: create_poll and get_full_chat on basic groups
Two failures left over from the telethon 1.44 bump (#148). create_poll raised "Poll.__init__() missing 1 required positional argument: 'hash'" for every call. 1.44 made `hash` required; a poll being created has no cached server results, so send 0. get_full_chat raised "Cannot cast InputPeerChat to any kind of InputChannel" for basic (non-supergroup) groups, because it always issued channels.GetFullChannelRequest. Route basic groups to messages.GetFullChatRequest. ChatFull has no participants_count, so derive it from the member list; channels are unaffected. Verified against a live account: basic group returns {"title": "...", "participants_count": 3}, a channel is unchanged, and create_poll posts a working poll. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 89d6bad commit 001dcfd

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

telegram_mcp/tools/chats.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,17 +735,35 @@ async def get_full_chat(chat_id: Union[int, str], account: str = None) -> str:
735735
cl = get_client(account)
736736
await ensure_connected(cl)
737737
entity = await resolve_entity(chat_id, cl)
738-
full = await cl(functions.channels.GetFullChannelRequest(channel=entity))
738+
739+
# Basic ("legacy") groups are not channels: GetFullChannelRequest cannot
740+
# cast an InputPeerChat and raises TypeError. They are served by
741+
# messages.GetFullChatRequest instead.
742+
if isinstance(entity, (Chat, InputPeerChat)):
743+
basic_id = getattr(entity, "chat_id", None) or getattr(entity, "id", None)
744+
full = await cl(functions.messages.GetFullChatRequest(chat_id=basic_id))
745+
else:
746+
full = await cl(functions.channels.GetFullChannelRequest(channel=entity))
739747

740748
chat = full.chats[0] if full.chats else None
741749
full_chat = full.full_chat
742750

751+
# Channels carry participants_count on the full object; basic groups only
752+
# carry the member list, so count that instead.
753+
participants_count = getattr(full_chat, "participants_count", None)
754+
if participants_count is None:
755+
members = getattr(getattr(full_chat, "participants", None), "participants", None)
756+
if members is not None:
757+
participants_count = len(members)
758+
743759
result = {
744760
"id": get_marked_id(chat) if chat else None,
745761
"title": sanitize_name(getattr(chat, "title", None)) if chat else None,
746762
"username": getattr(chat, "username", None) if chat else None,
747-
"about": sanitize_user_content(full_chat.about or "", max_length=1024),
748-
"participants_count": getattr(full_chat, "participants_count", None),
763+
"about": sanitize_user_content(
764+
getattr(full_chat, "about", None) or "", max_length=1024
765+
),
766+
"participants_count": participants_count,
749767
"linked_chat_id": getattr(full_chat, "linked_chat_id", None),
750768
}
751769

telegram_mcp/tools/messages.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,9 @@ async def create_poll(
15421542
PollAnswer(text=TextWithEntities(text=option, entities=[]), option=bytes([i]))
15431543
for i, option in enumerate(options)
15441544
],
1545+
# Telethon 1.44 made `hash` a required argument on Poll. It caches
1546+
# server-side results, so a poll being created sends 0.
1547+
hash=0,
15451548
multiple_choice=multiple_choice,
15461549
quiz=quiz_mode,
15471550
public_voters=public_votes,

0 commit comments

Comments
 (0)