Skip to content

Commit

Permalink
DEVEXP-276: Add example files (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored Jan 30, 2024
1 parent faca3fe commit 22cda81
Show file tree
Hide file tree
Showing 37 changed files with 1,265 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/simple-examples/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ VERIFICATION_ID=verification-id to fill with the verification started with the V
VERIFICATION_IDENTITY=verification-identity to fill with the identity of the user
VERIFICATION_REFERENCE=verification-reference to add when starting a verification or getting its status
VOICE_CALLBACK_URL=URL found in the Voice dashboard to handle webhooks
CONVERSATION_APP_ID=app_id to fill with one of the conversation app created with the Conversation API or in the Dashboard
CONVERSATION_CONTACT_ID=contact_id to fill with one off the contacts
MESSENGER_TOKEN=static token to define credentials for a Messenger application
MESSENGER_USER_ID=Id of a user using messenger (can be found on a desktop by selecting a user: the user id will be in the URL)
CONVERSATION_ID=conversation_id to fill with one of the conversation create with the Conversation API
MESSAGE_ID=message_id to fill from a sent or injected message
WEBHOOK_ID=webhook_id to fill from a webhook creation with the Conversation API or the Dashboard
WEBHOOK_TARGET=URL of the webhook
31 changes: 31 additions & 0 deletions examples/simple-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@
"build": "yarn run clean && yarn run compile",
"clean": "rimraf dist tsconfig.tsbuildinfo",
"compile": "tsc --build --verbose",
"conversation:app:create": "ts-node src/conversation/app/create.ts",
"conversation:app:get": "ts-node src/conversation/app/get.ts",
"conversation:app:list": "ts-node src/conversation/app/list.ts",
"conversation:app:update": "ts-node src/conversation/app/update.ts pretty",
"conversation:app:delete": "ts-node src/conversation/app/delete.ts",
"conversation:contact:create": "ts-node src/conversation/contact/create.ts",
"conversation:contact:get": "ts-node src/conversation/contact/get.ts",
"conversation:contact:list": "ts-node src/conversation/contact/list.ts",
"conversation:contact:update": "ts-node src/conversation/contact/update.ts",
"conversation:contact:merge": "ts-node src/conversation/contact/merge.ts",
"conversation:contact:getChannelProfile": "ts-node src/conversation/contact/getChannelProfile.ts",
"conversation:contact:delete": "ts-node src/conversation/contact/delete.ts",
"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: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: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: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",
"conversation:webhooks:get": "ts-node src/conversation/webhooks/get.ts",
"conversation:webhooks:list": "ts-node src/conversation/webhooks/list.ts",
"conversation:webhooks:update": "ts-node src/conversation/webhooks/update.ts",
"conversation:webhooks:delete": "ts-node src/conversation/webhooks/delete.ts",
"numbers:regions:list": "ts-node src/numbers/regions/list.ts",
"numbers:available:list": "ts-node src/numbers/available/list.ts",
"numbers:available:checkAvailability": "ts-node src/numbers/available/checkAvailability.ts",
Expand Down
32 changes: 32 additions & 0 deletions examples/simple-examples/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ export const getVoiceCallBackUrl = (): string => {
return readVariable('VOICE_CALLBACK_URL');
};

export const getAppIdFromConfig = () => {
return readVariable('CONVERSATION_APP_ID');
};

export const getContactIdFromConfig = () => {
return readVariable('CONVERSATION_CONTACT_ID');
};

export const getMessengerTokenFormConfig = () => {
return readVariable('MESSENGER_TOKEN');
};

export const getMessengerUserIdFromConfig = () => {
return readVariable('MESSENGER_USER_ID');
};

export const getConversationIdFromConfig = () => {
return readVariable('CONVERSATION_ID');
};

export const getMessageIdFromConfig = () => {
return readVariable('MESSAGE_ID');
};

export const getWebhookIdFromConfig = () => {
return readVariable('WEBHOOK_ID');
};

export const getWebhookTargetFromConfig = () => {
return readVariable('WEBHOOK_TARGET');
};

const readVariable = ( name: string): string => {
const value = process.env[name];
if (!value) {
Expand Down
41 changes: 41 additions & 0 deletions examples/simple-examples/src/conversation/app/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { CreateAppRequestData } from '@sinch/sdk-core';
import { getMessengerTokenFormConfig, getPrintFormat, initClient, printFullResponse } from '../../config';

(async () => {
console.log('*****************');
console.log('* App_CreateApp *');
console.log('*****************');

const messengerToken = getMessengerTokenFormConfig();

const requestData: CreateAppRequestData = {
appCreateRequestBody: {
display_name: 'New app created with the Node.js SDK',
channel_credentials: [
{
channel: 'MESSENGER',
static_token: {
token: messengerToken,
},
},
],
conversation_metadata_report_view: 'FULL',
retention_policy: {
retention_type: 'CONVERSATION_EXPIRE_POLICY',
ttl_days: 60,
},
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.create(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`New app created with the id '${response.id}'`);
} else {
printFullResponse(response);
}
console.log(`You may want to update your .env file with the following value: CONVERSATION_APP_ID=${response.id}`);
})();
20 changes: 20 additions & 0 deletions examples/simple-examples/src/conversation/app/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DeleteAppRequestData } from '@sinch/sdk-core';
import { getAppIdFromConfig, initClient, printFullResponse } from '../../config';

(async () => {
console.log('*****************');
console.log('* App_DeleteApp *');
console.log('*****************');

const appId = getAppIdFromConfig();

const requestData: DeleteAppRequestData = {
app_id: appId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.delete(requestData);

printFullResponse(response);

})();
26 changes: 26 additions & 0 deletions examples/simple-examples/src/conversation/app/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { GetAppRequestData } from '@sinch/sdk-core';
import { getAppIdFromConfig, getPrintFormat, initClient, printFullResponse } from '../../config';

(async () => {
console.log('**************');
console.log('* App_GetApp *');
console.log('**************');

const appId = getAppIdFromConfig();

const requestData: GetAppRequestData = {
app_id: appId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.get(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The app with the id '${response.id}' is named '${response.display_name}'`);
} else {
printFullResponse(response);
}

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

(async () => {
console.log('****************');
console.log('* App_ListApps *');
console.log('****************');

const requestData: ListAppsRequestData= {};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.list(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(response.apps
? response.apps.map((app) => `'${app.id}': ${app.display_name}`).join('\n')
: 'No Conversation Applications were found');
} else {
printFullResponse(response);
}

})();
47 changes: 47 additions & 0 deletions examples/simple-examples/src/conversation/app/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { UpdateAppRequestData } from '@sinch/sdk-core';
import {
getAppIdFromConfig, getMessengerTokenFormConfig,
getPrintFormat,
initClient,
printFullResponse,
} from '../../config';

(async () => {
console.log('*****************');
console.log('* App_UpdateApp *');
console.log('*****************');

const appId = getAppIdFromConfig();

const requestData: UpdateAppRequestData = {
app_id: appId,
update_mask: ['display_name', 'conversation_metadata_report_view'],
appUpdateRequestBody: {
display_name: 'Updated name by the Node.js SDK',
conversation_metadata_report_view: 'NONE',
channel_credentials: [
{
channel: 'MESSENGER',
static_token: {
token: 'new token (invalid) - should not be updated thanks to the mask',
},
},
],

},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.app.update(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`App updated! New name: '${response.display_name}'.`);
const token = getMessengerTokenFormConfig();
console.log(`Verifying the token (it should be unchanged):\nOLD: '${token}'\nNEW: '${response.channel_credentials?.[0].static_token?.token}'`);
} else {
printFullResponse(response);
}

})();
27 changes: 27 additions & 0 deletions examples/simple-examples/src/conversation/capability/lookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { LookupCapabilityRequestData } from '@sinch/sdk-core';
import { getAppIdFromConfig, getContactIdFromConfig, initClient, printFullResponse } from '../../config';

(async () => {
console.log('******************************');
console.log('* Capability_QueryCapability *');
console.log('******************************');

const appId = getAppIdFromConfig();
const contactId = getContactIdFromConfig();

const requestData: LookupCapabilityRequestData = {
lookupCapabilityRequestBody: {
app_id: appId,
recipient: {
contact_id: contactId,
},
request_id: 'myPersonalId_' + new Date().getTime(),
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.capability.lookup(requestData);

printFullResponse(response);

})();
50 changes: 50 additions & 0 deletions examples/simple-examples/src/conversation/contact/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { CreateContactRequestData } from '@sinch/sdk-core';
import {
getAppIdFromConfig,
getMessengerUserIdFromConfig,
getPhoneNumberFromConfig,
getPrintFormat,
initClient,
printFullResponse,
} from '../../config';

(async () => {
console.log('*************************');
console.log('* Contact_CreateContact *');
console.log('*************************');

const phoneNumber = getPhoneNumberFromConfig();
const messengerUserId = getMessengerUserIdFromConfig();
const appId = getAppIdFromConfig();

const requestData: CreateContactRequestData = {
contactCreateRequestBody: {
display_name: 'New contact created with the Node.js SDK',
channel_identities: [
{
identity: messengerUserId,
channel: 'MESSENGER',
app_id:appId,
},
{
identity: phoneNumber,
channel: 'WHATSAPP',
},
],
channel_priority: ['WHATSAPP'],
language: 'EN_US',
},
};

const sinchClient = initClient();
const response = await sinchClient.conversation.contact.create(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`New contact created with the id '${response.id}'`);
} else {
printFullResponse(response);
}

})();
20 changes: 20 additions & 0 deletions examples/simple-examples/src/conversation/contact/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DeleteContactRequestData } from '@sinch/sdk-core';
import { getContactIdFromConfig, initClient, printFullResponse } from '../../config';

(async () => {
console.log('*************************');
console.log('* Contact_DeleteContact *');
console.log('*************************');

const contactId = getContactIdFromConfig();

const requestData: DeleteContactRequestData = {
contact_id: contactId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.contact.delete(requestData);

printFullResponse(response);

})();
26 changes: 26 additions & 0 deletions examples/simple-examples/src/conversation/contact/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { GetContactRequestData } from '@sinch/sdk-core';
import { getContactIdFromConfig, getPrintFormat, initClient, printFullResponse } from '../../config';

(async () => {
console.log('**********************');
console.log('* Contact_GetContact *');
console.log('**********************');

const contactId = getContactIdFromConfig();

const requestData: GetContactRequestData = {
contact_id: contactId,
};

const sinchClient = initClient();
const response = await sinchClient.conversation.contact.get(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The contact with the id '${response.id}' is named '${response.display_name}'`);
} else {
printFullResponse(response);
}

})();
Loading

0 comments on commit 22cda81

Please sign in to comment.