Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVEXP-559: Add snippets for Voice API #5

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions snippets/voice/applications/assign-numbers/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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/snippet.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
Loading