tdl
tdl copied to clipboard
can someone explain how to get the last n messages from a chat?
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()