Skip to content

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Feb 23, 2024
1 parent f757134 commit 3c8b782
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 44 deletions.
1 change: 1 addition & 0 deletions examples/simple-examples/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 51 additions & 44 deletions examples/simple-examples/README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions examples/simple-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions examples/simple-examples/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
ConversationRecentMessage,
ListRecentConversationsRequestData,
PageResult,
} from '@sinch/sdk-core';
import { getAppIdFromConfig, getPrintFormat, initClient, printFullResponse } from '../../config';

const populateConversationsList = (
conversationPage: PageResult<ConversationRecentMessage>,
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);
}
}

})();
21 changes: 21 additions & 0 deletions examples/simple-examples/src/conversation/events/delete.ts
Original file line number Diff line number Diff line change
@@ -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);

})();
20 changes: 20 additions & 0 deletions examples/simple-examples/src/conversation/events/get.ts
Original file line number Diff line number Diff line change
@@ -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);

})();
76 changes: 76 additions & 0 deletions examples/simple-examples/src/conversation/events/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ConversationEvent, ListEventsRequestData, PageResult } from '@sinch/sdk-core';
import {
getContactIdFromConfig,
getConversationIdFromConfig,
getPrintFormat,
initClient,
printFullResponse,
} from '../../config';

const populateEventsList = (
eventPage: PageResult<ConversationEvent>,
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);
}
}

})();
27 changes: 27 additions & 0 deletions examples/simple-examples/src/conversation/messages/update.ts
Original file line number Diff line number Diff line change
@@ -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);

})();
3 changes: 3 additions & 0 deletions examples/webhooks/src/services/conversation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3c8b782

Please sign in to comment.