如何通过电视马拉松从链接的频道讨论组获取消息

发布于 2025-01-20 16:19:46 字数 567 浏览 0 评论 0原文

我已经成功从频道检索消息。 我用 iter_messages 函数来做到这一点 但是,消息对象不包含评论,只有用户写的评论。 对象中有一个channel_id,这似乎是链接的组。但该群组没有像 t.me/xxx 这样的 URL。有人有解决办法吗?

以下是 JSON 对象的摘录。

 "replies": {
  "_": "MessageReplies",
  "replies": 8,
  "replies_pts": 17846,
  "comments": true,
  "recent_repliers": [
    {
      "_": "PeerUser",
      "user_id": 57135752
    },
    {
      "_": "PeerUser",
      "user_id": 564589817
    },
    {
      "_": "PeerUser",
      "user_id": 888542547
    }
  ],
  "channel_id": 1484030956,
  "max_id": 13402,
  "read_max_id": null
},
  

I have already successfully retrieved messages from channels.
I do that with the iter_messages function
However, the message object does not contain the comments, only the users wrote the comments.
In the object is a channel_id, this seems to be the linked group. But the group doesn't have a URL like t.me/xxx. Does anyone have an approach for a solution?

Here is an excerpt from the object as JSON.

 "replies": {
  "_": "MessageReplies",
  "replies": 8,
  "replies_pts": 17846,
  "comments": true,
  "recent_repliers": [
    {
      "_": "PeerUser",
      "user_id": 57135752
    },
    {
      "_": "PeerUser",
      "user_id": 564589817
    },
    {
      "_": "PeerUser",
      "user_id": 888542547
    }
  ],
  "channel_id": 1484030956,
  "max_id": 13402,
  "read_max_id": null
},
  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2025-01-27 16:19:46

如果您手动加入该组,则可以使用。

async for message in client.iter_messages(peer):
    if message.replies:
        channel_peer = types.InputChannel(message.replies.channel_id, 0)
        chat = await client.get_entity(channel_peer)

但更好的方法是获取完整的频道请求,这样您就可以获取频道本身并作为实体进行聊天(也许可以加入它?),然后查找其中是否有对频道帖子的回复。

chat_full = await client(functions.channels.GetFullChannelRequest(peer))
channel = chat_full.chats[0]

chat = channel_full.linked_chat_id
if chat:
    chat = chat_full.chats[-1]

async for message in client.iter_messages(peer):
    msg_data = {
        "id": message.id,
        "date": message.date,
        "message": message.message,
    }
    if chat:
        msg_data["replies"] = client.get_messages(chat, reply_to=message.id)
        

该代码未经测试,但我正在研究它 o_0

If you had joined to this group manually, It can work.

async for message in client.iter_messages(peer):
    if message.replies:
        channel_peer = types.InputChannel(message.replies.channel_id, 0)
        chat = await client.get_entity(channel_peer)

But better way is to get full channel request, so you can get a channel itself and chat as an entities (and maybe join it?), then find if any of them are replies to channel post.

chat_full = await client(functions.channels.GetFullChannelRequest(peer))
channel = chat_full.chats[0]

chat = channel_full.linked_chat_id
if chat:
    chat = chat_full.chats[-1]

async for message in client.iter_messages(peer):
    msg_data = {
        "id": message.id,
        "date": message.date,
        "message": message.message,
    }
    if chat:
        msg_data["replies"] = client.get_messages(chat, reply_to=message.id)
        

This code is untested, but I am working on it o_0

夜夜流光相皎洁 2025-01-27 16:19:46

最后,我找到了一个工作正常的解决方案。
当您具有特定消息ID时,您可以设置标志Reply_to。然后,您将获得频道帖子的评论。

def get_comments(client: TelegramClient, channel: str, message_id: int):
    async def crawl_comments():
        async for message in client.iter_messages(channel, reply_to=message_id):
            print(message.text)  # only comment
            full_comment_obj = message.to_dict()  # in JSON-Format
            print(full_comment_obj)

    with client:
       client.loop.run_until_complete(crawl_comments())

Finally i found a solution which is working fine.
When you have the specific message id you can set the flag reply_to. Then you get the comments of the channel posts.

def get_comments(client: TelegramClient, channel: str, message_id: int):
    async def crawl_comments():
        async for message in client.iter_messages(channel, reply_to=message_id):
            print(message.text)  # only comment
            full_comment_obj = message.to_dict()  # in JSON-Format
            print(full_comment_obj)

    with client:
       client.loop.run_until_complete(crawl_comments())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文