Skip to content

fix: handle MessageReplyStoryHeader without reply_to_msg_id attribute#128

Merged
chigwell merged 1 commit into
chigwell:mainfrom
akhromovRT:fix/message-reply-story-header-attribute
May 21, 2026
Merged

fix: handle MessageReplyStoryHeader without reply_to_msg_id attribute#128
chigwell merged 1 commit into
chigwell:mainfrom
akhromovRT:fix/message-reply-story-header-attribute

Conversation

@akhromovRT
Copy link
Copy Markdown
Contributor

Summary

Message.reply_to in Telethon can be either MessageReplyHeader (when the
message replies to another regular message) or MessageReplyStoryHeader
(when it replies to a story). Only the former carries reply_to_msg_id;
the latter has story_id + peer instead.

Before this fix, get_history() and list_messages() crashed with
AttributeError as soon as the requested range contained a
reply-to-story message:

File "telegram_mcp/tools/messages.py", line 1080, in get_history
    if msg.reply_to and msg.reply_to.reply_to_msg_id:
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'MessageReplyStoryHeader' object has no attribute
'reply_to_msg_id'

This made any sufficiently large history fetch unreliable for users whose
chats include story-reply messages — symptom is GEN-ERR-971 in MCP logs.

Fix

Use getattr(msg.reply_to, "reply_to_msg_id", None) to safely probe for
the attribute on whichever reply-header class is attached. Behavior for
ordinary MessageReplyHeader is unchanged.

Test plan

  • Manually reproduced the crash with get_history(chat_id, limit=200)
    on a chat containing a reply-to-story message
  • After patch — same call returns 200 messages successfully
  • Replies to regular messages still populate reply_to field in the
    output (verified on a chat with both reply types)

Telethon's Message.reply_to can be either MessageReplyHeader (replies to
regular messages) or MessageReplyStoryHeader (replies to stories). Only
the former has a reply_to_msg_id attribute; the latter has story_id and
peer instead.

Before this fix, get_history() and list_messages() crashed with
AttributeError as soon as the requested range contained a reply-to-story
message:

  File "telegram_mcp/tools/messages.py", line 1080, in get_history
    if msg.reply_to and msg.reply_to.reply_to_msg_id:
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  AttributeError: 'MessageReplyStoryHeader' object has no attribute
  'reply_to_msg_id'

This made any sufficiently large history fetch unreliable for users
whose chats contain story-reply messages.

Use getattr() to safely probe for the attribute on whichever
reply-header class is attached. The behavior for ordinary
MessageReplyHeader is unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@chigwell chigwell merged commit 479230d into chigwell:main May 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants