使用telethon获取特定ID后的电报频道消息

时间:2018-09-16 06:41:54

标签: telethon

我想在ID(例如1245)之后获取特定的频道帖子。我写这段代码:

<?php
    $xml_str = "
    <images>
        <image>
            <position>0</position>                  
        </image>
        <image1>
            <position>10</position>
        </image1>
    </images>";
                    // turn the xml into a multidimentional array
            $ob = simplexml_load_string($xml_str);
            $json = json_encode($ob);
            $my_array = json_decode($json, true);

            print_r($my_array);
                    // flatten it
            $result = array();
            array_walk_recursive($my_array, function($v, $k) use (&$result){ $i = ""; for (; isset($result[$k."$i"]); $i++); $result[$k."$i"] = $v; });

            print_r($result);
?>

但这给了我最后100条信息。即使将# After connection to session in cli variable last_post = cli( GetHistoryRequest( peer=entity, limit=100, offset_date=None, add_offset=0, hash=0 ) ) 参数传递给GetHistoryRequest,我也将从较早的消息传递到较旧的消息。如何获取相反顺序的消息?

2 个答案:

答案 0 :(得分:0)

您必须先阅读文档,然后再提出问题。 您只需使用iter_messages参数即可完成此操作,

next_post = cli.iter_messages(
                entity,
                limit=5,
                min_id=your_post_id,
                reverse=True
            )

使用reverse参数可以按相反的顺序获取帖子。

答案 1 :(得分:0)

以下是使用漫游器的示例(代码的客户端版本几乎相同)

bot = TelegramClient('bot', API_ID, API_HASH).start(bot_token=BOT_TOKEN)
result = await bot(functions.messages.GetMessagesRequest(id=[message_id]))
message = result.messages[0]
print(message.text)

进行聊天/完整聊天

@bot.on(events.NewMessage)
async def any_message_arrived_handler(event):
    chat = await event.get_chat()
    full_chat = await bot(functions.messages.GetFullChatRequest(
        chat_id=chat.id
    ))

最好看的地方是the documentation。只需在页面上搜索messages,您就会发现包含示例的所需内容。

相关问题