Difference between revisions of "CSharp Receiving SMS"

From SMS Wiki
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 4: Line 4:
  
 
Echo.ashx
 
Echo.ashx
 +
 
<source lang="csharp">
 
<source lang="csharp">
 +
 
<%@ WebHandler Language="C#" Class="Echo" %>
 
<%@ WebHandler Language="C#" Class="Echo" %>
  
 
using System;
 
using System;
 +
 
using System.Web;
 
using System.Web;
 +
 
using System.Web.Services.Protocols;
 
using System.Web.Services.Protocols;
 +
 
using com.upsidewireless.api.sms;
 
using com.upsidewireless.api.sms;
 +
  
 
public class IncomingMessage
 
public class IncomingMessage
Line 167: Line 173:
 
}
 
}
 
</source>
 
</source>
 +
 +
 +
----
 +
[[APIs | back]]

Latest revision as of 15:27, 12 August 2009

This example is a .NET Generic Handler. It demonstrates the reception of an inbound (HTTP Forwarded) SMS.

To emulate an inbound SMS forwarded via HTTP POST, please see the code block following the HTTP Handler code.

Echo.ashx

<%@ WebHandler Language="C#" Class="Echo" %>
 
using System;
 
using System.Web;
 
using System.Web.Services.Protocols;
 
using com.upsidewireless.api.sms;
 
 
public class IncomingMessage
{
    public string username;
    public string sender;
    public string contents;
    public SmsEncoding encoding;
    public string carrierCode;
    public string recipient;
}
 
public class OutgoingMessage
{
    public string dedicatedNumber;
    public string recipient;
    public string carrierCode;
    public string contents;
    public SmsEncoding encoding;
 
    public override string ToString()
    {
        return "[" + recipient + "]";
    }
}
 
public class Echo : IHttpHandler
{
 
    System.Diagnostics.EventLog evenLogger = new System.Diagnostics.EventLog("SMS Echo");
 
    public Echo()
    {
        evenLogger.Source = "SMS Echo Test";
    }
 
    public void ProcessRequest(HttpContext context)
    {
        // This sample will respond to incoming HTTP POSTs and send back to the sender the same text that was sent
        string requestType = context.Request.RequestType;
        switch (requestType)
        {
            case "POST":
 
                IncomingMessage incomingMessage = buildIncomingMessage(context);
                OutgoingMessage outgoingMessage = processIncomingMessage(incomingMessage);
                sendMessage(outgoingMessage);
 
                context.Response.Write("Successfully sent echo back to: " + outgoingMessage);
                break;
 
            default:
                context.Response.Write("This handler only accepts HTTP operations of type POST");
                break;
        }
    }
 
    private OutgoingMessage processIncomingMessage(IncomingMessage incomingMessage)
    {
        // TODO Build an OutgoingMessage object based on data contained within the incomingMessage.
        OutgoingMessage result = new OutgoingMessage();
 
        string message = "Time:" + DateTime.Now + "\n";
        message += "Username:" + incomingMessage.username + "\n";
        message += "From:" + incomingMessage.sender + "\n";
        message += "To:" + incomingMessage.recipient + "\n";
        message += "Encoding:" + incomingMessage.encoding.ToString() + "\n";
        message += "Carrier Code:" + incomingMessage.carrierCode + "\n";
        message += "Message:" + incomingMessage.contents;
 
        result.recipient = incomingMessage.sender;
        result.contents = message;
        result.encoding = incomingMessage.encoding;
        result.carrierCode = incomingMessage.carrierCode;
        result.dedicatedNumber = incomingMessage.recipient;
 
        return result;
    }
 
    private IncomingMessage buildIncomingMessage(HttpContext context)
    {
        IncomingMessage result = new IncomingMessage();
 
        result.username = context.Request.Form["name"];
        result.sender = context.Request.Form["sender"];
        result.contents = context.Request.Form["data"];
        string encodingString = context.Request.Form["encoding"];
        result.carrierCode = context.Request.Form["carriercode"];
        result.recipient = context.Request.Form["inboundnumber"];
 
        SmsEncoding smsEncoding = SmsEncoding.Seven;
 
        // we need to convert the encoding over to a strongly typed variable
        switch (encodingString)
        {
            case "Sixteen":
                smsEncoding = SmsEncoding.Sixteen;
                break;
        }
 
        result.encoding = smsEncoding;
 
        evenLogger.WriteEntry("Incoming Message Received", System.Diagnostics.EventLogEntryType.Information, 500, 0);
 
        return result;
    }
 
 
    private void sendMessage(OutgoingMessage outgoingMessage)
    {
        // TODO: be sure to change the data stored within the resource before using this function!
 
        string token = Resources.UpsideWirelessCredentialsResource.token;
        string signature = Resources.UpsideWirelessCredentialsResource.signature;
 
        SMS sms = new SMS();
 
        try
        {
            SMSSendResult result = null;
 
            if (String.IsNullOrEmpty(outgoingMessage.carrierCode) || String.IsNullOrEmpty(outgoingMessage.dedicatedNumber))
            {
                // standard routing
                result = sms.Send_Plain(token, signature, outgoingMessage.recipient, outgoingMessage.contents, outgoingMessage.encoding);
            }
            else
            {
                // dedicated routing
                result = sms.Send_Plain_Dedicated(token, signature, outgoingMessage.recipient, outgoingMessage.contents, outgoingMessage.encoding, outgoingMessage.carrierCode, outgoingMessage.dedicatedNumber, 0.0);
            }
 
            if (result.isOk)
            {
                evenLogger.WriteEntry("Sent SMS: " + outgoingMessage, System.Diagnostics.EventLogEntryType.Information, 0, 0);
            }
            else
            {
                evenLogger.WriteEntry("Unable to send SMS: " + outgoingMessage, System.Diagnostics.EventLogEntryType.Error, 1000, 0);
            }
        }
        catch (SoapException e)
        {
            evenLogger.WriteEntry("Fatal: Unable to send SMS: " + outgoingMessage + " Exception: " + e.Detail.InnerText, System.Diagnostics.EventLogEntryType.Error, 5000, 0);
        }
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
}



back