Integrating SMS API in the Windows Sidebar
Oct 11thThis article presents the Send SMS Sidebar gadget and the integration of the Developer Garden Send SMS API in the Windows Sidebar using JavaScript.
Sidebar gadgets are popular mini-applications which are displayed in a bar on the Windows Desktop. Gadgets can perform useful tasks or display a variety of information (e.g., CPU usage or RSS feeds).
Windows Sidebar gadgets can be implemented in various script languages. JavaScript or VBScript are available for the application logic; the graphical interfaces are created with HTML and CSS. For any interested readers, there is a good tutorial in the MSDN Magazine Blog.
It is possible to access web services in gadgets by sending out asynchronous REST requests using the XMLHttpRequest object. Three steps are needed to launch the Developer Garden Send SMS API in the Sidebar:
1. Creating the XMLHttpRequest object
There are various ways of obtaining the XMLHttpRequest object depending on the version of Internet Explorer installed (which renders the gadget in the Sidebar).
function getXMLHTTP () {
var xmlhttp = null ;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
if (typeof xmlhttp.overrideMimeType != 'undefined')
xmlhttp.overrideMimeType('text/xml');
}
else
if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
throw "No XMLHTTP" ;
}
return xmlhttp;
}
2. Requesting the token
Authentication at the security token server is required in order to use the Developer Garden services. This takes place with the Base64-coded access data, in the form “User name: Password”. After a response is received from the security token server, the token is parsed and the SMS sending function is invoked.
function preSend(base64auth, environment, number, message, flash, originator, account) {
var xmlHttp = getXMLHTTP();
xmlHttp.open("GET","https://sts.idm.telekom.com/rest-v1/tokens/odg", true);
xmlHttp.setRequestHeader("Authorization", "Basic "+base64auth);
xmlHttp.send();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
// received, OK
if (xmlHttp.status == 200) {
// extract the token
var token_start = xmlHttp.responseText.indexOf("token\"");
token_start += 8;
var token_end = xmlHttp.responseText.indexOf("\"",token_start);
var token = xmlHttp.responseText.substring(token_start, token_end);
send(token, environment, number, message, flash, originator, account);
} else {
// error
var response;
if (xmlHttp.status == 0) {
response = "Falscher Login?";
} else {
var error;
var errorstart = xmlHttp.responseText.indexOf("message");
if (errorstart != -1) {
errorstart += 10;
var errorend = xmlHttp.responseText.indexOf("\"",errorstart);
error = xmlHttp.responseText.substring(errorstart, errorend);
} else {
error = "Unbekannter Fehler";
}
response = error;
}
// output the error message
document.getElementById('message').innerHTML = response;
}
} else {
// wait...
}
};
}
3. Invoking the SMS interface
As described in the documentation, the parameters “Environment”, “Number”, “Message”, “Flash”, “Originator” and “Account” are sent to the interface. The abovementioned token is used for authentication with the service.
After the request has successfully been sent out (readyState == 4), analysis of the API request can take place. Any errors arising must be intercepted here.
function sendmsg(token, environment, number, message, flash, originator, account) {
var xmlHttp = getXMLHTTP();
xmlHttp.open("POST","https://gateway.developer.telekom.com/p3gw-mod-odg-sms/rest/"+environment+"/sms", true);
xmlHttp.setRequestHeader("Authorization", "TAuth realm=\"https://odg.t-online.de\", tauth_token=\""+token+"\"");
xmlHttp.send("number="+number+"&message="+encodeURIComponent(message)+"&flash="+flash+"&originator="+originator+"&account="+account);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
// received, OK
// further processing
} else {
// wait...
}
};
}
The interface aggregation is now almost complete and it remains to create a small GUI along with the XML definition file.
Functions of the gadget
The Send SMS Sidebar gadget implements all of the functions offered by the Send SMS interface. Thus, for example, any alphanumerical sender ID or, via a validation function, a real telephone number can be used as the sender.
It is also possible to decide whether to send a “normal” or “Flash SMS”. Flash SMS appear directly in the display (not in the Inbox folder) and cannot be stored.
Other functions include: Using sub- accounts, requesting credit, an address book and logging, character counting and message compression. It is also possible to send SMS to a predefined recipient list (in CSV format).
Downloading and installing
The gadget can be downloaded here. To install, launch the .gadget file.
In order to view the source text, the file extension can be changed from .gadget to .zipped and the file can be extracted.
By: Daniel Buchheim


Markus Willner says: