You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Telethon的GetMessagesRequest返回结果中提取消息文本?

Extracting Message Text from Telethon's GetMessagesRequest Result

Hey there! Let's walk through how to pull that specific message text out—since the Telethon team noted this boils down to basic Python object handling, it's simpler than you might think.

First, let's recap what's happening: when you call client(GetMessagesRequest(...)), the response you get is a container object (usually of type messages.Messages) that holds a list of message objects in its messages attribute.

Here's a step-by-step example with code:

  1. First, make your request as you did before:
from telethon.tl.functions.messages import GetMessagesRequest

# Assume you've already set up your client, defined your peer, and have pinnedMsgId
response = client(GetMessagesRequest(peers=[your_target_peer], id=[pinnedMsgId]))
  1. Next, grab the actual message object from the response. Since you're requesting a single pinned message, you can access the first item in the messages list:
# Get the first (and only, in this case) message from the response
target_msg = response.messages[0]
  1. Finally, extract the text. Telethon's Message object stores the raw message content in the message attribute. If you want text parsed with formatting entities (like links or bold), you can use text instead—but for your plain "test message test" string, message will work perfectly:
message_text = target_msg.message
# Or, if you need parsed text: message_text = target_msg.text

print(message_text)  # This will output: test message test

A quick note: If you ever request multiple messages, you'd loop through response.messages to process each one individually, but since you're targeting a single pinned message, accessing the index [0] is the way to go.

内容的提问来源于stack exchange,提问作者Py_Dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:13:47