feat(signalwire): add receive call

This commit is contained in:
Ali BARIN
2024-12-13 14:13:35 +00:00
parent 79b0d083e6
commit bfcf54a26f
6 changed files with 127 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import listIncomingPhoneNumbers from './list-incoming-phone-numbers/index.js';
import listIncomingSmsPhoneNumbers from './list-incoming-sms-phone-numbers/index.js';
import listIncomingCallPhoneNumbers from './list-incoming-call-phone-numbers/index.js';
export default [listIncomingPhoneNumbers];
export default [listIncomingCallPhoneNumbers, listIncomingSmsPhoneNumbers];

View File

@@ -0,0 +1,37 @@
export default {
name: 'List incoming call phone numbers',
key: 'listIncomingCallPhoneNumbers',
async run($) {
let requestPath = `/api/laml/2010-04-01/Accounts/${$.auth.data.accountSid}/IncomingPhoneNumbers`;
const aggregatedResponse = {
data: [],
};
do {
const { data } = await $.http.get(requestPath);
const voiceCapableIncomingPhoneNumbers = data.incoming_phone_numbers
.filter((incomingPhoneNumber) => {
return incomingPhoneNumber.capabilities.voice;
})
.map((incomingPhoneNumber) => {
const friendlyName = incomingPhoneNumber.friendly_name;
const phoneNumber = incomingPhoneNumber.phone_number;
const name = [friendlyName, phoneNumber].filter(Boolean).join(' - ');
return {
value: incomingPhoneNumber.sid,
name,
};
});
aggregatedResponse.data.push(...voiceCapableIncomingPhoneNumbers);
requestPath = data.next_page_uri;
} while (requestPath);
return aggregatedResponse;
},
};

View File

@@ -1,6 +1,6 @@
export default {
name: 'List incoming phone numbers',
key: 'listIncomingPhoneNumbers',
name: 'List incoming SMS phone numbers',
key: 'listIncomingSmsPhoneNumbers',
async run($) {
let requestPath = `/api/laml/2010-04-01/Accounts/${$.auth.data.accountSid}/IncomingPhoneNumbers`;

View File

@@ -1,3 +1,4 @@
import receiveCall from './receive-call/index.js';
import receiveSms from './receive-sms/index.js';
export default [receiveSms];
export default [receiveCall, receiveSms];

View File

@@ -0,0 +1,82 @@
import { URLSearchParams } from 'node:url';
import Crypto from 'node:crypto';
import isEmpty from 'lodash/isEmpty.js';
import defineTrigger from '../../../../helpers/define-trigger.js';
export default defineTrigger({
name: 'Receive Call',
key: 'receiveCall',
type: 'webhook',
description: 'Triggers when a new call is received.',
arguments: [
{
label: 'To Number',
key: 'phoneNumberSid',
type: 'dropdown',
required: true,
description:
'The number to receive the call on. It should be a SignalWire number in your project.',
variables: false,
source: {
type: 'query',
name: 'getDynamicData',
arguments: [
{
name: 'key',
value: 'listIncomingCallPhoneNumbers',
},
],
},
},
],
async run($) {
const dataItem = {
raw: $.request.body,
meta: {
internalId: Crypto.randomUUID(),
},
};
$.pushTriggerItem(dataItem);
},
async testRun($) {
const lastExecutionStep = await $.getLastExecutionStep();
if (!isEmpty(lastExecutionStep?.dataOut)) {
$.pushTriggerItem({
raw: lastExecutionStep.dataOut,
meta: {
internalId: '',
},
});
}
},
async registerHook($) {
const phoneNumberSid = $.step.parameters.phoneNumberSid;
const payload = new URLSearchParams({
VoiceUrl: $.webhookUrl,
}).toString();
await $.http.post(
`/2010-04-01/Accounts/${$.auth.data.accountSid}/IncomingPhoneNumbers/${phoneNumberSid}.json`,
payload
);
},
async unregisterHook($) {
const phoneNumberSid = $.step.parameters.phoneNumberSid;
const payload = new URLSearchParams({
VoiceUrl: '',
}).toString();
await $.http.post(
`/2010-04-01/Accounts/${$.auth.data.accountSid}/IncomingPhoneNumbers/${phoneNumberSid}.json`,
payload
);
},
});

View File

@@ -20,7 +20,7 @@ export default defineTrigger({
arguments: [
{
name: 'key',
value: 'listIncomingPhoneNumbers',
value: 'listIncomingSmsPhoneNumbers',
},
],
},