Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.6 kB
1
Indexable
Never
    flex.Actions.replaceAction(
      "StartOutboundCall",
      (payload, invokeOriginal) => {
        // Log that the outbound call is starting
        console.log("Starting Outbound Call...");
        // Define the SIP address to be used for the call

        const sipAddress = "unionbank.sip.ulap.net";
        // Get the first six digits of the destination number
        const firstSixDigits = payload.destination.slice(0, 6);
        // Check if the calling queue is SIP by checking the `callerID` array for the business unit
        const businessUnitResult = callerIDS.find(
          (item) => item.team === "FEWS"
        );

        if (!businessUnitResult) {
          console.error("Could not find business unit in caller ID list.");
          return invokeOriginal(payload);
        }
        // Initialize a variable to store the selected caller ID
        let selectedCallerID = null;
        // Define a function to determine if the destination number is a landline number
        function isLandlineNumber(number) {
          return number.startsWith("+632") || number.startsWith("+6332");
        }
        // Determine the phone number type (landline, Globe, Smart) based on the first six digits
        if (isLandlineNumber(payload.destination)) {
          selectedCallerID = getRandomElementFromArray(
            businessUnitResult.landlineCallerIds
          );
        } 
        else if (globePhoneNumbers.includes(firstSixDigits)) {
          selectedCallerID = getRandomElementFromArray(
            businessUnitResult.globeCallerIds
          );
        } else if (smartPhoneNumbers.includes(firstSixDigits)) {
          selectedCallerID = getRandomElementFromArray(
            businessUnitResult.smartCallerIds
          );
        } else {
          console.error(
            "Could not determine phone number type for destination number."
          );
          return invokeOriginal(payload);
        }
        
        // If the call is from the "Bring Your Own Carrier" queue, update the call details
        if (payload.queueSid === process.env.FLEX_APP_QUEUE_SID && selectedCallerID != null) {
          payload.callerId = selectedCallerID;
          payload.destination = `sip:${payload.destination}@${sipAddress};edge=singapore;secure=true`;
          console.warn("Updated outbound call to: ", payload);
        } else {
          console.log("Non BYOC Call, doing nothing...");
        }
        // Invoke the original implementation of the "StartOutboundCall" action
        invokeOriginal(payload);
      }
    );