eliecerthoms1

inbound-call-drop.js

Oct 20th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { twilio, validateTwilioSignature } from '../../../shared/twilio';
  2. import { getBaseUrl, handleTryCatch } from '../../../shared/utils';
  3. import * as qs from 'querystring';
  4. import { fetchInboundCall, getCallDropResponse } from '../phone-call-actions';
  5. import rqstbin from '../../../shared/rqstbin';
  6.  
  7. import { twiml } from 'twilio';
  8.  
  9. /**
  10.  * Inbound call drop handler.
  11.  *
  12.  * @param {object} event - The cloud function's event data.
  13.  * @param {object} ctx - The cloud function's context api.
  14.  *
  15.  * @returns {Promise} The webhook response.
  16.  */
  17. export default async function(event, ctx) {
  18.   const { call, conference } = event.pathParameters;
  19.   const body = qs.parse(event.body);
  20.   const url = `${getBaseUrl(ctx)}/webhook/calls/inbound-drop/${call}/${conference}`;
  21.   const signature = event.headers['X-Twilio-Signature'];
  22.  
  23.   const headers = { 'Content-Type': 'text/xml' };
  24.  
  25.   let twilioClient = await twilio(body.AccountSid);
  26.  
  27.   if (validateTwilioSignature(signature, url, body) === false) {
  28.     console.log('Invalid twilio signature [' + signature + ']');
  29.  
  30.     return {
  31.       statusCode: 401,
  32.       body: JSON.stringify({
  33.         message: 'Unauthorized request',
  34.       }),
  35.     };
  36.   } else {
  37.     rqstbin({ signature: 'validated', from: 'inbound-call-drop' });
  38.   }
  39.  
  40.   const [phoneCall, error] = await handleTryCatch(
  41.     fetchInboundCall(call, ctx, { checkPermissions: false }),
  42.   );
  43.   console.log('phoneCall', phoneCall);
  44.   const allowVoicemail = Boolean(phoneCall?.agency?.phoneCallSettings?.voicemail);
  45.   const voicemailUrl = phoneCall?.agency?.voicemailUrl;
  46.  
  47.   if (error) {
  48.     console.log('call: error updating call data');
  49.     const response = new twiml.VoiceResponse();
  50.     response.say('Sorry we can not retrieve the phone call data.');
  51.     return {
  52.       statusCode: 200,
  53.       body: response.toString(),
  54.     };
  55.   }
  56.  
  57.   //console.log(getCallDropResponse(ctx, call, voicemail));
  58.  
  59.   const [updatedCall, updateCallError] = await handleTryCatch(async () => {
  60.     const participants = await twilioClient.conferences(conference).participants.list();
  61.  
  62.     if (participants.length < 2) {
  63.       return twilioClient.calls(phoneCall.callSid).update({
  64.         twiml: getCallDropResponse(ctx, call, allowVoicemail, voicemailUrl),
  65.       });
  66.     }
  67.   });
  68.  
  69.   if (updateCallError) {
  70.     console.log('call: error updating call data');
  71.     return {
  72.       statusCode: 200,
  73.       body:
  74.         '<Response><Say>Sorry, we are experiencing technical difficulties. Please try again later.</Say></Response>',
  75.     };
  76.   }
  77.  
  78.   console.log(JSON.stringify(updatedCall, null, 2));
  79.  
  80.   return {
  81.     headers,
  82.     statusCode: 200,
  83.     body: '<Response />',
  84.   };
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment