-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-mailbox.js
210 lines (180 loc) · 6.85 KB
/
export-mailbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const HelpScout = require("helpscout-mailbox-api");
const axios = require("axios");
const fs = require("fs-extra");
const path = require("path");
// Add this sleep function at the top of the file
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const clientId = process.env.HELPSCOUT_APP_ID;
const clientSecret = process.env.HELPSCOUT_APP_SECRET;
// Initialize HelpScout client
const helpscout = new HelpScout({
clientId,
clientSecret,
authenticationFlow: "clientCredentials",
});
async function exportHelpScoutData() {
try {
// Authenticate and get the access token
const tokens = await helpscout.getTokens();
helpscout.setCredentials(tokens);
// List all mailboxes
await sleep(100);
const mailboxesResponse = await helpscout.mailboxes.list();
if (
!mailboxesResponse ||
!mailboxesResponse.data ||
!mailboxesResponse.data._embedded ||
!mailboxesResponse.data._embedded.mailboxes
) {
throw new Error("Unexpected mailboxes response structure");
}
const mailboxes = mailboxesResponse.data._embedded.mailboxes;
for (const mailbox of mailboxes) {
let page = 1;
// Since the script will get interrupted if your machine loses network access
// let page = 61;
// If the script gets interrupted, you'll want to skip exporting any mailboxes you've completed
// if (mailbox.name === "Spruce Sales" || mailbox.name === "Spruce Support" || mailbox.name === "Number Ports") {
// continue;
// }
const mailboxDir = path.join(__dirname, "mailbox-exports", mailbox.name);
await fs.ensureDir(mailboxDir);
// List conversations in the mailbox
console.log(
`Fetching conversations for mailbox: ${mailbox.name} (ID: ${mailbox.id})`
);
let hasMoreConversations = true;
while (hasMoreConversations) {
await sleep(100);
const timestamp = new Date().toISOString();
console.log(
`[${timestamp}] Fetching page ${page} for mailbox: ${mailbox.name}`
);
const conversationsResponse = await helpscout.conversations.list({
mailbox: mailbox.id,
page,
status: "all",
});
if (
!conversationsResponse ||
!conversationsResponse.data ||
!conversationsResponse.data._embedded ||
!conversationsResponse.data._embedded.conversations
) {
console.warn(
`No conversations found for mailbox ${mailbox.name} on page ${page}`
);
break;
}
const conversations =
conversationsResponse.data._embedded.conversations;
for (const conversation of conversations) {
const conversationDir = path.join(
mailboxDir,
`conversation_${conversation.id}`
);
if (await fs.pathExists(conversationDir)) {
console.log(`Skipping existing conversation: ${conversation.id}`);
continue;
}
await fs.ensureDir(conversationDir);
// Save conversation details to a JSON file
await fs.writeJson(
path.join(conversationDir, "conversation.json"),
conversation,
{ spaces: 2 }
);
// List threads in the conversation
console.log(`Fetching threads for conversation: ${conversation.id}`);
await sleep(100);
const threadsResponse = await helpscout.conversations.threads.list(
conversation.id
);
if (
!threadsResponse ||
!threadsResponse.data ||
!threadsResponse.data._embedded ||
!threadsResponse.data._embedded.threads
) {
console.warn(
`No threads found for conversation ${conversation.id}`
);
continue;
}
const threads = threadsResponse.data._embedded.threads;
for (const thread of threads) {
// Save thread details to a JSON file
await fs.writeJson(
path.join(conversationDir, `thread_${thread.id}.json`),
thread,
{ spaces: 2 }
);
// Download and save attachments
if (
thread._embedded &&
thread._embedded.attachments &&
thread._embedded.attachments.length > 0
) {
for (const attachment of thread._embedded.attachments) {
try {
await sleep(100);
const attachmentResponse =
await helpscout.conversations.attachments.getData(
conversation.id,
attachment.id
);
let attachmentData = attachmentResponse.data.data;
if (!attachmentData) {
throw new Error(
"Attachment data is empty or in an unexpected format"
);
}
// The data is likely base64 encoded, so we need to decode it
const buffer = Buffer.from(attachmentData, "base64");
const attachmentPath = path.join(
conversationDir,
attachment.filename
);
await fs.writeFile(attachmentPath, buffer);
console.log(
`Successfully downloaded attachment: ${attachment.filename}`
);
} catch (attachmentError) {
console.error(
`Failed to download attachment ${attachment.id} for thread ${thread.id}: ${attachmentError.message}`
);
// Create an error text file instead of the attachment
const errorFileName = `ERROR_${attachment.id}_${attachment.filename}.txt`;
const errorFilePath = path.join(
conversationDir,
errorFileName
);
const errorMessage = `Failed to download attachment ${attachment.id}: ${attachmentError.message}`;
await fs.writeFile(errorFilePath, errorMessage);
console.log(
`Created error file for attachment: ${errorFileName}`
);
}
}
}
await sleep(100);
}
}
// Check if there are more pages
const pageInfo = conversationsResponse.data.page;
hasMoreConversations = pageInfo.number < pageInfo.totalPages;
page = pageInfo.number + 1;
}
}
console.log("Export completed successfully.");
} catch (error) {
console.error("Error exporting data:", error);
if (error.response) {
console.error(
"API response:",
JSON.stringify(error.response.data, null, 2)
);
}
}
}
exportHelpScoutData();