refactor(twilio/receive-sms): convert to webhook

This commit is contained in:
Ali BARIN
2023-05-15 20:30:16 +00:00
parent 6c14a353ef
commit 25cb4d90f3
4 changed files with 115 additions and 4 deletions

View File

@@ -1,23 +1,73 @@
import { URLSearchParams } from 'node:url';
import isEmpty from 'lodash/isEmpty';
import defineTrigger from '../../../../helpers/define-trigger';
import fetchMessages from './fetch-messages';
export default defineTrigger({
name: 'Receive SMS',
key: 'receiveSms',
pollInterval: 15,
type: 'webhook',
description: 'Triggers when a new SMS is received.',
arguments: [
{
label: 'To Number',
key: 'toNumber',
type: 'string',
key: 'phoneNumberSid',
type: 'dropdown' as const,
required: true,
description:
'The number to receive the SMS on. It should be a Twilio number.',
variables: false,
source: {
type: 'query',
name: 'getDynamicData',
arguments: [
{
name: 'key',
value: 'listIncomingPhoneNumbers',
},
{
name: 'valueType',
value: 'sid',
}
],
},
},
],
async run($) {
async testRun($) {
await fetchMessages($);
if (!isEmpty($.lastExecutionStep?.dataOut)) {
$.pushTriggerItem({
raw: $.lastExecutionStep.dataOut,
meta: {
internalId: '',
}
});
}
},
async registerHook($) {
const phoneNumberSid = $.step.parameters.phoneNumberSid as string;
const payload = new URLSearchParams({
SmsUrl: $.webhookUrl,
}).toString();
await $.http.post(
`/2010-04-01/Accounts/${$.auth.data.accountSid}/IncomingPhoneNumbers/${phoneNumberSid}.json`,
payload
);
},
async unregisterHook($) {
const toNumber = $.step.parameters.toNumber as string;
const payload = new URLSearchParams({
SmsUrl: '',
}).toString();
await $.http.post(
`/2010-04-01/Accounts/${$.auth.data.accountSid}/IncomingPhoneNumbers/PN${toNumber}.json`,
payload
);
},
});