getThreadHistory parameters adjustments #453
getThreadHistory parameters adjustments #453
Conversation
Awesome! Thanks a lot for fixing this. |
What? When it started ignoring end? I was able to pull all messages from thread using only start and end about a month ago :/ |
@ravkr Can you try it again with the old api.getThreadHistory(threadID, 5, 10, undefined, (err, history)=>{
history.forEach((msg, index)=>{
console.log(msg.body);
});
app.quit();
}); The code above basically ignores the |
If I remember good, the |
yeah... |
Unfortunately, this doesn't seem to work on my side.
|
@bilyanhadzhi can you show us your code? |
@ravkr Sure. I'm trying to export all messages from a huge group chat (>32,000 messages) to a JSON file. const login = require('facebook-chat-api');
const jsonfile = require('jsonfile');
const credentials = {
email: process.env.FB_EMAIL,
password: process.env.FB_PASSWORD,
};
const file = '/home/billy/Coding/projects/z-class-chat-analysis/src/messages.json';
const threadId = 814129402005312;
let timestamp = undefined;
let allMessages = [];
const writeNextHistory = api => {
api.getThreadHistory(threadId, 3, timestamp, (err, history) => {
if (err) {
console.error(err);
}
if (timestamp !== undefined) {
history.pop();
}
console.log('timestamp', timestamp);
history.forEach(message => console.log(`${message.body}: timestamp is ${message.timestamp}`));
console.log();
allMessages = history.concat(allMessages);
timestamp = history[0].timestamp;
});
};
login(credentials, (err, api) => {
if (err) {
console.error(err);
}
writeNextHistory(api);
writeNextHistory(api);
}); The result is a little something like this:
As you can see, it's using the message with timestamp of |
@bilyanhadzhi The API works asynchronously. So the second In effect, you will have something like this: login(credentials, (err, api) => {
if (err) {
console.error(err);
}
writeNextHistory(api, () => writeNextHistory(api));
}); |
you are calling // ...
const writeNextHistory = api => {
console.log('timestamp', timestamp);
api.getThreadHistory(threadId, 3, timestamp, (err, history) => {
// ... you will see that both calls are made when your second call to |
Ah, I see now. So, I guess if I wanted to call this function again and again till I've loaded some arbitrary number of messages or reached the beginning of history, I guess I'd write some sort of recursive callback? Thank you very much again, guys, I'll ask again if need be. |
Addressing #262
Since
thread_info.php
pretty much ignores the offset data, I thought it would be better to remove that option from the api as it may cause some confusion (as it did to me).So I changed getThreadHistory to only require
amount
andtimestamp
to get the thread history. I also provided an example of how to load batches of messages in order (like scrolling up on a messenger client) without the need ofoffset
functionality. However this means you cannot load messages in history without loading the messages before (more recent) it. Although you couldn't do this before anyhow.