Sending Messages in C#

The MessageMedia Messages API can handle sending messages from a C# console or web application. You can use this to keep track of message statuses in a database to log the final result.

This article shows you how to send messages from the Messages API and then get status in C#.

Prerequisites

Before you start, you need:

Installing MessageMedia Messages Package

  • Open Visual Studio -> Project -> Visual C# -> .NET Core -> Console App (.NET Core)
  • Name the app Test
  • On the navigation bar, click on Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution
  • Click on the Browse tab and search for “messagemedia messages”
  • Select the first option which should be MessageMedia.SDK.Messages
  • On the mini window that appears on the right side, select/tick your project and click on Install
  • You should see the following appear in your Solution Explorer after a successful installation

Send Messages

We’re going to show you step-by-step how to send a message and handle webhooks.

The first step is to add the API references in your using statements. Add the following using statements to the Program.cs file:

using MessageMedia.Messages;
using MessageMedia.Messages.Controllers;
using MessageMedia.Messages.Models;

Before you can use the Messages API, you must configure it using your API key and secret that were sent to you when you signed at the Developer Portal.

String basicAuthUserName = "YOUR_API_KEY";
String basicAuthPassword = "YOUR_API_SECRET";
bool useHmacAuthentication = false; //Change this to true if you are using HMAC keys

You then need to create an instance of the client:

MessageMediaMessagesClient client = new MessageMediaMessagesClient(basicAuthUserName, basicAuthPassword, useHmacAuthentication);
IMessagesController messages = client.Messages;

After you configure the API, you can create a message as a JSON object.

string bodyValue = @"{
     ""messages"":[
        {
          ""content"":""Greetings from MessageMedia!"",
          ""destination_number"":""YOUR_MOBILE_NUMBER""
        }
     ]
}";

This JSON object is then going to be deserialized into .NET object before being passed to the main CreateSendMessages method.

var body = Newtonsoft.Json.JsonConvert.DeserializeObject<MessageMedia.Messages.Models.SendMessagesRequest>(bodyValue);

MessageMedia.Messages.Models.SendMessagesResponse result = messages.CreateSendMessages(body);

For the purpose of the tutorial, the JSON object has the minimal number of parameters but in reality, it can have several. For a list of parameters and what they do, check the MessageMedia documentation for Messages here.

Putting all the code together, this is the entire code snippet:

using System;
using MessageMedia.Messages;
using MessageMedia.Messages.Controllers;
using MessageMedia.Messages.Models;

namespace Test {
    class Program {
        static void Main(string[] args) {
            String basicAuthUserName = "YOUR_API_KEY";
            String basicAuthPassword = "YOUR_API_SECRET";
            bool useHmacAuthentication = false; 
            
            MessageMediaMessagesClient client = new MessageMediaMessagesClient(basicAuthUserName, basicAuthPassword, useHmacAuthentication);
            IMessagesController messages = client.Messages;

            string bodyValue = @"{
                                   ""messages"":[
                                      {
                                         ""content"":""Greetings from MessageMedia!"",
                                         ""destination_number"":""YOUR_MOBILE_NUMBER""
                                      }
                                   ]
                                }";

            var body = Newtonsoft.Json.JsonConvert.DeserializeObject<MessageMedia.Messages.Models.SendMessagesRequest>(bodyValue);
            MessageMedia.Messages.Models.SendMessagesResponse result = messages.CreateSendMessages(body);
            Console.WriteLine(result.Messages);
            Console.ReadKey();
        }
    }
}

Don’t forget to update the placeholders in the code i.e – API_KEY, API_SECRET & MOBILE_NUMBER. You can try sending a message by clicking on Debug from the navigation bar -> Start Debugging and a successful response should look like:

Getting Status

The submitted message object is usually queued. You wouldn’t know when and if the message was actually sent from the initial API response. That’s where the GetMessageStatus method is useful.

This is very similar to the way we sent a message earlier. This is what the code looks like:

using System;
using MessageMedia.Messages;
using MessageMedia.Messages.Controllers;
using MessageMedia.Messages.Models;

namespace Test {
    class Program {
        static void Main(string[] args) {
            String basicAuthUserName = "YOUR_API_KEY";
            String basicAuthPassword = "YOUR_API_SECRET";
            bool useHmacAuthentication = false; //Change this to true if you are using HMAC keys
            
            MessageMediaMessagesClient client = new MessageMediaMessagesClient(basicAuthUserName, basicAuthPassword, useHmacAuthentication);
            IMessagesController messages = client.Messages;

            string messageId = "YOUR_MESSAGE_ID";
            dynamic result = messages.GetMessageStatus(messageId);

            string msg = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            Console.WriteLine(msg);
        }
    }
}

You need to use a valid message ID to get the status of a message. You can get this from the response of the message that you sent earlier.
After updating the placeholders and running the code, this is the response I got:

 

To find out more about the Messages API, visit https://developers.messagemedia.com/code/messages-api-documentation/