Change messages with a C# script
In the basic- and full editions of Diafaan SMS Server it is possible to block or change individual messages with an internal C# script using the HTTP callback function. This is an easy way to manipulate the message parameters or to block certain messages. It is also possible to use HTTP callbacks with a local or remote PHP script but that is much slower than using a local C# script.
Enable HTTP callbacks
The first step is to enable HTTP callbacks in Diafaan SMS Server and set it up to use the C# script. The HTTP callbacks settings are available in the general options: 'Actions-Options-HTTP callbacks'.
For use with C# the 'Callback method' must be set to 'C# script'.
Edit the C# script
Diafaan SMS Server now uses the C# script for every message that a connector wants to send, each message that a gateway receives and for each messages that is written to the log or receives a status update. The default script returns 'result=1' for each message, this means that no messages are blocked and the message parameters are not changed:
// Diafaan SMS Server HTTP Callback C# skeleton script
// AddReference System.dll
// AddReference System.Web.dll
//
using System;
using System.Web;
using System.Collections.Specialized;
namespace DiafaanMessageServer
{
public class HTTPCallbackScript : IHttpCallbackScript
{
public void OnLoad()
{
//
// TODO: Add initialization code.
//
}
public void OnUnload()
{
//
// TODO: Add cleanup code, make sure to remove (Timer) event handlers here
//
}
public string OnHttpCallback(string httpCallbackQuery)
{
NameValueCollection parameterList;
parameterList = HttpUtility.ParseQueryString(httpCallbackQuery);
if (parameterList["action"] == "message_out") {
// TODO: Add code to handle the message_out HTTP callback
return "result=1";
}
else if (parameterList["action"] == "message_in") {
// TODO: Add code to handle the message_in HTTP callback
return "result=1";
}
else if (parameterList["action"] == "message_log") {
// TODO: Add code to handle the message_log HTTP callback
return "result=1";
}
else if (parameterList["action"] == "message_log_update") {
// TODO: Add code to handle the message_log_update HTTP callback
return "result=1";
}
else if (parameterList["action"] == "event_log") {
// TODO: Add code to handle the event_log HTTP callback
return "result=1";
}
else if (parameterList["action"] == "validate_user") {
// TODO: Add code to handle the validate_user HTTP callback
return "result=1";
}
else if (parameterList["action"] == "accept_connection") {
// TODO: Add code to handle the accept_connection HTTP callback
return "result=1";
}
// TIP: Use HttpUtility.UrlEncode to build the response string, e.g.:
// return "result=1" + "&message=" + HttpUtility.UrlEncode(@"Message: " + parameterList["message"]);
return "result=1";
}
}
}
Information about the parameters that can be used in the HTTP callback scripts is available in the online manual.
Script to change the message text
The following script changes the message text for each outgoing SMS message, it does not change received SMS messages and log updates:
// AddReference System.dll
// AddReference System.Web.dll
//
using System;
using System.Web;
using System.Collections.Specialized;
namespace DiafaanMessageServer
{
public class HTTPCallbackScript : IHttpCallbackScript
{
public void OnLoad() {}
public void OnUnload() {}
public string OnHttpCallback(string httpCallbackQuery)
{
NameValueCollection parameterList;
parameterList = HttpUtility.ParseQueryString(httpCallbackQuery);
if (parameterList["action"] == "message_out") {
// Change the message text to: "C# script: " plus the original message text
return "result=1&message=" + HttpUtility.UrlEncode("C# script: " + parameterList["message"]);
}
return "result=1";
}
}
}
Script to allow only French destination numbers
This script blocks all messages without a French destination number:
// AddReference System.dll
// AddReference System.Web.dll
//
using System;
using System.Web;
using System.Collections.Specialized;
namespace DiafaanMessageServer
{
public class HTTPCallbackScript : IHttpCallbackScript
{
public void OnLoad() {}
public void OnUnload() {}
public string OnHttpCallback(string httpCallbackQuery)
{
NameValueCollection parameterList;
parameterList = HttpUtility.ParseQueryString(httpCallbackQuery);
if (parameterList["action"] == "message_out") {
if ((parameterList["to"].StartsWith("+33")) || (parameterList["to"].StartsWith("33"))) {
// Allow this message to go through
return "result=1";
}
else {
// block this message
return "result=0";
}
}
return "result=1";
}
}
}
Script to block all inbound messages except from one number
This script blocks all inbound messages except messages from number +49111111111:
// AddReference System.dll
// AddReference System.Web.dll
//
using System;
using System.Web;
using System.Collections.Specialized;
namespace DiafaanMessageServer
{
public class HTTPCallbackScript : IHttpCallbackScript
{
public void OnLoad() {}
public void OnUnload() {}
public string OnHttpCallback(string httpCallbackQuery)
{
NameValueCollection parameterList;
parameterList = HttpUtility.ParseQueryString(httpCallbackQuery);
if (parameterList["action"] == "message_in") {
if (parameterList["from"] == "+49111111111") {
// Allow this inbound message to go through
return "result=1";
}
else {
// block this inbound message
return "result=0";
}
}
return "result=1";
}
}
}
Script to change the 'from' number format of inbound messages
This script removes the leading '+' characters of the sender phone number of inbound messages:
// AddReference System.dll
// AddReference System.Web.dll
//
using System;
using System.Web;
using System.Collections.Specialized;
namespace DiafaanMessageServer
{
public class HTTPCallbackScript : IHttpCallbackScript
{
public void OnLoad() {}
public void OnUnload() {}
public string OnHttpCallback(string httpCallbackQuery)
{
NameValueCollection parameterList;
parameterList = HttpUtility.ParseQueryString(httpCallbackQuery);
if (parameterList["action"] == "message_in") {
// remove leading '+' characters from the 'from' address
return "result=1&from=" + HttpUtility.UrlEncode(parameterList["from"].TrimStart('+'));
}
return "result=1";
}
}
}