feat(placetel): Add hungup call trigger
This commit is contained in:
3
packages/backend/src/apps/placetel/dynamic-data/index.ts
Normal file
3
packages/backend/src/apps/placetel/dynamic-data/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import listNumbers from './list-numbers';
|
||||||
|
|
||||||
|
export default [listNumbers];
|
@@ -0,0 +1,31 @@
|
|||||||
|
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: 'List numbers',
|
||||||
|
key: 'listNumbers',
|
||||||
|
|
||||||
|
async run($: IGlobalVariable) {
|
||||||
|
const numbers: {
|
||||||
|
data: IJSONObject[];
|
||||||
|
} = {
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const { data } = await $.http.get('/v2/numbers');
|
||||||
|
|
||||||
|
if (!data) {
|
||||||
|
return { data: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.length) {
|
||||||
|
for (const number of data) {
|
||||||
|
numbers.data.push({
|
||||||
|
value: number.number,
|
||||||
|
name: number.number,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return numbers;
|
||||||
|
},
|
||||||
|
};
|
@@ -1,6 +1,8 @@
|
|||||||
import defineApp from '../../helpers/define-app';
|
import defineApp from '../../helpers/define-app';
|
||||||
import addAuthHeader from './common/add-auth-header';
|
import addAuthHeader from './common/add-auth-header';
|
||||||
import auth from './auth';
|
import auth from './auth';
|
||||||
|
import triggers from './triggers';
|
||||||
|
import dynamicData from './dynamic-data';
|
||||||
|
|
||||||
export default defineApp({
|
export default defineApp({
|
||||||
name: 'Placetel',
|
name: 'Placetel',
|
||||||
@@ -13,4 +15,6 @@ export default defineApp({
|
|||||||
primaryColor: '069dd9',
|
primaryColor: '069dd9',
|
||||||
beforeRequest: [addAuthHeader],
|
beforeRequest: [addAuthHeader],
|
||||||
auth,
|
auth,
|
||||||
|
triggers,
|
||||||
|
dynamicData,
|
||||||
});
|
});
|
||||||
|
@@ -0,0 +1,97 @@
|
|||||||
|
import { IJSONObject } from '@automatisch/types';
|
||||||
|
import defineTrigger from '../../../../helpers/define-trigger';
|
||||||
|
|
||||||
|
export default defineTrigger({
|
||||||
|
name: 'Hungup Call',
|
||||||
|
key: 'hungupCall',
|
||||||
|
type: 'webhook',
|
||||||
|
description: 'Triggers when a call is hungup.',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
label: 'Numbers',
|
||||||
|
key: 'numbers',
|
||||||
|
type: 'dynamic' as const,
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
label: 'Number',
|
||||||
|
key: 'number',
|
||||||
|
type: 'dropdown' as const,
|
||||||
|
required: true,
|
||||||
|
description:
|
||||||
|
'Filter events by number. If the numbers are not specified, all numbers will be notified.',
|
||||||
|
variables: true,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getDynamicData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listNumbers',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
async testRun($) {
|
||||||
|
const response = await $.http.get('/v2/calls?order=desc');
|
||||||
|
|
||||||
|
if (!response?.data) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastCall = response.data[0];
|
||||||
|
|
||||||
|
const computedWebhookEvent = {
|
||||||
|
type: lastCall.type,
|
||||||
|
duration: lastCall.duration,
|
||||||
|
from: lastCall.from_number,
|
||||||
|
to: lastCall.to_number.number.number,
|
||||||
|
call_id: lastCall.id.toString(),
|
||||||
|
event: 'HungUp',
|
||||||
|
direction: 'in',
|
||||||
|
};
|
||||||
|
|
||||||
|
const dataItem = {
|
||||||
|
raw: computedWebhookEvent,
|
||||||
|
meta: {
|
||||||
|
internalId: computedWebhookEvent.call_id.toString(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
$.pushTriggerItem(dataItem);
|
||||||
|
},
|
||||||
|
|
||||||
|
async registerHook($) {
|
||||||
|
const numbers = ($.step.parameters.numbers as IJSONObject[])
|
||||||
|
.map((number: IJSONObject) => number.number)
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
const subscriptionPayload = {
|
||||||
|
service: 'string',
|
||||||
|
url: $.webhookUrl,
|
||||||
|
incoming: false,
|
||||||
|
outgoing: false,
|
||||||
|
hungup: true,
|
||||||
|
accepted: false,
|
||||||
|
phone: false,
|
||||||
|
numbers,
|
||||||
|
};
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
};
|
||||||
|
|
||||||
|
const { data } = await $.http.put('/v2/subscriptions', subscriptionPayload);
|
||||||
|
|
||||||
|
await $.flow.setRemoteWebhookId(data.id);
|
||||||
|
},
|
||||||
|
|
||||||
|
async unregisterHook($) {
|
||||||
|
await $.http.delete(`/v2/subscriptions/${$.flow.remoteWebhookId}`);
|
||||||
|
},
|
||||||
|
});
|
3
packages/backend/src/apps/placetel/triggers/index.ts
Normal file
3
packages/backend/src/apps/placetel/triggers/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import hungupCall from './hungup-call';
|
||||||
|
|
||||||
|
export default [hungupCall];
|
Reference in New Issue
Block a user