- Joined
- Oct 1, 2024
- Messages
- 4
- Reaction score
- 1
Hello,
i spend some Time to make A script that fires up on Call on a Trunk. The Goal is Trigger Tellows Service what is a Service in Germany what is Scoring Phonenumbers and get it Spamscore. If the Score is Higher than 5 it should route the Call to an Destination Number.
But it does not Work and i dont know why. But Honestly i am not very Familiar with 3CX Advanced Featues.
Also my Logging Function is not working i got this idea from from https://www.3cx.com/community/threads/how-to-write-logs-at-external-script.54757/
i spend some Time to make A script that fires up on Call on a Trunk. The Goal is Trigger Tellows Service what is a Service in Germany what is Scoring Phonenumbers and get it Spamscore. If the Score is Higher than 5 it should route the Call to an Destination Number.
But it does not Work and i dont know why. But Honestly i am not very Familiar with 3CX Advanced Featues.
Also my Logging Function is not working i got this idea from from https://www.3cx.com/community/threads/how-to-write-logs-at-external-script.54757/
C#:
using CallFlow;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
using TCX.Configuration;
using TCX.PBXAPI;
namespace dummy
{
public class CallHandler : ScriptBase<CallHandler>
{
// Variables for Tellows API and spam forwarding
private static string tellowsApiKey = "your_api_key"; // API key for Tellows
private static string tellowsPartnerID = "your_partner_id"; // Partner ID for Tellows
private static int tellowsThreshold = 7; // Threshold for spam detection (1-10)
private static string spamDestinationNumber = "+1234567890"; // Destination number for spam calls
PhoneSystem ps = null;
public CallHandler()
{
ps = PhoneSystem.Root;
if (ps == null)
{
Log("Error: Phone system not initialized");
}
}
// Method for logging messages
public static void Log(string text)
{
string fileName = "/var/lib/3cxpbx/Instance1/Data/Logs/tellows.log";
string logMessage = String.Format("[{0}]: {1}", DateTime.Now.ToString(), text + Environment.NewLine);
File.AppendAllText(fileName, logMessage);
}
// Example method to process an incoming call
public async Task HandleIncomingCall(ICall myCall)
{
try
{
// Log the CallerID and check if it is valid
string callerID = myCall.Caller?.CallerID;
if (string.IsNullOrEmpty(callerID))
{
Log("Error: CallerID is empty or invalid.");
return;
}
Log($"Incoming call from: {callerID}");
// Use Tellows API to analyze the caller
int spamScore = await GetSpamScore(callerID);
// If the spam score is equal to or greater than the threshold, mark it as spam
if (spamScore >= tellowsThreshold)
{
Log($"Spam detected! Score: {spamScore}. Forwarding to spam destination.");
await RouteCallToSpamDestination(myCall);
}
else
{
Log($"No spam detected. Score: {spamScore}. Processing call normally.");
}
}
catch (Exception ex)
{
Log($"Error during call processing: {ex.Message}");
}
}
// Method to fetch the spam score from Tellows (XML Parsing)
private async Task<int> GetSpamScore(string callerID)
{
try
{
// Build the Tellows API URL with the relevant parameters
string apiUrl = $"https://www.tellows.de/basic/num/{callerID}?xml=1&partner={tellowsPartnerID}&apikey={tellowsApiKey}";
Log($"Making API request to Tellows: {apiUrl}");
using (HttpClient client = new HttpClient())
{
Log($"Sending API request to Tellows: {apiUrl}");
HttpResponseMessage response = await client.GetAsync(apiUrl);
Log($"API response status: {response.StatusCode}");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Log($"Tellows API response: {responseBody}");
// Load the XML data and extract the spam score
XDocument xmlResponse = XDocument.Parse(responseBody);
// Extract the spam score from the XML response
XElement scoreElement = xmlResponse.Root.Element("score");
if (scoreElement != null)
{
int spamScore = int.Parse(scoreElement.Value);
Log($"Spam score from Tellows for {callerID}: {spamScore}");
return spamScore;
}
else
{
Log($"Spam score not found in response for {callerID}");
return 0; // Default to no spam if no score found
}
}
}
catch (HttpRequestException httpEx)
{
Log($"HTTP error during spam score retrieval: {httpEx.Message}");
return 0; // Default value if API request fails
}
catch (Exception ex)
{
Log($"General error during spam score retrieval: {ex.Message}");
return 0; // Default value if an error occurs
}
}
// Method to forward spam calls
private async Task RouteCallToSpamDestination(ICall myCall)
{
try
{
// Convert the destination number into a DN object
DN spamDestination = PhoneSystem.Root.GetDNByNumber(spamDestinationNumber);
if (spamDestination == null)
{
Log("Spam destination number could not be resolved.");
return;
}
// Use the ReplaceWithAsync method to forward the call
CallControlResult result = await myCall.ReplaceWithAsync(new DestinationStruct(spamDestination));
if (result != null) // Check if the result is not null
{
Log("Call successfully forwarded to spam destination.");
}
else
{
Log("Failed to forward call to spam destination.");
}
}
catch (Exception ex)
{
Log($"Error during call forwarding: {ex.Message}");
}
}
}
}
Last edited: