Dispatch SMS to gateways

<< Click to Display Table of Contents >>

Navigation:  How to... > Scripting examples > Gateway scripting examples >

Dispatch SMS to gateways

Learn how to use a C# or Visual Basic .NET script with Diafaan SMS Server to send an SMS message through a specific gateway, depending on the GSM number.



In this scenario three gateways are defined, GSM Modem 1, GSM Modem 2 and Clickatell. SMS messages to British GSM numbers should be sent with GSM Modem 1 and messages to French GSM numbers to GSM Modem 2. All messages to other destinations should be rejected. For both destinations the Clickatell gateway is used as a backup gateway. The maximum send attempts on both GSM modems is 2, this is indicated in the script with "GSM Modem 1:2" and "GSM Modem 2:2".



The only adaptation to the skeleton script is in the OnMessageToSend event:


C# script:


private void OnMessageToSend(string recordId, string toAddress, string fromAddress, 
                             string message, string messageType)
  if (toAddress.StartsWith("+44")) {
    PostDispatchMessage(recordId, toAddress, fromAddress, message, messageType, 

                        "GSM Modem 1:2", "Clickatell");
  else if (toAddress.StartsWith("+33")) {
    PostDispatchMessage(recordId, toAddress, fromAddress, message, messageType, 

                        "GSM Modem 2:2", "Clickatell");
  else {
    PostSendResult(recordId, "", StatusCode.SendError, 

                   "Error: Invalid country destination", "", "", false);         


Visual Basic .NET script:


Private Sub OnMessageToSend(recordId As String, toAddress As String, _

                            fromAddress As String, message As String, _

                            messageType As String)
  If toAddress.StartsWith("+44") Then
    PostDispatchMessage(recordId, toAddress, fromAddress, message, messageType, _

                        "GSM Modem 1:2", "Clickatell")
  ElseIf toAddress.StartsWith("+33") Then
    PostDispatchMessage(recordId, toAddress, fromAddress, message, messageType, _

                        "GSM Modem 2:2", "Clickatell")
    PostSendResult(recordId, "", StatusCode.SendError, _

                   "Error: Invalid country destination", "", "", False)
  End If
End Sub


If you make any change to the script make sure that the PostSendResult or PostDispatchMessage is always called, even if an exception is caused. Otherwise the script will only continue with the next message after a timeout of several minutes.



To enhance performance when a large amount of messages is sent, you can set the MaximumBatchSize in the Advanced properties to a higher value.