C#.Net SMS Script

In the Ozeki SMS Gateway you can write a script, that allows you to run C# .NET code when an SMS message arrives. This script can implement any logic to do various tasks with the incoming messages, and optionally send response SMS messages.

Step 1 - Add new user/application...

You can simply install the C# script User on the Management console by clicking Add new user/application... in the Users/Applications panel (Figure 1).

add new user or application
Figure 1 - Add new user/application...

Step 2 - Add C# script

An interface will open consisting of two panels. The left side panel contains the already installed users and applications. The right-side panel contains the users and applications you can install with a brief description next to them. Search the C# script User and click the blue 'install' button next to it (Figure 2).

add c sharp script
Figure 2 - Add C# script

Step 3 - Provide the script

The 'Configuration' panel has a 'General' tab which contains the basic settings. First please provide a unique Name. In the Script section of tab page contains the source of the script you wish to execute (Figure 3).

provide the script
Figure 3 - Provide the script

Step 4 - Enable user

Please enable to user with the Connection switch and in the Events tab view that the Script is compiled, and the user is initialized successfully (Figure 4).

enable user
Figure 4 - Enable user

Step 5 - Message received

Finally, you can see if a message is received by this user the script will run and as this example works the response SMS message is sent to the Original sender (Figure 5).

message received
Figure 5 - Message received

ASP C# programming guide

Create a class named 'Program' in the 'Ozeki' namespace. You may also include the necessary using directives.

using System;
​
namespace Ozeki
{ 
    public class Program 
    {
    }
}

Implement the 'Receive' function inside the 'Program' class. This function will be called when your connection receives a message. The parameter of the 'Receive' function will be an object with the type of 'Message'.

using System;
​
namespace Ozeki
{ 
    public class Program 
    {
        public void Receive(Message msg)
        {
        }
    }
}

Send a reply message

When receiving a message, you may send a reply to notify your sender of successful delivery. To achieve this, you may use the built-in 'Reply' function.

using System;
​
namespace Ozeki
{ 
    public class Program 
    {
        public void Receive(Message msg)
        {
            Reply(msg, "Thank you for the message!");
        }
    }
}

Message forwarding

You may also forward your received message to another connection by using the 'Send' function.

using System;
​
namespace Ozeki
{ 
    public class Program 
    {
        public void Receive(Message msg)
        {
            Send("admin@localhost", msg.Text);
        }
    }
}

Compose your message

Composing a message is easier, than you think. You just need to use the built-in 'Message' type to create a new message object. In the following example, when a message was received, we will create and send a new message to the admin connection.

using System;
​
namespace Ozeki
{ 
    public class Program 
    {
        public void Receive(Message msg)
        {
            var message = new Message();
            message.Text = "Hello World!";
            message.ToAddress = "+4412345678910";
            message.ToConnection = "admin@localhost";
            message.FromAddress = "+448888999910";
          
            Send(message);
        }
    }
}

Logging

Using log messages will make debugging your script much easier. By calling the 'Log' function you may implement proper logging in your script if needed.

using System;
​
namespace Ozeki
{ 
    public class Program 
    {
        public void Receive(Message msg)
        {
            Log("Message received: " + msg.Text);
        }
    }
}

Summary

In the article above, you have learned important thing about running a C# .NET code using the Ozeki SMS Gateway. Combining the power of the Ozeki SMS Gateway and a C#.NET script will result in a useful and versatile messaging system. If you can run a C#.NET code when an SMS message arrives, you can implement logics to do perform any task. For example, a good task would be to send a scripted response to the incoming message. Another good example is to send notification messages inside your organization in case of an incoming message.

To find out more information about the C# SMS API, follow the link to the article about this topic on the Ozeki Webpage : C# SMS API. Ozeki has other articles about this topic. For example, C# HTTP SMS.

To start working, all you need to do is download the Ozeki SMS Gateway now!

More information