-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.ts
79 lines (67 loc) · 2.51 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
import { Conversation, PageResult } from '@sinch/sdk-core';
import {
getAppIdFromConfig,
getContactIdFromConfig, getConversationIdFromConfig,
getPrintFormat,
initConversationService,
printFullResponse,
} from '../../config';
const populateMessagesList = (
conversationPage: PageResult<Conversation.ConversationMessage>,
conversationList: Conversation.ConversationMessage[],
conversationDetailsList: string[],
) => {
conversationPage.data.map((message: Conversation.ConversationMessage) => {
conversationList.push(message);
conversationDetailsList.push(`${message.id} - ${message.accept_time}`);
});
};
(async () => {
console.log('*************************');
console.log('* Messages_ListMessages *');
console.log('*************************');
const appId = getAppIdFromConfig();
const contactId = getContactIdFromConfig();
const conversationId = getConversationIdFromConfig();
const requestData: Conversation.ListMessagesRequestData = {
app_id: appId,
contact_id: contactId,
conversation_id: conversationId,
channel: 'MESSENGER',
};
const conversationService = initConversationService();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await conversationService.messages.list(requestData);
const messageList: Conversation.ConversationMessage[] = [];
const messagesDetailsList: string[] = [];
// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateMessagesList(response, messageList, messagesDetailsList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(messagesDetailsList.length > 0
? 'List of messages:\n' + messagesDetailsList.join('\n')
: 'Sorry, no messages were found.');
} else {
printFullResponse(messageList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const message of conversationService.messages.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`${message.id} - ${message.accept_time}`);
} else {
console.log(message);
}
}
})();