#nullable disable
using CallFlow;
using System;
using System.Threading;
using System.Linq;
using System.Threading.Tasks;
using TCX.Configuration;
using TCX.PBXAPI;
using System.Collections.Generic;
namespace dummy
{
//this handler plays holiday prompt as specified for destination
public class PlayDestinationHolidayPromptBeforeRouting : ScriptBase<PlayDestinationHolidayPromptBeforeRouting>
{
//reference implementation of trunk routing.
DestinationStruct FindDefaultDestination(ExternalLine trunk, string callerID, string DID)
{
DestinationStruct retval = new();
string[] range;
foreach (var a in trunk.RoutingRules)
{
bool match = (a.Conditions.Condition.Type == RuleConditionType.BasedOnDID &&
(
a.Data == DID
|| (a.Data.StartsWith('*') && DID.EndsWith(a.Data[1..]))
))
||
(
a.Conditions.Condition.Type == RuleConditionType.BasedOnCallerID &&
a.Data.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Any
(x =>
x == "*"
|| x == callerID
|| x.StartsWith('*') && callerID.EndsWith(x[1..])
|| x.EndsWith('*') && callerID.StartsWith(x[..^1])
|| x.StartsWith('*') && x.EndsWith('*') && callerID.Contains(x[1..^1])
|| ((range = x.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)).Length == 2 ?
(range[0].Length == callerID.Length && range[1].Length == callerID.Length) && (range[0].CompareTo(callerID) <= 0 && range[1].CompareTo(callerID) >= 0) : false)
)
)
||
(a.Conditions.Condition.Type == RuleConditionType.ForwardAll);
if (match)
{
retval.CopyFrom(a.ForwardDestinations.OfficeHoursDestination);
break;
}
}
return retval;
}
public override async Task<bool> StartAsync()
{
if(MyCall.Caller.DN is ExternalLine externalLine && MyCall.IsInbound) //only from trunk
{
//get the inbound target
var defaultDestination = FindDefaultDestination(externalLine, MyCall.Caller.CallerID, MyCall.Caller.CalledNumber).Internal;
//if the destination is Internal
if (defaultDestination != null)
{
var timeBasedroute = defaultDestination.GetTimeBasedRoutingInfo();
//if destination is currently "on holiday" and holiday prompt is defined, we play it
// if (timeBasedroute.reason == CallControlAPI.DivertReason.Holiday && !string.IsNullOrWhiteSpace(timeBasedroute.holiday?.HolidayPrompt))
// MODIFIED - REQUIRES UPLOAD OF "VACATION.wav"
if (timeBasedroute.reason == CallControlAPI.DivertReason.Holiday)
{
if (!string.IsNullOrWhiteSpace(timeBasedroute.holiday?.HolidayPrompt))
{
await MyCall.AssureMedia()
.ContinueWith(x => MyCall.PlayPrompt(null, [timeBasedroute.holiday?.HolidayPrompt], PlayPromptOptions.Blocked))
.Unwrap();
MyCall.Info($"timeBasedroute holiday PromptPlayed");
}
else
{
await MyCall.AssureMedia()
.ContinueWith(x => MyCall.PlayPrompt(null, new[] {"VACATION.wav"}, PlayPromptOptions.Blocked))
.Unwrap();
MyCall.Info($"VACATION.wav PromptPlayed");
// return true; // <<<=== STOP default routing procedure !!
}
}
}
}
return false;//we always return false to continue default trunk routing procedure.
}
}
}

Founded in 2005, when VoIP was an emerging technology, 3CX has gone on to establish itself as a global leader in business communications.