-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
50 lines (44 loc) · 1.3 KB
/
index.ts
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
import twilio from 'twilio';
import { MessageInstance } from 'twilio/lib/rest/api/v2010/account/message';
import papaparse from 'papaparse';
import fs from 'fs';
async function main(): Promise<void> {
const instance = twilio(process.env.ACCOUNT_SID, process.env.AUTH_TOKEN);
const dateSentAfter = new Date('2020-01-31 15:00:00-05');
const dateSentBefore = new Date('2020-01-31 21:30:00-05');
const inboundMessages = await new Promise<MessageInstance[]>(
(resolve, _reject) => {
let count = 0;
const result: MessageInstance[] = [];
instance.messages.each({
dateSentAfter,
dateSentBefore,
pageSize: 500,
callback: (item, _done) => {
count += 1;
if (count % 1000 === 0) console.log(`Completed ${count}`);
if (item.direction === 'inbound') {
result.push(item);
}
},
done: () => {
console.log(
`All done! Total count: ${count}. Inbound count: ${result.length}.`
);
resolve(result);
}
});
}
);
const output = papaparse.unparse(inboundMessages);
fs.writeFileSync('./messages.csv', output);
}
main()
.then(result => {
console.log(result);
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
});