07 June 2015

Using Telstra API to send SMS in ASP.NET

Telstra (https://dev.telstra.com/) is pushing their own API and as a part of the plan, it is giving away 1000 free SMS per month with the limit of 100 per day.

After a simple register and set up, wait about 1 hour for get "MyAPP" approved, it is ready to go.



It requests an access token with your own "key" and "secret" first which expires in 1 hour. Then we need to pass in this token with all the request we are going to make.
The documentation is simple, straight forward and all the sample code is hosted on github. I write a quick C# to test it out and it works well!

public string GetAccessToken()
{
    const string consumerKey = "{yourkey}";
    const string consumerSecret = "{secret}";
    string url =string.Format("https://api.telstra.com/v1/oauth/token?client_id={0}&client_secret={1}&grant_type=client_credentials&scope=SMS", consumerKey, consumerSecret);

    using (var webClient = new System.Net.WebClient())
    {
        var json = webClient.DownloadString(url);
        var obj = JObject.Parse(json);
        return obj.GetValue("access_token").ToString();
     }
}
public string SendSms(string token, string recipientNumber, string message)
{
     try
     {
        using (var webClient = new System.Net.WebClient())
        {
            webClient.Headers.Clear();
            webClient.Headers.Add(HttpRequestHeader.ContentType, @"application/json");
            webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);

            string data = "{\"to\":\"" + recipientNumber + "\", \"body\":\"" + message + "\"}";
            var response = webClient.UploadData("https://api.telstra.com/v1/sms/messages", "POST", Encoding.Default.GetBytes(data));
            var responseString = Encoding.Default.GetString(response);
            var obj = JObject.Parse(responseString);
            return obj.GetValue("messageId").ToString();
                    // Now parse with JSON.Net
        }
     }
     catch (Exception e)
     {
        Console.WriteLine(e.Message);
     }
        return string.Empty;
 }

12 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. you might not even be around a TV. our other option for watching Television on a regular cable or satellite box is to do so online hotstickybun

    ReplyDelete
  3. Everyone carries their mobile wherever they go and involuntarily read any new incoming message. So your ad message will be immediately viewed and so SMS marketing is particularly useful for time-sensitive marketing campaigns.
    http://www.padisoft.org

    ReplyDelete
  4. In today's competitive business arena, you need to have a regular interaction with the customers to avoid losing potential customers padisoft.org

    ReplyDelete
  5. The fact that SMS also provides an enormous opportunity for companies to interact with their customers, is not really well known.padisoft.org

    ReplyDelete
  6. Along these lines, with the huge development in the versatile gaming industry, up and coming engineers are balanced with the unavoidable issue of picking amongst iOS and Android diversion improvement. www.adlist24.com

    ReplyDelete
  7. What could fill somebody's heart with joy other than perusing such perfectly created blog, for example, this.
    best reverse phone number lookup review

    ReplyDelete
  8. When it comes to typical tech enterprising, a business is either an SME or a startup, categorically.best tablets under 302

    ReplyDelete
  9. I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. vpnveteran

    ReplyDelete
  10. Awesome bit of composing, I extremely loved the manner in which you featured some extremely critical and huge focuses like SMS Sending Using ASP.NET. I value your work.Bulk SMS API

    ReplyDelete
  11. Impressive web site, Distinguished feedback that I can tackle. I am moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards maggiori informazioni

    ReplyDelete
  12. This is very interesting content! I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are great. gbwhatsapp ios

    ReplyDelete

WFFM Web form for marketer fail to submit in Sitecore

I have setup a standard form using WFFM in Sitecore 8.2. It always fail when submit. Error as below, Error: Exception: System.Data.DataE...