Telegram bot or API (messages.getHistory) get all chats messages history

116 Views Asked by At

I made a telegram bot, received a token from it and now made it an administrator. How can I get through the bot all messages from the moment it was added? There are requests in this group and I need to get their full text.

https://api.telegram.org/bot<token>/getUpdates

{
    "ok": true,
    "result": [
        {
            "update_id": 523188842,
            "message": {
                "message_id": 3,
                "from": {
                    "id": 1087333324,
                    "is_bot": true,
                    "first_name": "Group",
                    "username": "GroupBot"
                }, 

             "sender_chat": {
                "id": -10021999992,
                "title": TITLE",
                "type": "supergroup"
            },

}

The bot's privacy mode is turned off and after that I accepted it into the group

When I write messages there, I see them, but two telegram bots write to this group, and I need my bot to read their messages into the database of my site

I receive a response after a getUpdate request, it contains the chat id and title, but how can I get the full text of the messages?

P.S. I read that now in a telegram the bot can listen to the bot: Why doesn't my bot see messages from other bots? Bots talking to each other could potentially get stuck in unwelcome loops. To avoid this, we decided that bots will not be able to see messages from other bots regardless of mode.

Then I went the API route through libraries https://mtproto-core.js.org/docs/call-the-telegram-methods. But here when I get information about myself, I see everything except my chats https://disk.yandex.ru/i/YrBOZXV2wLLk5A . Мой запрос:

const api = require("./api");


(async () => {
    const resolvedPeer = await api.call('contacts.resolveUsername', {
        username: 'myname',
    });


    console.log(resolvedPeer);

    
    const channel = resolvedPeer.chats.find(
        (chat) => chat.id === resolvedPeer.peer.channel_id
    );

   

    const inputPeer = {
        _: 'inputPeerChannel',
        channel_id: channel.id,
        access_hash: channel.access_hash,
    };

    const LIMIT_COUNT = 10;
    const allMessages = [];

    const firstHistoryResult = await api.call('messages.getHistory', {
        peer: inputPeer,
        limit: LIMIT_COUNT,
    });

    const historyCount = firstHistoryResult.count;

    for (let offset = 0; offset < historyCount; offset += LIMIT_COUNT) {
        const history = await api.call('messages.getHistory', {
            peer: inputPeer,
            add_offset: offset,
            limit: LIMIT_COUNT,
        });

        allMessages.push(...history.messages);
    }

    console.log('allMessages:', allMessages);
0

There are 0 best solutions below