Skip to content

Instantly share code, notes, and snippets.

@spasiu
Created April 28, 2020 19:04
Show Gist options
  • Save spasiu/5599cd53a237b4baf46bf508ab608a73 to your computer and use it in GitHub Desktop.
Save spasiu/5599cd53a237b4baf46bf508ab608a73 to your computer and use it in GitHub Desktop.
Get events for a particular conversation
/*
provide a unix timestamp in seconds
a conversation ID
and a count for number of calls to the logs API to make
results stored in global variable "results"
*/
const TIMESTAMP_SECONDS = '';
const CONVERSATION_ID = '';
const COUNT = 100;
async function getLogs(n, id, ts=undefined, hasMore=true, events=[]) {
const appId = location.pathname.split('/')[2];
ts = ts || Date.now() / 1000;
if (!hasMore) return events;
if (n === 0) return events;
const response = await fetch(`https://app.smooch.io/webapi/apps/${appId}/logs?before=${ts}`, {
'credentials':`include`,
'headers':{
'accept':`application/json`,
'accept-language':`en-US,en;q=0.9`,
'content-type':`application/json`,
'sec-fetch-mode':`cors`,
'sec-fetch-site':`same-origin`,
'x-requested-with':`XMLHttpRequest`
}
});
const data = await response.json();
const eventBatch = data.events || [];
const lastEvent = eventBatch.slice(-1).pop() || {};
const relevantEvents = eventBatch.filter(event => event.details.conversationId === id);
if (relevantEvents.length > 0) {
console.log('\nHIT!\n');
} else {
console.log('miss');
}
return getLogs(n - 1, id, lastEvent.timestamp, data.hasMore, events.concat(relevantEvents))
}
getLogs(COUNT, CONVERSATION_ID, TIMESTAMP_SECONDS)
.then(events => results = events)
.then(() => console.log(results));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment