Skip to content

Commit

Permalink
DEVEXP-559: Add snippets for Voice API
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Sep 16, 2024
1 parent 1591a88 commit c57348d
Show file tree
Hide file tree
Showing 17 changed files with 400 additions and 2 deletions.
3 changes: 1 addition & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export default [...compat.extends('eslint:recommended', 'google', 'prettier'), {
ignorePattern: '^import.+|test',
}],

'new-cap': 'off',
'no-unused-vars': 'off'
'new-cap': 'off'
},
}];
24 changes: 24 additions & 0 deletions snippets/voice/applications/assign-numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const phoneNumber = 'YOUR_Sinch_phone_number_with_voice_capability';
const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';

/** @type {Voice.AssignNumbersRequestData} */
const requestData = {
assignNumbersRequestBody: {
numbers: [
phoneNumber,
],
applicationkey: applicationKey,
capability: 'voice',
},
};

await voiceService.applications.assignNumbers(requestData);

console.log('The phone numbers have been assigned to the application successfully.');
};
17 changes: 17 additions & 0 deletions snippets/voice/applications/get-callback-urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';

/** @type {Voice.GetCallbackURLsRequestData} */
const requestData = {
applicationkey: applicationKey,
};

const response = await voiceService.applications.getCallbackURLs(requestData);

console.log(`Callback URLs:\n${JSON.stringify(response, null, 2)}`);
};
13 changes: 13 additions & 0 deletions snippets/voice/applications/list-numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

/** @type {Voice.GetNumbersRequestData} */
const requestData = {};

const response = await voiceService.applications.listNumbers(requestData);

console.log(`Numbers list:\n${JSON.stringify(response, null, 2)}`);
};
17 changes: 17 additions & 0 deletions snippets/voice/applications/query-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const phoneNumber = 'the_phone_number';

/** @type {Voice.QueryNumberRequestData} */
const requestData = {
number: phoneNumber,
};

const response = await voiceService.applications.queryNumber(requestData);

console.log(`Phone number information:\n${JSON.stringify(response, null, 2)}`);
};
22 changes: 22 additions & 0 deletions snippets/voice/applications/unassign-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const phoneNumber = 'YOUR_Sinch_phone_number_with_voice_capability';
const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';

/** @type {Voice.UnassignNumberRequestData} */
const requestData = {
unassignNumbersRequestBody: {
number: phoneNumber,
applicationkey: applicationKey,
capability: 'voice',
},
};

await voiceService.applications.unassignNumber(requestData);

console.log('The phone numbers have been unassigned from the application successfully.');
};
23 changes: 23 additions & 0 deletions snippets/voice/applications/update-callback-urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';

/** @type {Voice.UpdateCallbackURLsRequestData} */
const requestData = {
applicationkey: applicationKey,
updateCallbacksRequestBody: {
url: {
primary: 'https://new-primary-callback-url.com',
fallback: 'https://new-fallback-callback-url.com',
},
},
};

await voiceService.applications.updateCallbackURLs(requestData);

console.log('The callback URLs have been successfully updated.');
};
29 changes: 29 additions & 0 deletions snippets/voice/callouts/conference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const conferenceId = 'YOUR_conference_ID';
const recipientPhoneNumber = 'the_phone_number_to_call';
const callingNumber = 'the_calling_number';

/** @type {Voice.ConferenceCalloutRequestData} */
const requestData = {
conferenceCalloutRequestBody: {
method: 'conferenceCallout',
conferenceCallout: {
conferenceId,
destination: {
type: 'number',
endpoint: recipientPhoneNumber,
},
cli: callingNumber,
},
},
};

const response = await voiceService.callouts.conference(requestData);

console.log(`Callout response: \n${JSON.stringify(response, null, 2)}`);
};
66 changes: 66 additions & 0 deletions snippets/voice/callouts/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const recipientPhoneNumber = 'the_phone_number_to_call';
const callingNumber = 'the_calling_number';
const callbackUrl= 'the_PIE_callback_URL';

/** @type {Voice.CustomCalloutRequestData} */
const requestData = {
customCalloutRequestBody: {
method: 'customCallout',
customCallout: {
destination: {
type: 'number',
endpoint: recipientPhoneNumber,
},
cli: callingNumber,
ice: Voice.customCalloutHelper.formatIceResponse(
Voice.iceActionHelper.connectPstn({
number: recipientPhoneNumber,
cli: callingNumber,
}),
Voice.iceInstructionHelper.say('Welcome to Sinch', 'en-US/male'),
),
ace: Voice.customCalloutHelper.formatAceResponse(
Voice.aceActionHelper.runMenu({
locale: 'Kimberly',
enableVoice: true,
barge: true,
menus: [
{
id: 'main',
mainPrompt: '#tts[Welcome to the main menu. Press 1 to confirm order or 2 to cancel.',
repeatPrompt: '#tts[We didn\'t get your input, please try again.]',
timeoutMills: 5000,
options: [
{
dtmf: '1',
action: 'menu(confirm)',
},
{
dtmf: '2',
action: 'return(cancel)',
},
],
},
{
id: 'confirm',
mainPrompt: '#tts[Thank you for confirming your order. Enter your 4-digit PIN.]',
maxDigits: 4,
},
],
}),
),
pie: callbackUrl,
},
},
};

const response = await voiceService.callouts.custom(requestData);

console.log(`Callout response: \n${JSON.stringify(response, null, 2)}`);
};
29 changes: 29 additions & 0 deletions snippets/voice/callouts/tts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const recipientPhoneNumber = 'the_phone_number_to_call';
const callingNumber = 'the_calling_number';

/** @type {Voice.TtsCalloutRequestData} */
const requestData = {
ttsCalloutRequestBody: {
method: 'ttsCallout',
ttsCallout: {
destination: {
type: 'number',
endpoint: recipientPhoneNumber,
},
cli: callingNumber,
locale: 'en-US/male',
text: 'Hello, this is a call from Sinch.',
},
},
};

const response = await voiceService.callouts.tts(requestData);

console.log(`Callout response: \n${JSON.stringify(response, null, 2)}`);
};
17 changes: 17 additions & 0 deletions snippets/voice/calls/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const callId = 'the_call_ID';

/** @type {Voice.GetCallResultRequestData} */
const requestData = {
callId,
};

const response = await voiceService.calls.get(requestData);

console.log(`Call information: \n${JSON.stringify(response, null, 2)}`);
};
30 changes: 30 additions & 0 deletions snippets/voice/calls/manage-with-call-leg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const callId = 'the_call_ID';

/** @type {Voice.ManageWithCallLegRequestData} */
const requestData = {
callId,
callLeg: 'callee',
manageWithCallLegRequestBody: {
action: {
name: 'hangup',
},
instructions: [
{
name: 'say',
locale: 'Matthew',
text: 'Hello, the call is over, hanging up now. Goodbye.',
},
],
},
};

await voiceService.calls.manageWithCallLeg(requestData);

console.log('Call managed with a call leg successfully.');
};
36 changes: 36 additions & 0 deletions snippets/voice/calls/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const callId = 'the_call_ID';

/** @type {Voice.UpdateCallRequestData} */
const requestData = {
callId,
updateCallRequestBody: {
instructions: [
{
name: 'startRecording',
options: {
destinationUrl: 's3://sinch-storage/voice-recordings/my-recording.mp3',
credentials: '(AwsAccessKey):(AwsSecretKey):(AwsRegion)',
notificationEvents: true,
transcriptionOptions: {
enabled: true,
locale: 'en-US',
},
},
},
],
action: {
name: 'hangup',
},
},
};

await voiceService.calls.update(requestData);

console.log('Call updated successfully.');
};
17 changes: 17 additions & 0 deletions snippets/voice/conferences/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const conferenceId = 'the_conference_ID';

/** @type {Voice.GetConferenceInfoRequestData} */
const requestData = {
conferenceId,
};

const response = await voiceService.conferences.get(requestData);

console.log(`Conference information: \n${JSON.stringify(response, null, 2)}`);
};
17 changes: 17 additions & 0 deletions snippets/voice/conferences/kick-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const conferenceId = 'the_conference_ID';

/** @type {Voice.KickAllRequestData} */
const requestData = {
conferenceId,
};

await voiceService.conferences.kickAll(requestData);

console.log('All the participants have been successfully kicked from the conference');
};
19 changes: 19 additions & 0 deletions snippets/voice/conferences/kick-participant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// eslint-disable-next-line no-unused-vars
import { Voice, VoiceService } from '@sinch/sdk-core';

/** @param {VoiceService} voiceService */
export const execute = async (voiceService) => {

const conferenceId = 'the_conference_ID';
const callId = 'the_call_ID_identifying_a_participant';

/** @type {Voice.KickParticipantRequestData} */
const requestData = {
conferenceId,
callId,
};

await voiceService.conferences.kickParticipant(requestData);

console.log('The participant has been successfully kicked from the conference');
};
Loading

0 comments on commit c57348d

Please sign in to comment.