Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can someone explain how to get the last n messages from a chat? #106

Open
divramod opened this issue Apr 27, 2021 · 0 comments
Open

can someone explain how to get the last n messages from a chat? #106

divramod opened this issue Apr 27, 2021 · 0 comments
Labels
TDLib Issue The issue is related to TDLib itself, not to the tdl bindings. Type: Question

Comments

@divramod
Copy link

hey,

i already have the following code get the last message of a chat.
but i like to get the last n messages from a chat.
can someone explain to me how to accomplish that?
i am fairly new to tdl.
the problem unfolds in the function getChatHistory().
thx in advance for possible help!

const { Client } = require('tdl')
const { TDLib } = require('tdl-tdlib-addon')

const client = new Client(new TDLib('/usr/lib/libtdjson.so'), {
  apiId: myId,
  apiHash: 'myHash',
})

async function getChatHistory(lChatId, lastMsgId) {
  return client.invoke({
    _: 'getChatHistory',
    chat_id: lChatId,
    from_message_id: lastMsgId,
    offset: -3,
    limit: 5,
    only_local: false
  })
}

async function main() {
  await client.connectAndLogin()
  const chats = await getChats(3)

  for (var i = 0, len = chats.chat_ids.length; i < len; i++) {
    const lChatId = chats.chat_ids[i]
    const lChat = await getChat(lChatId)
    console.log('') 
    console.log(`[CHAT] ${lChat.title} [id] ${lChat.id}`) 
    const lastMsgId = lChat.last_message.id
    try {
      const lMsgO = await getChatHistory(lChatId, lastMsgId)
      const lMsgA = lMsgO.messages
      for (var y = 0, lenM = lMsgA.length; y < lenM; y++) {
        const lMsg = lMsgA[y]
        console.log(`[MSG ${lMsg.id}] ${lMsg.content.text.text}`)
      } 
    } catch (e) {
      console.log('e', e) 
    } 
  }
e' }))
}

async function getChat(lChatId) {
  return client.invoke({
    _: 'getChat',
    chat_id: lChatId
  })
}

async function getChats(limit) {
  return client.invoke({
    _: 'getChats',
    offset_order: '9223372036854775807',
    offset_chat_id: 0,
    limit
  })
}

main()
@Bannerets Bannerets added the TDLib Issue The issue is related to TDLib itself, not to the tdl bindings. label Dec 12, 2021
@Bannerets Bannerets added Type: Question and removed TDLib Issue The issue is related to TDLib itself, not to the tdl bindings. labels Aug 27, 2022
@Bannerets Bannerets added the TDLib Issue The issue is related to TDLib itself, not to the tdl bindings. label Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TDLib Issue The issue is related to TDLib itself, not to the tdl bindings. Type: Question
Projects
None yet
Development

No branches or pull requests

2 participants