25 July 2010

Sending SMS using the HTTP based SMS API from QTP

We already know how to send emails from QTP for reporting purposes. I was wondering how wonderful it is going to be if we can send SMS from QTP so it does not matter whether you have access to emails or not, you can still know the status of your email via SMS on your mobile phone. I am assuming that 99% of test professionals carry a mobile phone with them at all times. It is really very easy to send messages from QTP using  a SMS API provided by a SMS provider. You can make calls to the web based SMS API of a provider via XML-HTTP API provided by Microsoft.


Before we could really start, we first need to look for SMS providers that also provide a HTTP API access. If you Google on the web, you will find numerous paid sms services backed by a SMS API. However, after days of searching on the internet, I came across a website called "www.freesmsapi.com". They provide free SMS API for developers and innovators for their startup project. So, I signed up with them which gave me access to their SMS API with credits for sending 100 SMS per day. However, to signup you would be required to provide them your company domain address and your email address with the same domain. Like, i signed for "learnqtp.info" and "info@learnqtp.info".


Let's review the code for sending SMS. The API call consists of a single URL where you have to provide the API Key, the actual SMS string which should be url-encoded and the mobile phone number the SMS is intended for. I have provided the function for url-encoding in the code so you can directly use it. The important information in the API call URL is the API key. You will receive your API Key once you setup your account with freesmsapi.com. Once the code runs, it takes about 1-2 minutes for the SMS to arrive to your mobile phone.


Also, if you are looking for a paid option and serious implementation of the SMS functionality in your code, you have numerous better and premium services with faster delivery and reliability.

Code - Tried and Tested, "YOUR-API-KEY" below in strURL needs to be replaced by your own FreeSMSAPI.com's API Key.


SendSMS "9561800000", "This is a test message from learnqtp.info using the free api from http://www.freesmsapi.com"

 Function URLEncode (strSMS)
    If IsNull(strSMS) then
    URLEncode = ""
    exit function
    End if
    URLEncode = strSMS
    URLEncode = Replace(URLEncode, chr(0), "")
    URLEncode = Replace(URLEncode, "%", "%25")
    URLEncode = Replace(URLEncode, " ", "%20")
    URLEncode = Replace(URLEncode, """", "%22")
    URLEncode = Replace(URLEncode, "&", "%26")
    URLEncode = Replace(URLEncode, "'", "%27")
    URLEncode = Replace(URLEncode, "/", "%2F")
    URLEncode = Replace(URLEncode, "?", "%3F")
    URLEncode = Replace(URLEncode, "\", "%5C")
 End function

 Function SendSMS(strPhone, strSMSMessage)
   SMS = URLEncode(strSMSMessage)
    
      Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")

   strURL = "http://s2.freesmsapi.com/messages/send?skey=YOUR-API-KEY&message=" & SMS & "&recipient=" & strPhone
  
   temp = WinHttpReq.Open("POST", strURL, false)
   WinHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   WinHttpReq.Send()
  
   strResult = WinHttpReq.ResponseText
  
   Print strURL
   Print strResult
 End Function

I hope the code above helps you in your day to day business scenario. For any queries. do comment back here or on the facebook group or fan page.

8 comments:

  1. good one dude.. keep up the good work

    ReplyDelete
  2. thank you so much for the post.

    ReplyDelete
  3. hi friends this great site.now i introduce a new site for sms fans www.freesmsapi.com .this is a grate for the people who generally connect with sms.i am sure once you visit you will visit regularly.just go there and check then tell me i am right or wrong.Thanks.I like to thanks owner of this site for sharing resources and information.Thanks

    ReplyDelete
  4. Another way of sending SMS

    http://bharath-marrivada.blogspot.com/2010/03/qtp-send-sms-to-your-mobile.html

    ReplyDelete
  5. Thanks for the sharing.

    http://qualitypointtech.net/ebook/index.php

    ReplyDelete
  6. Hi ,

    I use the above code and passed the number and message into that code but it does not work means i did not get any message at my number so please help and tell me format of mobile number

    ReplyDelete
  7. I am getting:

    Invalid Sub-domain

    Could you please help me on this.

    ReplyDelete
  8. this code is running butnot getting sms on mobile number

    ReplyDelete