Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { getBaseUrl } from '../../../shared/utils';
- import { validateTwilioSignature } from '../../../shared/twilio';
- import { createPhoneCallRecording } from '../phone-call-actions';
- import * as qs from 'querystring';
- import { twiml } from 'twilio';
- /**
- * Inbound call finished webhook handler.
- *
- * @param {object} event - The cloud function's event data.
- * @param {object} ctx - The cloud function's context api.
- *
- * @returns {Promise} The webhook result.
- */
- export default async function(event, ctx) {
- const { callId } = event.pathParameters;
- const body = qs.parse(event.body);
- const url = `${getBaseUrl(ctx)}/webhook/inbound-call/${callId}/finished`;
- const signature = event.headers['X-Twilio-Signature'];
- console.log('call: inbound-call-finished is executing');
- if (validateTwilioSignature(signature, url, body) === false) {
- console.log('call: inbound-call-finished Invalid twilio signature [' + signature + ']');
- return {
- statusCode: 401,
- body: {
- message: 'Unauthorized request',
- },
- };
- }
- if (body.RecordingSid) {
- try {
- console.log('call: inbound-call-finished recording SID:', body.RecordingSid);
- await createPhoneCallRecording(
- ctx,
- {
- recordingSid: body.RecordingSid,
- recordingUrl: body.RecordingUrl,
- duration: parseInt(body.RecordingDuration),
- status: 'AVAILABLE',
- inboundCall: { connect: { id: callId } },
- },
- { checkPermissions: false },
- );
- } catch (e) {
- console.log('call: Something happened creating the call recording', JSON.stringify(e));
- }
- }
- const response = new twiml.VoiceResponse();
- response.hangup();
- return {
- headers: { 'Content-Type': 'text/xml' },
- statusCode: 200,
- body: response.toString(),
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment