Send incoming calls to the right destination.

This script intercepts incoming calls. It checks the dialed number against a list in a file. This means it’s easy to adjust the rules as you just update the file. When a number matches one on the list, the call is forwarded to its designated destination. If the trunk settings are strict and there is no match, the call is blocked.

How the Inbound DID Routing Script Works:

  1. The script opens a file, named routes.json, that lists phone numbers and their destinations.
  2. It compares the incoming call’s number to those in the file.
  3. When it finds a match, the call is forwarded to the corresponding destination.

How to Use the Script:

  • In the admin area, go to Console > Integrations > Call Scripts.
  • Add from the Store and select “Inbound DID Routing.”
  • Name the script.
  • Set the script to run when a call is received on a trunk (or when this DID is called).
  • Choose the trunk or DID to monitor.
  • Upload your file that lists phone numbers and their destinations.
  • If you rename the file, update the script configuration to match.
  • For example, if you rename the JSON file, update the jsonFileName variable accordingly.

Note:

In case the script is set to run when this DID is called. The DID and destination must also be specified in the json file otherwise the call will drop
The .json file should be formatted like this:

{
"": ".."
}
So, for example: DID 112233 route to extension user 100 you will need to save the file like this;
{
"112233":"Extension.100."
}

Script Functionality and Implementation:

Implementation:

  • Uses the ExternalLine object and IsInbound property to check if the call is inbound to the phone system
  • Uses the GetPropertyValue method in order to get the call script name and construct the path where the file is stored
  • Uses the ReadAllText method from C# class File in order to read the contents of the file
  • Deserializes the Json object and extracts the DID and destination.
  • Uses the destination struct in order to check if the destination is valid and if it is valid construct the destination
  • Routes the call to the destination using RouteToAsync

Functionality:

  • Reads the key/value pairs from the routes.json file
  • Routes the call to the destination based on the key/value pair

DISCLAIMER! To see this script v20 U5 is required.This script is for reference purposes only. For the latest version, download from the store.


#nullable disable
using CallFlow;
using System.Threading.Tasks;
using TCX.Configuration;
using TCX.PBXAPI;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace dummy
{
public class InterceptInboundCall : ScriptBase
{
// Specify here your json filename with routing
// Structure of JSON is "Did_number":"[destination]"
// the destination is defined as string presentation of DestinationStruct
//Extension.. - routing to the extension (can be used with system extensions as well), forwarding rules are applied
//VoiceMail.<ringgroup,queue,extension number>. - voicemail of the specific entity.
// EG: "1222333":"Extension.100."

const string jsonFileName = "routes.json";

public override async Task StartAsync()
{
if (MyCall.Caller.DN is ExternalLine externalLine && MyCall.IsInbound)
{
var DIDNumber = MyCall.Caller.CalledNumber;

var cfdAppName = MyCall.CallHandler.MyDN.GetPropertyValue("CF_APP_NAME");
string cfdPath;

if (!string.IsNullOrEmpty(cfdAppName))
cfdPath = cfdAppName.Split('.')[0];
else
cfdPath = MyCall.CallHandler.MyDN.Number.Split('.')[0];

var filePath = System.IO.Path.Combine(MyCall.PS.GetParameterValue("IVRPROMPTPATH"), "Callflows", cfdPath, jsonFileName);
string jsonString = File.ReadAllText(filePath);
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);

if (data.TryGetValue(DIDNumber, out var route))
{
var has_valid_destination = DestinationStruct.TryParse(route, out var destination_struct);
//if destination is malformed, the call is not intercepted
if (has_valid_destination)
{
var result = await MyCall.RouteToAsync(destination_struct);
//we are here when forwarding is successful, so we set returned value to true and put log message to CallFlow service log
MyCall.Info($"{DIDNumber} has been redirected to {route} ({result}");
return true;
}
}
}
return false;
}
}
}

More Call Flow Scripts Available

We have a collection of call flow scripts on our website. Check them out and see how you can automate 3CX to fit your needs.