#nullable disable
using CallFlow;
using System;
using System.Linq;
using System.Threading.Tasks;
using TCX.Configuration;
using TCX.PBXAPI;
using System.Collections.Generic;
using System.IO;
namespace dummy
{
// KLM IT AG
// Script Name: MultiHolidayPromptScript
// Created by: Kilian Meister
// Last Modified: 2024-05-18
public class MultiHolidayPromptScript : ScriptBase<MultiHolidayPromptScript>
{
// Destination IVR extensions as variables for easy customization
private string forwardingExtension = "404";
private string fallbackExtension = "401"; // Fallback destination IVR extension
private string ausserhalbExtension = "401"; // Ausserhalb IVR extension
// Method to play the holiday prompt if applicable and then forward the call
async Task PlayHolidayPromptAndForward()
{
var ps = MyCall.PS as PhoneSystem; // Access the PhoneSystem object
var holidays = ps.GetAll<OfficeHoliday>().ToArray(); // Get all office holidays and convert to array
var today = DateTime.Today; // Get today's date
bool promptPlayed = false; // Flag to check if a prompt was played
// First loop: Attempt to play the specific holiday prompt
foreach (var holiday in holidays)
{
var holidayDate = new DateTime(today.Year, holiday.Month, holiday.Day); // Construct the holiday date for this year
if (holidayDate == today) // Check if today is a holiday
{
string holidayName = holiday.Name; // Get the holiday name
string promptFileName = $"{holidayName}.wav"; // Construct the filename for the prompt dynamically
// Check if a specific prompt file exists for the full holiday name
if (File.Exists(Path.Combine("/var/lib/3cxpbx/Instance1/Data/Ivr/Prompts/", promptFileName))) // Update the path as needed
{
// Play the specific holiday prompt
await MyCall.PlayPrompt(null, new[] { promptFileName }, PlayPromptOptions.Blocked);
promptPlayed = true;
break; // Exit the loop once the holiday prompt is played
}
}
}
// Second loop: Attempt to play partial matches if no specific prompt was played
if (!promptPlayed)
{
foreach (var holiday in holidays)
{
var holidayDate = new DateTime(today.Year, holiday.Month, holiday.Day);
if (holidayDate == today)
{
string holidayName = holiday.Name;
// Check for partial matches
if (holidayName.Contains("Brückentag"))
{
await MyCall.PlayPrompt(null, new[] { "Brückentag.wav" }, PlayPromptOptions.Blocked);
promptPlayed = true;
}
else if (holidayName.Contains("Interner Firmenanlass"))
{
await MyCall.PlayPrompt(null, new[] { "Interner Firmenanlass.wav" }, PlayPromptOptions.Blocked);
promptPlayed = true;
}
else if (holidayName.Contains("bevorstehender Feiertag"))
{
await MyCall.PlayPrompt(null, new[] { "bevorstehender Feiertag.wav" }, PlayPromptOptions.Blocked);
promptPlayed = true;
}
else if (holidayName.Contains("Ausserhalb"))
{
//await MyCall.PlayPrompt(null, new[] { "IVR-Ausserhalb.wav" }, PlayPromptOptions.Blocked);
promptPlayed = true;
forwardingExtension = ausserhalbExtension; // Set to Ausserhalb IVR extension
}
if (promptPlayed)
break; // Exit the loop once the holiday prompt is played
}
}
}
// Determine the forwarding destination
string destinationExtension = promptPlayed ? forwardingExtension : fallbackExtension;
// Forward the call to the determined IVR extension
var destination = new DestinationStruct(DestinationType.Extension, ps.GetDNByNumber(destinationExtension), null);
var routeResult = await MyCall.RouteToAsync(destination);
if (routeResult.FinalStatus == CallControlResult.CallRequestStatus.Success)
{
MyCall.Debug($"Call successfully forwarded to IVR with extension {destinationExtension}."); // Log success
}
else
{
MyCall.Critical($"Failed to forward the call to IVR. Reason: {routeResult.ReasonText}"); // Log failure with reason
}
}
// Main entry point for the script
public override async void Start()
{
try
{
await Task.Run(async () =>
{
try
{
MyCall.Debug($"Script start delay: {DateTime.UtcNow - MyCall.LastChangeStatus}"); // Log script start delay
MyCall.Debug($"Incoming connection {MyCall}"); // Log incoming connection details
// Play holiday prompt if applicable and forward the call
await PlayHolidayPromptAndForward();
MyCall.Info("MultiHolidayPromptScript Exit script"); // Log script exit
MyCall.Return(true); // Return success
}
catch
{
MyCall.Return(false); // Return failure in case of any exception
}
});
}
catch
{
MyCall.Return(false); // Return failure in case of any exception
}
}
}
}