Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { twiml } from 'twilio';
- import { fetchSettingByName } from '../../system-setting/system-setting-action';
- import { validateTwilioSignature } from '../../../shared/twilio';
- import { getBaseUrl } from '../../../shared/utils';
- import * as qs from 'querystring';
- /**
- * Waiting for a conference webhook handler.
- *
- * @param {object} event - The cloud function's event.
- * @param {object} ctx - The cloud function's context api.
- *
- * @returns {Promise} The request result.
- */
- export default async function(event, ctx) {
- const { call } = event.pathParameters;
- try {
- const response = new twiml.VoiceResponse();
- const signature = event.headers['X-Twilio-Signature'];
- const url = `${getBaseUrl(ctx)}/webhook/inbound-wait-conference-action/${call}`;
- const body = qs.parse(event.body);
- let setting = null;
- console.log('call: inbound-wait-conference-action Body:', JSON.stringify(body, null, 2));
- if (validateTwilioSignature(signature, url, body) === false) {
- console.log(
- 'call: inbound-wait-conference-action Invalid twilio signature [' + signature + ']',
- );
- return {
- headers: { 'Content-Type': 'application/json' },
- statusCode: 401,
- body: JSON.stringify({
- message: 'Unauthorized request',
- }),
- };
- }
- console.log('call: inbound-wait-conference-action Valid twilio signature');
- try {
- setting = await fetchSettingByName(ctx, 'CALL_WAIT_SOUND', { checkPermissions: false });
- } catch (e) {
- console.log('call: inbound-wait-conference-action Could not fetch waiting sound:', e.message);
- }
- if (setting) {
- console.log('call: inbound-wait-conference-action Playing waiting sound');
- response.play(
- {
- loop: 5,
- },
- setting.file.downloadUrl,
- );
- } else {
- console.log('call: inbound-wait-conference-action Playing default sound');
- /**
- * This is the defautl audio twilio plays
- * when entering a conference.
- */
- response.play(
- {
- loop: 1,
- },
- 'http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3',
- );
- }
- /*console.log('call: inbound-wait-conference-action Redirecting call');
- response.redirect(
- `${getBaseUrl(ctx)}/webhook/calls/inbound-drop/${call}/${body.ConferenceSid}`,
- );*/
- console.log('call: inbound-wait-conference-action Twiml:', response.toString());
- return {
- headers: {
- 'Content-Type': 'text/xml',
- },
- statusCode: 200,
- body: response.toString(),
- };
- } catch (e) {
- console.log('call: inbound-wait-conference-action Error', e.message);
- // return {
- // statusCode: 500,
- // body: `Something happened: ${e.message}`,
- // };
- const response = new twiml.VoiceResponse();
- console.log('call: inbound-wait-conference-action Something happened:', e.message);
- response.say(`Something happened: ${e.message}`);
- return {
- statusCode: 200,
- body: response.toString(),
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment