Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- fetchUserIdsToCall,
- createParticipant,
- updateInboundPhoneCall,
- } from '../phone-call-actions';
- import { twilio } from '../../../shared/twilio';
- import { getAgencySIDFromAgencyID } from '../../../shared/users/users-actions';
- import { getBaseUrl } from '../../../shared/utils';
- import * as qs from 'querystring';
- /**
- * Task handler that fetches the available users
- * in an agency and calls them.
- *
- * @param {object} event - The cloud function's event.
- * @param {object} ctx - The cloud function's context api.
- *
- * @returns {Promise<void>} The event object.
- */
- export default async function(event, ctx) {
- console.log('call: triggered distribute-inbound-call');
- const body = qs.parse(event.body);
- console.log('call: distribute-inbound-call body', JSON.stringify(body));
- let agencySID;
- try {
- agencySID = await getAgencySIDFromAgencyID(ctx, { id: event.data.agencyId });
- } catch (error) {
- console.log('call: distribute-inbound-call error getting agency', JSON.stringify(error));
- return {
- statusCode: 400,
- body: {
- message: error.message,
- },
- };
- }
- let twilioClient = await twilio(agencySID);
- const {
- callSid, // The original inbound call sid
- callId, // The id of the call stored on 8base
- from, // The phone number that made the call
- agencyId, // The id of the agency receiving the call
- campaign, // The campaign object related to the agency phone number, if any
- } = event.data;
- let users = [];
- let conferences = [];
- let promise = twilioClient.conferences.list({ friendlyName: callSid, limit: 1 });
- try {
- users = await fetchUserIdsToCall(agencyId, campaign?.id, ctx, { checkPermissions: false });
- } catch (e) {
- console.log('call: distribute-inbound-call Could not fetch users to call:', e.message);
- }
- try {
- conferences = await promise;
- } catch (e) {
- console.log('call: distribute-inbound-call Could not fetch conference:', e.message);
- return event;
- }
- const [conference] = conferences;
- console.log('call: distribute-inbound-call users', users);
- if (users.length === 0) {
- /**
- * Announce on the conference
- * that there are not agents available
- * an then hangup.
- */
- return event;
- }
- console.log('call: distribute-inbound-call conference', conference.sid);
- promise = updateInboundPhoneCall(
- { callSid },
- {
- conferenceSid: conference.sid,
- },
- ctx,
- { checkPermissions: false },
- );
- const promises = Promise.all(
- users.map(async (user) => {
- const participant = await createParticipant(
- {
- user: user,
- inboundCall: callId,
- isInitialParticipant: true,
- },
- ctx,
- { checkPermissions: false },
- );
- return twilioClient.conferences(conference.sid).participants.create({
- beep: true,
- earlyMedia: false,
- from: from,
- to: `client:${user}?${qs.stringify({ id: participant.id })}`,
- statusCallback: `${getBaseUrl(ctx)}/webhook/participant/${participant.id}/status`,
- statusCallbackMethod: 'POST',
- statusCallbackEvent: ['ringing', 'answered', 'completed'],
- });
- }),
- );
- try {
- let awaitedPromises = await promises;
- console.log('call: distribute-inbound-call awaitedPromises', JSON.stringify(awaitedPromises));
- } catch (e) {
- console.log('call: distribute-inbound-call Could not dispatch all calls:', e.message);
- }
- try {
- let awaitedPromise = await promise;
- console.log('call: distribute-inbound-call awaitedPromise', JSON.stringify(awaitedPromise));
- } catch (e) {
- console.log('call: distribute-inbound-call Could not set conference id on call:', e.message);
- }
- return event;
- }
Advertisement
Add Comment
Please, Sign In to add comment