eliecerthoms1

inbound-wait-conference-action.js

Oct 20th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { twiml } from 'twilio';
  2. import { fetchSettingByName } from '../../system-setting/system-setting-action';
  3. import { validateTwilioSignature } from '../../../shared/twilio';
  4. import { getBaseUrl } from '../../../shared/utils';
  5. import * as qs from 'querystring';
  6.  
  7. /**
  8.  * Waiting for a conference webhook handler.
  9.  *
  10.  * @param {object} event - The cloud function's event.
  11.  * @param {object} ctx - The cloud function's context api.
  12.  *
  13.  * @returns {Promise} The request result.
  14.  */
  15. export default async function(event, ctx) {
  16.   const { call } = event.pathParameters;
  17.   try {
  18.     const response = new twiml.VoiceResponse();
  19.     const signature = event.headers['X-Twilio-Signature'];
  20.     const url = `${getBaseUrl(ctx)}/webhook/inbound-wait-conference-action/${call}`;
  21.     const body = qs.parse(event.body);
  22.     let setting = null;
  23.  
  24.     console.log('call: inbound-wait-conference-action Body:', JSON.stringify(body, null, 2));
  25.  
  26.     if (validateTwilioSignature(signature, url, body) === false) {
  27.       console.log(
  28.         'call: inbound-wait-conference-action Invalid twilio signature [' + signature + ']',
  29.       );
  30.  
  31.       return {
  32.         headers: { 'Content-Type': 'application/json' },
  33.         statusCode: 401,
  34.         body: JSON.stringify({
  35.           message: 'Unauthorized request',
  36.         }),
  37.       };
  38.     }
  39.     console.log('call: inbound-wait-conference-action Valid twilio signature');
  40.     try {
  41.       setting = await fetchSettingByName(ctx, 'CALL_WAIT_SOUND', { checkPermissions: false });
  42.     } catch (e) {
  43.       console.log('call: inbound-wait-conference-action Could not fetch waiting sound:', e.message);
  44.     }
  45.  
  46.     if (setting) {
  47.       console.log('call: inbound-wait-conference-action Playing waiting sound');
  48.       response.play(
  49.         {
  50.           loop: 5,
  51.         },
  52.         setting.file.downloadUrl,
  53.       );
  54.     } else {
  55.       console.log('call: inbound-wait-conference-action Playing default sound');
  56.       /**
  57.        * This is the defautl audio twilio plays
  58.        * when entering a conference.
  59.        */
  60.       response.play(
  61.         {
  62.           loop: 1,
  63.         },
  64.         'http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3',
  65.       );
  66.     }
  67.  
  68.     /*console.log('call: inbound-wait-conference-action Redirecting call');
  69.     response.redirect(
  70.       `${getBaseUrl(ctx)}/webhook/calls/inbound-drop/${call}/${body.ConferenceSid}`,
  71.     );*/
  72.  
  73.     console.log('call: inbound-wait-conference-action Twiml:', response.toString());
  74.  
  75.     return {
  76.       headers: {
  77.         'Content-Type': 'text/xml',
  78.       },
  79.       statusCode: 200,
  80.       body: response.toString(),
  81.     };
  82.   } catch (e) {
  83.     console.log('call: inbound-wait-conference-action Error', e.message);
  84.  
  85.     // return {
  86.     //   statusCode: 500,
  87.     //   body: `Something happened: ${e.message}`,
  88.     // };
  89.     const response = new twiml.VoiceResponse();
  90.  
  91.     console.log('call: inbound-wait-conference-action Something happened:', e.message);
  92.     response.say(`Something happened: ${e.message}`);
  93.     return {
  94.       statusCode: 200,
  95.       body: response.toString(),
  96.     };
  97.   }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment