Impementing Clickatell CallBack in ASP.NET using C

2019-06-06 22:11发布

问题:

I am building a web application in ASP.NET using C#.NET which sends a SMS using clickatell.

I am trying unsuccessfully to receive a status back from clickatell by creating a CallBack Page called ClickatellCallBack.aspx Here is the codebehind of the Page:

public partial class ClickatellCallBack : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Retrieve the values from the QueryString.
string apiID = Request.QueryString["api_id"];
string from = Request.QueryString["from"];
string to = Request.QueryString["to"];
string timestamp = Request.QueryString["timestamp"];
string apiMsgId = Request.QueryString["apiMsgId"];
string cliMsgId = Request.QueryString["cliMsgId"];
string status = Request.QueryString["status"];
string charge = Request.QueryString["charge"];

   // Insert the SMS Status values into the database.  
   int smsStatusID = SMSManager.InsertSMSStatus(apiID, from, to,
      timestamp, apiMsgId, cliMsgId, status, charge);  

}
}

Basically, this Page retrieves the Query String values sent from clickatell and inserts them into a database table.

I have registered the following Callback URL: http://www.mydomain.com/ClickatellCallBack.aspx with clickatell and selected the Callback Type: HTTP GET

In my 'sendmsg' command I set delivery acknowledgement and callback request as follows: deliv_ack=1 and callback=3

The only problem being that nothing appears to be happening. The CallBack URL doesn't appear to be reached by clickatell.

Am I missing something? Am I doing something wrong. Do I need to implement this Callback URL using something other than an ASP.NET Page? Is there some clickatell setting I'm missing?

Any help would be greatly appreciated.

Regards

Walter

回答1:

I solved the problem by placing the callback and deliver_ack parameters with the startbatch command and not the quicksend command. This seems to work. So here is what I have inside the C#.NET function that starts the batch:

protected string get_batch_id(string session_id, string message_body)
{
    // Declare a WebClient.
    WebClient client = new WebClient();

    // Add a User Agent Header.
    client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");

    // Add the session_id to the Query String.
    client.QueryString.Add("session_id", session_id);

    // Add the template to the Query String.
    client.QueryString.Add("template", message_body);

    // Add the callback to the Query String.
    client.QueryString.Add("callback", "3");

    // Add the deliv_ack to the Query String.
    client.QueryString.Add("deliv_ack", "1");

    // Declare the baseurl.
    string baseurl = "http://api.clickatell.com/http_batch/startbatch";

    // Open the baseurl.
    Stream data = client.OpenRead(baseurl);

    // Declare and instantiate a StreamReader to retrieve the results of executing the startbatch command.
    StreamReader reader = new StreamReader(data);

    // Parse and split the returned string into the returned_strings array.
    string[] returned_strings = reader.ReadToEnd().Split(' ');

    // Retrieve the batch_id from the (second element of the) returned_strings array.
    string batch_id = returned_strings[1].ToString();

    // Close the Stream.
    data.Close();

    // Close the Reader.
    reader.Close();

    // Return the batch_id.
    return (batch_id);
}

Phew!

So I've also managed to get the following functionality successfully coded up in ASP.NET using C#.NET:

  1. Send a message to a single recipient;
  2. Send the same message to multiple recipients;
  3. Send a personalised message to a single recipient;
  4. Send a personalised message to multiple recipients;

Along the way I've noticed that there aren't too many cliackatell code samples in ASP.NET using C#.NET.