From 3c8b78294c93c88b9cce544068400483a0414405 Mon Sep 17 00:00:00 2001 From: Antoine Sein Date: Fri, 23 Feb 2024 18:55:46 +0100 Subject: [PATCH] Add examples --- examples/simple-examples/.env.template | 1 + examples/simple-examples/README.md | 95 ++++++++++--------- examples/simple-examples/package.json | 6 ++ examples/simple-examples/src/config.ts | 4 + .../conversation/conversation/injectEvent.ts | 35 +++++++ .../conversation/conversation/listRecent.ts | 74 +++++++++++++++ .../src/conversation/events/delete.ts | 21 ++++ .../src/conversation/events/get.ts | 20 ++++ .../src/conversation/events/list.ts | 76 +++++++++++++++ .../src/conversation/messages/update.ts | 27 ++++++ .../src/services/conversation.service.ts | 3 + 11 files changed, 318 insertions(+), 44 deletions(-) create mode 100644 examples/simple-examples/src/conversation/conversation/injectEvent.ts create mode 100644 examples/simple-examples/src/conversation/conversation/listRecent.ts create mode 100644 examples/simple-examples/src/conversation/events/delete.ts create mode 100644 examples/simple-examples/src/conversation/events/get.ts create mode 100644 examples/simple-examples/src/conversation/events/list.ts create mode 100644 examples/simple-examples/src/conversation/messages/update.ts diff --git a/examples/simple-examples/.env.template b/examples/simple-examples/.env.template index 103f3c21..284ca213 100644 --- a/examples/simple-examples/.env.template +++ b/examples/simple-examples/.env.template @@ -34,6 +34,7 @@ CONVERSATION_CONTACT_ID=contact_id to fill with one of the contacts created by t MESSENGER_USER_ID=identity on the MESSENGER channel (can be found on a desktop by selecting a user: the user id will be in the URL) MESSENGER_TOKEN=static_token to define credentials for a MESSENGER channel CONVERSATION_ID=conversation_id to fill with one of the conversations created with the Conversation API +EVENT_ID=event_id to fill with a sent event MESSAGE_ID=message_id to fill with one of the messages sent or injected with the Conversation API TEMPLATE_ID=template_id to fill with one of the templates created with the Templates API (v1 or v2) WEBHOOK_ID=webhook_id to fill with one of the webhooks created with the Conversation API or the Dashboard diff --git a/examples/simple-examples/README.md b/examples/simple-examples/README.md index 3516b29d..975aa2c9 100644 --- a/examples/simple-examples/README.md +++ b/examples/simple-examples/README.md @@ -65,6 +65,7 @@ CONVERSATION_CONTACT_ID=contact_id to fill with one of the contacts created by t MESSENGER_USER_ID=identity on the MESSENGER channel (can be found on a desktop by selecting a user: the user id will be in the URL) MESSENGER_TOKEN=static_token to define credentials for a MESSENGER channel CONVERSATION_ID=conversation_id to fill with one of the conversations created with the Conversation API +EVENT_ID=event_id to fill with a sent event MESSAGE_ID=message_id to fill with one of the messages sent or injected with the Conversation API TEMPLATE_ID=template_id to fill with one of the templates created with the Templates API (v1 or v2) WEBHOOK_ID=webhook_id to fill with one of the webhooks created with the Conversation API or the Dashboard @@ -173,48 +174,54 @@ yarn run numbers:regions:list ### Conversation -| Service | Sample application name and location | Required parameters | -|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| -| App | [./src/conversation/app/create.ts](./src/conversation/app/create.ts) | `MESSENGER_TOKEN` | -| | [./src/conversation/app/delete.ts](./src/conversation/app/delete.ts) | `CONVERSATION_APP_ID` | -| | [./src/conversation/app/get.ts](./src/conversation/app/get.ts) | `CONVERSATION_APP_ID` | -| | [./src/conversation/app/list.ts](./src/conversation/app/list.ts) | | -| | [./src/conversation/app/update.ts](./src/conversation/app/update.ts) | `CONVERSATION_APP_ID` | -| Capability | [./src/conversation/capability/lookup.ts](./src/conversation/capability/lookup.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | -| Contact | [./src/conversation/contact/create.ts](./src/conversation/contact/create.ts) | `CONVERSATION_APP_ID` + `MESSENGER_USER_ID` + `PHONE_NUMBER` | -| | [./src/conversation/contact/delete.ts](./src/conversation/contact/delete.ts) | `CONVERSATION_CONTACT_ID` | -| | [./src/conversation/contact/get.ts](./src/conversation/contact/get.ts) | `CONVERSATION_CONTACT_ID` | -| | [./src/conversation/contact/getChannelProfile.ts](./src/conversation/contact/getChannelProfile.ts) | `CONVERSATION_CONTACT_ID` + `MESSENGER_USER_ID` | -| | [./src/conversation/contact/list.ts](./src/conversation/contact/list.ts) | | -| | [./src/conversation/contact/merge.ts](./src/conversation/contact/merge.ts) | `PHONE_NUMBER` | -| | [./src/conversation/contact/update.ts](./src/conversation/contact/update.ts) | `CONVERSATION_CONTACT_ID` | -| Conversation | [./src/conversation/conversation/create.ts](./src/conversation/conversation/create.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | -| | [./src/conversation/conversation/delete.ts](./src/conversation/conversation/delete.ts) | `CONVERSATION_ID` | -| | [./src/conversation/conversation/get.ts](./src/conversation/conversation/get.ts) | `CONVERSATION_ID` | -| | [./src/conversation/conversation/injectMessage.ts](./src/conversation/conversation/injectMessage.ts) | `CONVERSATION_ID` + `MESSENGER_USER_ID`+ `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | -| | [./src/conversation/conversation/list.ts](./src/conversation/conversation/list.ts) | `CONVERSATION_APP_ID` | -| | [./src/conversation/conversation/stop.ts](./src/conversation/conversation/stop.ts) | `CONVERSATION_ID` | -| | [./src/conversation/conversation/update.ts](./src/conversation/conversation/update.ts) | `CONVERSATION_ID` | -| Events | [./src/conversation/events/send.ts](./src/conversation/events/send.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | -| Messages | [./src/conversation/messages/delete.ts](./src/conversation/messages/delete.ts) | `MESSAGE_ID` | -| | [./src/conversation/messages/get.ts](./src/conversation/messages/get.ts) | `MESSAGE_ID` | -| | [./src/conversation/messages/list.ts](./src/conversation/messages/list.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` + `CONVERSATION_ID` | -| | [./src/conversation/messages/send.ts](./src/conversation/messages/send.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | -| TemplatesV1 | [./src/conversation/templates-v1/create.ts](./src/conversation/templates-v1/create.ts) | | -| | [./src/conversation/templates-v1/delete.ts](./src/conversation/templates-v1/delete.ts) | `TEMPLATE_ID` | -| | [./src/conversation/templates-v1/get.ts](./src/conversation/templates-v1/get.ts) | `TEMPLATE_ID` | -| | [./src/conversation/templates-v1/list.ts](./src/conversation/templates-v1/list.ts) | | -| | [./src/conversation/templates-v1/update.ts](./src/conversation/templates-v1/update.ts) | `TEMPLATE_ID` | -| TemplatesV2 | [./src/conversation/templates-v2/create.ts](./src/conversation/templates-v2/create.ts) | | -| | [./src/conversation/templates-v2/delete.ts](./src/conversation/templates-v2/delete.ts) | `TEMPLATE_ID` | -| | [./src/conversation/templates-v2/get.ts](./src/conversation/templates-v2/get.ts) | `TEMPLATE_ID` | -| | [./src/conversation/templates-v2/list.ts](./src/conversation/templates-v2/list.ts) | | -| | [./src/conversation/templates-v2/list-translations.ts](./src/conversation/templates-v2/list-translations.ts) | `TEMPLATE_ID` | -| | [./src/conversation/templates-v2/update.ts](./src/conversation/templates-v2/update.ts) | `TEMPLATE_ID` | -| Transcoding | [./src/conversation/transcoding/transcode.ts](./src/conversation/transcoding/transcode.ts) | `CONVERSATION_APP_ID` | -| Webhooks | [./src/conversation/webhooks/create.ts](./src/conversation/webhooks/create.ts) | `CONVERSATION_APP_ID` + `WEBHOOK_TARGET` | -| | [./src/conversation/webhooks/delete.ts](./src/conversation/webhooks/delete.ts) | `WEBHOOK_ID` | -| | [./src/conversation/webhooks/get.ts](./src/conversation/webhooks/get.ts) | `WEBHOOK_ID` | -| | [./src/conversation/webhooks/list.ts](./src/conversation/webhooks/list.ts) | `CONVERSATION_APP_ID` | -| | [./src/conversation/webhooks/update.ts](./src/conversation/webhooks/update.ts) | `CONVERSATION_APP_ID` + `WEBHOOK_ID` | +| Service | Sample application name and location | Required parameters | +|--------------|--------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| +| App | [./src/conversation/app/create.ts](./src/conversation/app/create.ts) | `MESSENGER_TOKEN` | +| | [./src/conversation/app/delete.ts](./src/conversation/app/delete.ts) | `CONVERSATION_APP_ID` | +| | [./src/conversation/app/get.ts](./src/conversation/app/get.ts) | `CONVERSATION_APP_ID` | +| | [./src/conversation/app/list.ts](./src/conversation/app/list.ts) | | +| | [./src/conversation/app/update.ts](./src/conversation/app/update.ts) | `CONVERSATION_APP_ID` | +| Capability | [./src/conversation/capability/lookup.ts](./src/conversation/capability/lookup.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | +| Contact | [./src/conversation/contact/create.ts](./src/conversation/contact/create.ts) | `CONVERSATION_APP_ID` + `MESSENGER_USER_ID` + `PHONE_NUMBER` | +| | [./src/conversation/contact/delete.ts](./src/conversation/contact/delete.ts) | `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/contact/get.ts](./src/conversation/contact/get.ts) | `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/contact/getChannelProfile.ts](./src/conversation/contact/getChannelProfile.ts) | `CONVERSATION_CONTACT_ID` + `MESSENGER_USER_ID` | +| | [./src/conversation/contact/list.ts](./src/conversation/contact/list.ts) | | +| | [./src/conversation/contact/merge.ts](./src/conversation/contact/merge.ts) | `PHONE_NUMBER` | +| | [./src/conversation/contact/update.ts](./src/conversation/contact/update.ts) | `CONVERSATION_CONTACT_ID` | +| Conversation | [./src/conversation/conversation/create.ts](./src/conversation/conversation/create.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/conversation/delete.ts](./src/conversation/conversation/delete.ts) | `CONVERSATION_ID` | +| | [./src/conversation/conversation/get.ts](./src/conversation/conversation/get.ts) | `CONVERSATION_ID` | +| | [./src/conversation/conversation/injectEvent.ts](./src/conversation/conversation/injectEvent.ts) | `CONVERSATION_ID` + `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/conversation/injectMessage.ts](./src/conversation/conversation/injectMessage.ts) | `CONVERSATION_ID` + `MESSENGER_USER_ID`+ `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/conversation/list.ts](./src/conversation/conversation/list.ts) | `CONVERSATION_APP_ID` | +| | [./src/conversation/conversation/listRecent.ts](./src/conversation/conversation/listRecent.ts) | `CONVERSATION_APP_ID` | +| | [./src/conversation/conversation/stop.ts](./src/conversation/conversation/stop.ts) | `CONVERSATION_ID` | +| | [./src/conversation/conversation/update.ts](./src/conversation/conversation/update.ts) | `CONVERSATION_ID` | +| Events | [./src/conversation/events/send.ts](./src/conversation/events/send.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/events/get.ts](./src/conversation/events/get.ts) | `EVENT_ID` | +| | [./src/conversation/events/list.ts](./src/conversation/events/list.ts) | `CONVERSATION_ID` + `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/events/delete.ts](./src/conversation/events/delete.ts) | `EVENT_ID` | +| Messages | [./src/conversation/messages/delete.ts](./src/conversation/messages/delete.ts) | `MESSAGE_ID` | +| | [./src/conversation/messages/get.ts](./src/conversation/messages/get.ts) | `MESSAGE_ID` | +| | [./src/conversation/messages/list.ts](./src/conversation/messages/list.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` + `CONVERSATION_ID` | +| | [./src/conversation/messages/send.ts](./src/conversation/messages/send.ts) | `CONVERSATION_APP_ID` + `CONVERSATION_CONTACT_ID` | +| | [./src/conversation/messages/update.ts](./src/conversation/messages/update.ts) | `MESSAGE_ID` | +| TemplatesV1 | [./src/conversation/templates-v1/create.ts](./src/conversation/templates-v1/create.ts) | | +| | [./src/conversation/templates-v1/delete.ts](./src/conversation/templates-v1/delete.ts) | `TEMPLATE_ID` | +| | [./src/conversation/templates-v1/get.ts](./src/conversation/templates-v1/get.ts) | `TEMPLATE_ID` | +| | [./src/conversation/templates-v1/list.ts](./src/conversation/templates-v1/list.ts) | | +| | [./src/conversation/templates-v1/update.ts](./src/conversation/templates-v1/update.ts) | `TEMPLATE_ID` | +| TemplatesV2 | [./src/conversation/templates-v2/create.ts](./src/conversation/templates-v2/create.ts) | | +| | [./src/conversation/templates-v2/delete.ts](./src/conversation/templates-v2/delete.ts) | `TEMPLATE_ID` | +| | [./src/conversation/templates-v2/get.ts](./src/conversation/templates-v2/get.ts) | `TEMPLATE_ID` | +| | [./src/conversation/templates-v2/list.ts](./src/conversation/templates-v2/list.ts) | | +| | [./src/conversation/templates-v2/list-translations.ts](./src/conversation/templates-v2/list-translations.ts) | `TEMPLATE_ID` | +| | [./src/conversation/templates-v2/update.ts](./src/conversation/templates-v2/update.ts) | `TEMPLATE_ID` | +| Transcoding | [./src/conversation/transcoding/transcode.ts](./src/conversation/transcoding/transcode.ts) | `CONVERSATION_APP_ID` | +| Webhooks | [./src/conversation/webhooks/create.ts](./src/conversation/webhooks/create.ts) | `CONVERSATION_APP_ID` + `WEBHOOK_TARGET` | +| | [./src/conversation/webhooks/delete.ts](./src/conversation/webhooks/delete.ts) | `WEBHOOK_ID` | +| | [./src/conversation/webhooks/get.ts](./src/conversation/webhooks/get.ts) | `WEBHOOK_ID` | +| | [./src/conversation/webhooks/list.ts](./src/conversation/webhooks/list.ts) | `CONVERSATION_APP_ID` | +| | [./src/conversation/webhooks/update.ts](./src/conversation/webhooks/update.ts) | `CONVERSATION_APP_ID` + `WEBHOOK_ID` | diff --git a/examples/simple-examples/package.json b/examples/simple-examples/package.json index ab7efb5b..fd7ddb2b 100644 --- a/examples/simple-examples/package.json +++ b/examples/simple-examples/package.json @@ -23,15 +23,21 @@ "conversation:messages:send": "ts-node src/conversation/messages/send.ts", "conversation:messages:get": "ts-node src/conversation/messages/get.ts", "conversation:messages:list": "ts-node src/conversation/messages/list.ts", + "conversation:messages:update": "ts-node src/conversation/messages/update.ts", "conversation:messages:delete": "ts-node src/conversation/messages/delete.ts", "conversation:conversation:create": "ts-node src/conversation/conversation/create.ts", "conversation:conversation:get": "ts-node src/conversation/conversation/get.ts", "conversation:conversation:list": "ts-node src/conversation/conversation/list.ts", + "conversation:conversation:listRecent": "ts-node src/conversation/conversation/listRecent.ts", + "conversation:conversation:injectEvent": "ts-node src/conversation/conversation/injectEvent.ts", "conversation:conversation:injectMessage": "ts-node src/conversation/conversation/injectMessage.ts", "conversation:conversation:update": "ts-node src/conversation/conversation/update.ts", "conversation:conversation:stop": "ts-node src/conversation/conversation/stop.ts", "conversation:conversation:delete": "ts-node src/conversation/conversation/delete.ts", "conversation:events:send": "ts-node src/conversation/events/send.ts", + "conversation:events:get": "ts-node src/conversation/events/get.ts", + "conversation:events:list": "ts-node src/conversation/events/list.ts", + "conversation:events:delete": "ts-node src/conversation/events/delete.ts", "conversation:transcoding:transcode": "ts-node src/conversation/transcoding/transcode.ts", "conversation:capability:lookup": "ts-node src/conversation/capability/lookup.ts", "conversation:webhooks:create": "ts-node src/conversation/webhooks/create.ts", diff --git a/examples/simple-examples/src/config.ts b/examples/simple-examples/src/config.ts index 3e4c7b56..6925e777 100644 --- a/examples/simple-examples/src/config.ts +++ b/examples/simple-examples/src/config.ts @@ -141,6 +141,10 @@ export const getTemplateIdFromConfig = () => { return readVariable('TEMPLATE_ID'); }; +export const getEventIdFromConfig = () => { + return readVariable('EVENT_ID'); +}; + const readVariable = ( name: string): string => { const value = process.env[name]; if (!value) { diff --git a/examples/simple-examples/src/conversation/conversation/injectEvent.ts b/examples/simple-examples/src/conversation/conversation/injectEvent.ts new file mode 100644 index 00000000..4af09178 --- /dev/null +++ b/examples/simple-examples/src/conversation/conversation/injectEvent.ts @@ -0,0 +1,35 @@ +import { InjectEventRequestData } from '@sinch/sdk-core'; +import { + getContactIdFromConfig, + getConversationIdFromConfig, + initClient, + printFullResponse, +} from '../../config'; + + +(async () => { + console.log('****************************'); + console.log('* Conversation_InjectEvent *'); + console.log('****************************'); + + const conversationId = getConversationIdFromConfig(); + const contactId = getContactIdFromConfig(); + + const requestData: InjectEventRequestData= { + conversation_id: conversationId, + injectConversationEventRequestBody: { + app_event: { + composing_event: {}, + }, + accept_time: new Date(), + conversation_id: conversationId, + contact_id: contactId, + }, + }; + + const sinchClient = initClient(); + const response = await sinchClient.conversation.conversation.injectEvent(requestData); + + printFullResponse(response); + +})(); diff --git a/examples/simple-examples/src/conversation/conversation/listRecent.ts b/examples/simple-examples/src/conversation/conversation/listRecent.ts new file mode 100644 index 00000000..f2fdd738 --- /dev/null +++ b/examples/simple-examples/src/conversation/conversation/listRecent.ts @@ -0,0 +1,74 @@ +import { + ConversationRecentMessage, + ListRecentConversationsRequestData, + PageResult, +} from '@sinch/sdk-core'; +import { getAppIdFromConfig, getPrintFormat, initClient, printFullResponse } from '../../config'; + +const populateConversationsList = ( + conversationPage: PageResult, + conversationList: ConversationRecentMessage[], + conversationDetailsList: string[], +) => { + conversationPage.data.map((recentConversation: ConversationRecentMessage) => { + conversationList.push(recentConversation); + conversationDetailsList.push(`${recentConversation.conversation?.id} - ${recentConversation.conversation?.active_channel}\n - Latest message: ${recentConversation.last_message?.accept_time}`); + }); +}; + +(async () => { + console.log('****************************************'); + console.log('* Conversation_ListRecentConversations *'); + console.log('****************************************'); + + const appId = getAppIdFromConfig(); + + const requestData: ListRecentConversationsRequestData = { + only_active: true, + app_id: appId, + order: 'ASC', + }; + + const sinchClient = initClient(); + + // ---------------------------------------------- + // Method 1: Fetch the data page by page manually + // ---------------------------------------------- + let response = await sinchClient.conversation.conversation.listRecent(requestData); + + const conversationList: ConversationRecentMessage[] = []; + const conversationDetailsList: string[] = []; + + // Loop on all the pages to get all the active numbers + let reachedEndOfPages = false; + while (!reachedEndOfPages) { + populateConversationsList(response, conversationList, conversationDetailsList); + if (response.hasNextPage) { + response = await response.nextPage(); + } else { + reachedEndOfPages = true; + } + } + + const printFormat = getPrintFormat(process.argv); + + if (printFormat === 'pretty') { + console.log(conversationDetailsList.length > 0 + ? 'List of conversations:\n' + conversationDetailsList.join('\n') + : 'Sorry, no conversations were found.'); + } else { + printFullResponse(conversationList); + } + + // --------------------------------------------------------------------- + // Method 2: Use the iterator and fetch data on more pages automatically + // --------------------------------------------------------------------- + for await (const recentConversation of sinchClient.conversation.conversation.listRecent(requestData)) { + if (printFormat === 'pretty') { + console.log(`${recentConversation.conversation?.id} - ${recentConversation.conversation?.active_channel}\n - Latest message: ${recentConversation.last_message?.accept_time}`); + } else { + console.log(recentConversation); + } + } + +})(); diff --git a/examples/simple-examples/src/conversation/events/delete.ts b/examples/simple-examples/src/conversation/events/delete.ts new file mode 100644 index 00000000..8bf353f1 --- /dev/null +++ b/examples/simple-examples/src/conversation/events/delete.ts @@ -0,0 +1,21 @@ +import { DeleteEventRequestData } from '@sinch/sdk-core'; +import { getEventIdFromConfig, initClient, printFullResponse } from '../../config'; + + +(async () => { + console.log('**********************'); + console.log('* Events_DeleteEvent *'); + console.log('**********************'); + + const eventId = getEventIdFromConfig(); + + const requestData: DeleteEventRequestData = { + event_id: eventId, + }; + + const sinchClient = initClient(); + const response = await sinchClient.conversation.events.delete(requestData); + + printFullResponse(response); + +})(); diff --git a/examples/simple-examples/src/conversation/events/get.ts b/examples/simple-examples/src/conversation/events/get.ts new file mode 100644 index 00000000..2a04eb0e --- /dev/null +++ b/examples/simple-examples/src/conversation/events/get.ts @@ -0,0 +1,20 @@ +import { GetEventRequestData } from '@sinch/sdk-core'; +import { getEventIdFromConfig, initClient, printFullResponse } from '../../config'; + +(async () => { + console.log('*******************'); + console.log('* Events_GetEvent *'); + console.log('*******************'); + + const eventId = getEventIdFromConfig(); + + const requestData: GetEventRequestData = { + event_id: eventId, + }; + + const sinchClient = initClient(); + const response = await sinchClient.conversation.events.get(requestData); + + printFullResponse(response); + +})(); diff --git a/examples/simple-examples/src/conversation/events/list.ts b/examples/simple-examples/src/conversation/events/list.ts new file mode 100644 index 00000000..b5f90209 --- /dev/null +++ b/examples/simple-examples/src/conversation/events/list.ts @@ -0,0 +1,76 @@ +import { ConversationEvent, ListEventsRequestData, PageResult } from '@sinch/sdk-core'; +import { + getContactIdFromConfig, + getConversationIdFromConfig, + getPrintFormat, + initClient, + printFullResponse, +} from '../../config'; + +const populateEventsList = ( + eventPage: PageResult, + eventList: ConversationEvent[], + eventDetailsList: string[], +) => { + eventPage.data.map((event: ConversationEvent) => { + eventList.push(event); + eventDetailsList.push(`${event.id} - ${event.accept_time}`); + }); +}; + +(async () => { + console.log('*********************'); + console.log('* Events_ListEvents *'); + console.log('*********************'); + + const contactId = getContactIdFromConfig(); + const conversationId = getConversationIdFromConfig(); + + const requestData: ListEventsRequestData = { + contact_id: contactId, + conversation_id: conversationId, + }; + + const sinchClient = initClient(); + + // ---------------------------------------------- + // Method 1: Fetch the data page by page manually + // ---------------------------------------------- + let response = await sinchClient.conversation.events.list(requestData); + + const eventsList: ConversationEvent[] = []; + const eventsDetailsList: string[] = []; + + // Loop on all the pages to get all the active numbers + let reachedEndOfPages = false; + while (!reachedEndOfPages) { + populateEventsList(response, eventsList, eventsDetailsList); + if (response.hasNextPage) { + response = await response.nextPage(); + } else { + reachedEndOfPages = true; + } + } + + const printFormat = getPrintFormat(process.argv); + + if (printFormat === 'pretty') { + console.log(eventsDetailsList.length > 0 + ? 'List of events:\n' + eventsDetailsList.join('\n') + : 'Sorry, no events were found.'); + } else { + printFullResponse(eventsList); + } + + // --------------------------------------------------------------------- + // Method 2: Use the iterator and fetch data on more pages automatically + // --------------------------------------------------------------------- + for await (const event of sinchClient.conversation.events.list(requestData)) { + if (printFormat === 'pretty') { + console.log(`${event.id} - ${event.accept_time}`); + } else { + console.log(event); + } + } + +})(); diff --git a/examples/simple-examples/src/conversation/messages/update.ts b/examples/simple-examples/src/conversation/messages/update.ts new file mode 100644 index 00000000..88c96267 --- /dev/null +++ b/examples/simple-examples/src/conversation/messages/update.ts @@ -0,0 +1,27 @@ +import { UpdateMessageRequestData } from '@sinch/sdk-core'; +import { + getMessageIdFromConfig, + initClient, + printFullResponse, +} from '../../config'; + +(async () => { + console.log('**************************'); + console.log('* Messages_UpdateMessage *'); + console.log('**************************'); + + const messageId = getMessageIdFromConfig(); + + const requestData: UpdateMessageRequestData = { + message_id: messageId, + updateMessageRequestBody: { + metadata: 'Updated metadata', + }, + }; + + const sinchClient = initClient(); + const response = await sinchClient.conversation.messages.update(requestData); + + printFullResponse(response); + +})(); diff --git a/examples/webhooks/src/services/conversation.service.ts b/examples/webhooks/src/services/conversation.service.ts index 1f47542d..6d2d1244 100644 --- a/examples/webhooks/src/services/conversation.service.ts +++ b/examples/webhooks/src/services/conversation.service.ts @@ -197,6 +197,9 @@ export class ConversationService { console.log(`Additional data:\n${JSON.stringify(channelEvent.additional_data, null, 2)}`); } break; + case 'RECORD_NOTIFICATION': + console.log('\n## RECORD_NOTIFICATION'); + break; case 'UNSUPPORTED': console.log('\n## UNSUPPORTED') const unsupportedCallback = event.unsupported_callback;