GetMessages

GramJS provides two firendly methods for getting messages. GetMessages which returns a totalList (a normal array with a .total attribute) of messages and iterMessages which returns a generator.

getMessages(entity: EntityLike, params: messageMethods.IterMessagesParams)
iterMessages(entity: EntityLike, params: messageMethods.IterMessagesParams)
export interface IterMessagesParams {
    limit?: number;
    offsetDate?: DateLike;
    offsetId?: number;
    maxId?: number;
    minId?: number;
    addOffset?: number;
    search?: string;
    filter?: Api.TypeMessagesFilter | Api.TypeMessagesFilter[];
    fromUser?: EntityLike;
    waitTime?: number;
    ids?: number | number[];
    reverse?: boolean;
    replyTo?: number;
}

Example

(async () => {
    const client = new TelegramClient(new StringSession(stringSession), apiId, apiHash, {})
    await client.connect();
    const msgs = await client.getMessages("me", {
        limit: 10,
    });
    console.log("the total number of msgs are", msgs.total);
    console.log("what we got is ", msgs.length);
    for (const msg of msgs) {
        //console.log("msg is",msg); // this line is very verbose but helpful for debugging
        console.log("msg text is : ", msg.text);
    }
})();

The return type is not Api.Message but is the custom type custom.Message

Last updated