Skip to content

Commit 5ab16e5

Browse files
authored
DEVEXP-559: Add snippets for Voice API (#5)
1 parent 1958848 commit 5ab16e5

File tree

17 files changed

+399
-0
lines changed

17 files changed

+399
-0
lines changed
File renamed without changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const phoneNumber = 'YOUR_Sinch_phone_number_with_voice_capability';
8+
const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';
9+
10+
/** @type {Voice.AssignNumbersRequestData} */
11+
const requestData = {
12+
assignNumbersRequestBody: {
13+
numbers: [
14+
phoneNumber,
15+
],
16+
applicationkey: applicationKey,
17+
capability: 'voice',
18+
},
19+
};
20+
21+
await voiceService.applications.assignNumbers(requestData);
22+
23+
console.log('The phone numbers have been assigned to the application successfully.');
24+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';
8+
9+
/** @type {Voice.GetCallbackURLsRequestData} */
10+
const requestData = {
11+
applicationkey: applicationKey,
12+
};
13+
14+
const response = await voiceService.applications.getCallbackURLs(requestData);
15+
16+
console.log(`Callback URLs:\n${JSON.stringify(response, null, 2)}`);
17+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
/** @type {Voice.GetNumbersRequestData} */
8+
const requestData = {};
9+
10+
const response = await voiceService.applications.listNumbers(requestData);
11+
12+
console.log(`Numbers list:\n${JSON.stringify(response, null, 2)}`);
13+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const phoneNumber = 'the_phone_number';
8+
9+
/** @type {Voice.QueryNumberRequestData} */
10+
const requestData = {
11+
number: phoneNumber,
12+
};
13+
14+
const response = await voiceService.applications.queryNumber(requestData);
15+
16+
console.log(`Phone number information:\n${JSON.stringify(response, null, 2)}`);
17+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const phoneNumber = 'YOUR_Sinch_phone_number_with_voice_capability';
8+
const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';
9+
10+
/** @type {Voice.UnassignNumberRequestData} */
11+
const requestData = {
12+
unassignNumbersRequestBody: {
13+
number: phoneNumber,
14+
applicationkey: applicationKey,
15+
capability: 'voice',
16+
},
17+
};
18+
19+
await voiceService.applications.unassignNumber(requestData);
20+
21+
console.log('The phone numbers have been unassigned from the application successfully.');
22+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const applicationKey = 'YOUR_Voice_application_key_from_the_dashboard';
8+
9+
/** @type {Voice.UpdateCallbackURLsRequestData} */
10+
const requestData = {
11+
applicationkey: applicationKey,
12+
updateCallbacksRequestBody: {
13+
url: {
14+
primary: 'https://new-primary-callback-url.com',
15+
fallback: 'https://new-fallback-callback-url.com',
16+
},
17+
},
18+
};
19+
20+
await voiceService.applications.updateCallbackURLs(requestData);
21+
22+
console.log('The callback URLs have been successfully updated.');
23+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const conferenceId = 'YOUR_conference_ID';
8+
const recipientPhoneNumber = 'the_phone_number_to_call';
9+
const callingNumber = 'the_calling_number';
10+
11+
/** @type {Voice.ConferenceCalloutRequestData} */
12+
const requestData = {
13+
conferenceCalloutRequestBody: {
14+
method: 'conferenceCallout',
15+
conferenceCallout: {
16+
conferenceId,
17+
destination: {
18+
type: 'number',
19+
endpoint: recipientPhoneNumber,
20+
},
21+
cli: callingNumber,
22+
},
23+
},
24+
};
25+
26+
const response = await voiceService.callouts.conference(requestData);
27+
28+
console.log(`Callout response: \n${JSON.stringify(response, null, 2)}`);
29+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const recipientPhoneNumber = 'the_phone_number_to_call';
8+
const callingNumber = 'the_calling_number';
9+
const callbackUrl= 'the_PIE_callback_URL';
10+
11+
/** @type {Voice.CustomCalloutRequestData} */
12+
const requestData = {
13+
customCalloutRequestBody: {
14+
method: 'customCallout',
15+
customCallout: {
16+
destination: {
17+
type: 'number',
18+
endpoint: recipientPhoneNumber,
19+
},
20+
cli: callingNumber,
21+
ice: Voice.customCalloutHelper.formatIceResponse(
22+
Voice.iceActionHelper.connectPstn({
23+
number: recipientPhoneNumber,
24+
cli: callingNumber,
25+
}),
26+
Voice.iceInstructionHelper.say('Welcome to Sinch', 'en-US/male'),
27+
),
28+
ace: Voice.customCalloutHelper.formatAceResponse(
29+
Voice.aceActionHelper.runMenu({
30+
locale: 'Kimberly',
31+
enableVoice: true,
32+
barge: true,
33+
menus: [
34+
{
35+
id: 'main',
36+
mainPrompt: '#tts[Welcome to the main menu. Press 1 to confirm order or 2 to cancel.',
37+
repeatPrompt: '#tts[We didn\'t get your input, please try again.]',
38+
timeoutMills: 5000,
39+
options: [
40+
{
41+
dtmf: '1',
42+
action: 'menu(confirm)',
43+
},
44+
{
45+
dtmf: '2',
46+
action: 'return(cancel)',
47+
},
48+
],
49+
},
50+
{
51+
id: 'confirm',
52+
mainPrompt: '#tts[Thank you for confirming your order. Enter your 4-digit PIN.]',
53+
maxDigits: 4,
54+
},
55+
],
56+
}),
57+
),
58+
pie: callbackUrl,
59+
},
60+
},
61+
};
62+
63+
const response = await voiceService.callouts.custom(requestData);
64+
65+
console.log(`Callout response: \n${JSON.stringify(response, null, 2)}`);
66+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Voice, VoiceService } from '@sinch/sdk-core';
3+
4+
/** @param {VoiceService} voiceService */
5+
export const execute = async (voiceService) => {
6+
7+
const recipientPhoneNumber = 'the_phone_number_to_call';
8+
const callingNumber = 'the_calling_number';
9+
10+
/** @type {Voice.TtsCalloutRequestData} */
11+
const requestData = {
12+
ttsCalloutRequestBody: {
13+
method: 'ttsCallout',
14+
ttsCallout: {
15+
destination: {
16+
type: 'number',
17+
endpoint: recipientPhoneNumber,
18+
},
19+
cli: callingNumber,
20+
locale: 'en-US/male',
21+
text: 'Hello, this is a call from Sinch.',
22+
},
23+
},
24+
};
25+
26+
const response = await voiceService.callouts.tts(requestData);
27+
28+
console.log(`Callout response: \n${JSON.stringify(response, null, 2)}`);
29+
};

0 commit comments

Comments
 (0)