JavaScript

From CDYNE Wiki
Jump to navigation Jump to search

JavaScript and Web Service

JavaScript is a widely-used general purpose scripting language that is specially designed to work on the Web and very well suited for web development. For this example we will consume a webservice with javascript. This example uses CDYNE's Phone Notify! Web Service.

Usage

The Status text areas are as follows:

  1. TopStatus = a div tag with an ID of TopStatus
  2. StartTime = a div with an ID of StartTime
  3. EndTime = a div with an ID of EndTime

Call the Dial function and the system will send out a CDYNE Notify!

Source Code

  1 function Dial(numbertodial, texttosay, callerid, CallerIDName,Voice, Key)
  2 {
  3 	
  4 
  5 	var url = "http://ws.cdyne.com/NotifyWS/PhoneNotify.asmx/NotifyPhoneBasic?PhoneNumberToDial=" + 
  6                   escape(numbertodial) + 
  7                   "&TextToSay=" + escape(texttosay) + 
  8                   "&CallerID=" + escape(callerid) + 
  9                   "&CallerIDname=" + escape(CallerIDName) + 
 10                   "&VoiceID=" + Voice + 
 11                   "&LicenseKey=" + Key;
 12 	request = new XMLHttpRequest();
 13 	request.onreadystatechange = myStatusProc;
 14 	request.open( "GET", url, true );
 15 	request.send();
 16 }
 17 
 18 function myStatusProc()
 19 {
 20 	if ( request.readyState == 4)
 21 	{
 22 		if ( request.status == 200 )
 23 		{
 24 				var XML = request.responseXML;
 25 				var nodeString = "";
 26 				
 27 				var nodeList = XML.documentElement.getElementsByTagName("ResponseCode");
 28 				var node = nodeList.item(0);
 29 				var RC = node.firstChild.nodeValue;
 30 				
 31 				if( RC==0 )
 32 				{
 33 					var nodeList = XML.documentElement.getElementsByTagName("QueueID");
 34 					var node = nodeList.item(0);
 35 					QueueID = node.firstChild.nodeValue;
 36     				        TopStatus.innerText = "Queued";
 37 
 38 					setTimeout("CheckNotify()",1000)
 39 				}
 40 				else
 41 				{
 42 					var nodeList = XML.documentElement.getElementsByTagName("ResponseText");
 43 					var node = nodeList.item(0);
 44 					var RT = node.firstChild.nodeValue;
 45 					TopStatus.innerText = RT;
 46 					else if(RC==13)
 47 					{
 48 						TopStatus.innerText = "CallerID and Number cannot match";
 49 					}
 50 				}
 51 		}
 52 	}
 53 }
 54 
 55 function CheckNotify()
 56 {
 57 	request = new XMLHttpRequest();
 58 	request.onreadystatechange = CheckProc;
 59 	request.open("GET", "http://ws.cdyne.com/NotifyWS/PhoneNotify.asmx/GetQueueIDStatus?QueueID=" + 
 60                      QueueID, true );
 61 	request.send();
 62 	
 63 }
 64 
 65 
 66 function CheckProc()
 67 {
 68     
 69 	if ( request.readyState == 4)
 70 	{
 71 		if ( request.status == 200 )
 72 		{
 73 				var XML = request.responseXML;
 74 				var nodeString = "";
 75 				
 76 				var nodeList = XML.documentElement.getElementsByTagName("ResponseCode");
 77 				var node = nodeList.item(0);
 78 				var RC = node.firstChild.nodeValue;
 79 				var nodeList = XML.documentElement.getElementsByTagName("ResponseText");
 80 				var node = nodeList.item(0);
 81 				var RT = node.firstChild.nodeValue;
 82 				var nodeList = XML.documentElement.getElementsByTagName("Duration");
 83 				var node = nodeList.item(0);
 84 				var dur = node.firstChild.nodeValue;
 85 				
 86 				TopStatus.innerText = RT;
 87 				
 88 				if( RC==1 )
 89 				{
 90 
 91 				    var nodeList = XML.documentElement.getElementsByTagName("MachineDetection");
 92 				    var node = nodeList.item(0);
 93 				    var MD = node.firstChild.nodeValue;
 94 				    TopStatus.innerText += " by " + MD;
 95 
 96 				    var nodeList = XML.documentElement.getElementsByTagName("StartTime");
 97 				    var node = nodeList.item(0);
 98 				    var ST = TimeStampToDate(node.firstChild.nodeValue);
 99 				    StartTime.innerText = "Start: " + ST.toLocaleTimeString();
100 
101 				}
102 				
103 				if( dur > 0 )
104 				{
105 					// stop timing and show EndTime
106 					var nodeList = XML.documentElement.getElementsByTagName("EndTime");
107 					var node = nodeList.item(0);
108 					var ET = TimeStampToDate(node.firstChild.nodeValue);
109 					EndTime.innerText = "End: " + ET.toLocaleTimeString();
110 				}
111 				else
112 				{
113 				     if (RC != 2)
114                                          t=setTimeout('CheckNotify()',3000) // every 3 seconds
115 				}
116 
117 		}
118 	}
119 }
120 
121 
122 function TimeStampToDate(xmlDate)
123 {
124     var dt = new Date();
125     var dtS = xmlDate.slice(xmlDate.indexOf('T')+1, xmlDate.indexOf('.'))
126     var TimeArray = dtS.split(":");
127     dt.setUTCHours(TimeArray[0],TimeArray[1],TimeArray[2]);
128     dtS = xmlDate.slice(0, xmlDate.indexOf('T'))
129     TimeArray = dtS.split("-");
130     dt.setUTCFullYear(TimeArray[0],TimeArray[1],TimeArray[2]);
131     return dt;
132 }