Last active
October 28, 2024 16:50
-
-
Save satyrius/976a5568223db392e8b8b1bd18d9983d to your computer and use it in GitHub Desktop.
Export Intercom conversations/chats entire history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
const H = require("highland"); | |
const axios = require("axios"); | |
const fs = require("fs").promises; | |
const exportDirectory = "./export"; | |
const apiUrl = "https://api.intercom.io"; | |
// config axios for the authorized API request | |
const apiClient = axios.create({ | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded", | |
Authorization: `Bearer ${process.env.INTERCOM_API_KEY}`, | |
"Intercom-Version": "2.10", | |
}, | |
}); | |
async function listConversations(after) { | |
const params = { per_page: 100 }; | |
if (after) params.starting_after = after; | |
const res = await apiClient.get(`${apiUrl}/conversations`, { params }); | |
console.log(res.data.pages); | |
const next = res.data.pages?.next?.starting_after; | |
return { ids: res.data.conversations.map(({ id }) => id), next }; | |
} | |
async function getConversation(conversationId) { | |
const res = await apiClient.get(`${apiUrl}/conversations/${conversationId}`); | |
return res.data; | |
} | |
async function exportConversations() { | |
await fs.mkdir(exportDirectory, { recursive: true }); | |
const listStream = H(async (push) => { | |
let after = null; | |
while (true) { | |
try { | |
const { ids, next: nextAfter } = await listConversations(after); | |
if (!ids.length) break; // Stop when no more data | |
ids.forEach((id) => push(null, id)); // Push ids to stream | |
if (!nextAfter) break; // Stop when no more pages | |
after = nextAfter; | |
} catch (err) { | |
push(err); | |
break; | |
} | |
} | |
push(null, H.nil); // End of stream | |
}); | |
await listStream | |
.map((id) => | |
H(async (push) => { | |
// get conversation data and write to file | |
try { | |
const res = await getConversation(id); | |
const fileName = `${exportDirectory}/${id}.json`; | |
await fs.writeFile(fileName, JSON.stringify(res, null, 2)); | |
console.log(id); | |
push(null, id); | |
push(null, H.nil); | |
} catch (err) { | |
console.error("Something went wrong: ", err); | |
push(err); | |
} | |
}) | |
) | |
.parallel(10) | |
.stopOnError((err) => { | |
console.error("Something went wrong: ", err); | |
}) | |
.collect() | |
.toPromise(Promise); | |
} | |
exportConversations() | |
.then(() => { | |
console.log("Finished."); | |
process.exit(0); | |
}) | |
.catch((error) => { | |
console.error(error.message); | |
process.exit(1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"axios": "^1.5.0", | |
"dotenv": "^16.3.1", | |
"highland": "^2.13.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this! It saved me a lot of time, and it works perfectly.