Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
5.6 kB
2
Indexable
Never
// Steps to build callflow object
// PUT /endpoints/:id endpoint

exports.buildCallfowTasks = async () => {
    // example of data which would come in as param
    const data = {
        "name": "new us",
        "timezone": "Europe/Paris",
        "welcome_message_enabled": true,
        "welcome_message": "Welcome and thank you for calling!",
        "welcome_message_file": null,
        "voicemail_message_enabled": true,
        "voicemail_message": "Please leave your message after the tone.",
        "voicemail_message_file": null,
        "timeout": 45,
        "call_recording_enabled": true,
        "call_recording_message": "Your call is being recorded for quality assurance purposes.",
        "call_recording_message_file": null,
        "voice_type": "Google.Ava",
        call_forwarding_enabled: true,
        call_forwarding_phone_number: "+43123456",
        call_forwarding_timeout: 10,
        call_sequential_ringing_enabled: true,
        call_sequential_ringing_clients: [],
        call_sequential_ringing_timeout: 10,
        call_simultanous_ringing_enabled: true,
        call_simultanous_ringing_clients: [],
    };

    const tasks = [];
    // welcome message
    if (data.welcome_message_enabled) {
        let welcomeTask = {};
        if (data.welcome_message) {
            welcomeTask = {
                id: 'welcome.text',
                text: data.welcome_message,
                type: 'say',
                voice: data.voice,
            };
            tasks.push(welcomeTask);
        } else if (data.welcome_message_file) {
            // do magic with file to get link of the file + remove old file
            const welcomeAudioFileUrl = '';
            welcomeTask = {
                id: 'welcome.audio',
                url: welcomeAudioFileUrl,
                type: 'play',
            };
            tasks.push(welcomeTask);
        }
    }
    // call recording message
    if (data.call_recording_enabled) {
        if (data.call_recording_message) {
            const callRecordingMessageTask = {
                id: 'callRecording.text',
                text: data.call_recording_message,
                type: 'say',
                voice: data.voice,
            };
            tasks.push(callRecordingMessageTask);
        } else if (data.call_recording_message_file) {
            // do magic with file to get link of the file + remove old file
            const recordingAudioFileUrl = '';
            const callRecordingMessageTask = {
                id: 'callRecording.audio',
                url: recordingAudioFileUrl,
                type: 'play',
            };
            tasks.push(callRecordingMessageTask);
        }
    }

    // call dial
    // only not present when immediate call forwarding
    if (!(data.call_forwarding_enabled && data.call_forwarding_timeout === 0)) {
        if (!data.call_sequential_ringing_enabled) {
            const dialTask = {
                id: 'dial',
                type: 'dial',
            };
            if (data.call_recording_enabled) {
                dialTask.record = true;
            }
            if (data.call_simultanous_ringing_enabled) {
                dialTask.clients = data.call_simultanous_ringing_clients;
            }
            // if forwarding enabled, set timeout
            if (data.call_forwarding_enabled) {
                dialTask.timeout = data.call_forwarding_timeout;
                // if not forwarding but voicemail set
            } else if (data.voicemail_message_enabled) {
                dialTask.timeout = data.timeout;
            }
            tasks.push(dialTask);
        // voicemail timeout is not used in sequential ringing
        } else {
            for (const client of data.call_sequential_ringing_clients) {
                const clientDialTask = {
                    id: `dial-${client}`,
                    type: 'dial',
                    clients: [client],
                    timeoute: data.call_sequential_ringing_timeout,
                };
                if (data.call_recording_enabled) {
                    clientDialTask.record = true;
                }
                tasks.push(clientDialTask);
            }
        }
    }

    // call forward
    if (data.call_forwarding_enabled) {
        const forwardTask = {
            id: 'forward',
            type: 'dial',
            numbers: [data.call_forwarding_phone_number],
        };
        if (data.call_recording_enabled) {
            forwardTask.record = true;
        }
        if (data.voicemail_message_enabled) {
            forwardTask.timeout = data.timeout;
        }
    }

    // voicemail message:
    if (data.voicemail_message_enabled) {
        if (data.voicemail_message) {
            const voicemailMessageTask = {
                id: 'voicemail.text',
                text: data.voicemail_message,
                type: 'say',
                voice: data.voice,
            };
            tasks.push(voicemailMessageTask);
        } else if (data.voicemail_message_file) {
            // do magic with file to get link of the file + remove old file
            const voicemailMessageAudioFileUrl = '';
            const voicemailMessageTask = {
                id: 'voicemail.audio',
                url: voicemailMessageAudioFileUrl,
                type: 'play',
            };
            tasks.push(voicemailMessageTask);
        }
    }
    // voicemail recording:
    if (data.voicemail_message_enabled) {
        const voicemailRecordingTask = {
            id: 'voicemail.record',
            type: 'record',
        };
        tasks.push(voicemailRecordingTask);
    }
};
Leave a Comment