-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.ts
90 lines (77 loc) · 2.99 KB
/
list.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
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
import {
getPrintFormat,
getRecipientPhoneNumberFromConfig,
initSmsServiceWithServicePlanId,
printFullResponse,
} from '../../config';
import {
Sms,
PageResult,
} from '@sinch/sdk-core';
const populateInboundMessagesList = (
inboundMessagesListPage: PageResult<Sms.InboundMessageResponse>,
fullInboundMessagesList: Sms.InboundMessageResponse[],
inboundMessagesList: string[],
) => {
fullInboundMessagesList.push(...inboundMessagesListPage.data);
inboundMessagesListPage.data.map((inboundMessage) => {
inboundMessagesList.push(`Inbound message ID: ${inboundMessage.id} - Type: ${inboundMessage.type} - From: ${inboundMessage.from}`);
});
};
(async () => {
console.log('***********************');
console.log('* ListInboundMessages *');
console.log('***********************');
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
oneWeekAgo.setHours(0, 0, 0, 0);
const recipientPhoneNumber = getRecipientPhoneNumberFromConfig();
const requestData: Sms.ListInboundMessagesRequestData= {
start_date: oneWeekAgo,
to: recipientPhoneNumber,
};
const smsService = initSmsServiceWithServicePlanId();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response;
try {
response = await smsService.inbounds.list(requestData);
} catch (error) {
console.error(`ERROR: Impossible to get the list the inbound messages for the numbers ${requestData.to}`);
throw error;
}
const fullInboundMessagesList: Sms.InboundMessageResponse[] = [];
const inboundMessagesList: string[] = [];
// Loop on all the pages to get all the batches
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateInboundMessagesList(response, fullInboundMessagesList, inboundMessagesList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(inboundMessagesList.length > 0
? 'List of inbound messages: ' + JSON.stringify(inboundMessagesList, null, 2)
: 'Sorry, no inbound messages were found.');
} else {
printFullResponse(fullInboundMessagesList);
}
if (fullInboundMessagesList.length > 0) {
console.log(`You may want to update your .env file with the following value: INBOUND_ID=${fullInboundMessagesList[0].id}`);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const inboundMessage of smsService.inbounds.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`Inbound message ID: ${inboundMessage.id} - Type: ${inboundMessage.type} - From: ${inboundMessage.from}`);
} else {
console.log(inboundMessage);
}
}
})();