Send SMS from Command Line

Posted by on Jul 13, 2004 in Support Blog

Topic Keywords:

A command line interface can sometimes be useful for quick sending of an SMS message from another application.

A message thread on our discussion board provided a simple example that has been referenced frequently. The purpose of this document is to provide an updated script which offers more flexibility. This updated script allows recipient phone numbers, and the message text, to be specified on the command line.

Assuming that the script is saved as a file named sms.js, you would issue the following command:

cscript sms.js PhoneNumber1[,PhoneNumber2,...] SMS Message Text

(cscript.exe is the Windows Script Host, which is a component of Windows which should be located in the \Windows\System32 directory.)

Examples:

cscript sms.js +44777777777 This is a test message

cscript sms.js +44777777777,+44777777778 This is a test message to 2 recipients

The SMS.JS script file is displayed below. Save everything between the “—begin sms.js—” and “—end sms.js—” markers to a file named SMS.JS. Edit the NowSMSServerAddress, NowSMSUserName and NowSMSPassword variable settings as appropriate for your installation.  This script is also available at the following link:  http://www.nowsms.com/download/sms.js.txt

—begin sms.js—

/*

This is a command line script for sending an SMS message via NowSMS.

Below, you must substitute in the address of your NowSMS server, plus a valid username and password for

an account defined in the “SMS Users” list of that server.

Note: This script uses the encodeURIComponent method introduced in Internet Explorer 5.5. If you are running

Windows 2000 or an earlier version of Windows, you must have Internet Explorer 5.5 or later installed for this

script to work.

*/

var NowSMSServerAddress = “http://127.0.0.1:8800″;

var NowSMSUserName = “test”;

var NowSMSPassword = “test”;

function HTTPGET(strURL)

{

var strResult;

try

{

// Create the WinHTTPRequest ActiveX Object.

var WinHttpReq = new ActiveXObject(“Msxml2.XMLHTTP” /* or “WinHttp.WinHttpRequest.5″*/);

// Create an HTTP request.

var temp = WinHttpReq.Open(“GET”, strURL, false);

// Send the HTTP request.

WinHttpReq.Send();

// Retrieve the response text.

strResult = WinHttpReq.ResponseText;

}

catch (objError)

{

strResult = objError + “\n”

strResult += “WinHTTP returned error: ” +

(objError.number & 0xFFFF).toString() + “\n\n”;

strResult += objError.description;

}

// Return the response text.

return strResult;

}

var strRequest;

if (WScript.Arguments.Count() < 2) {

WScript.Echo (“Usage: ” + WScript.ScriptName + ” PhoneNumber1[,PhoneNumber2,...] Message Text\r\n”);

WScript.Quit();

}

strRequest = NowSMSServerAddress + “/?PhoneNumber=” + encodeURIComponent(WScript.Arguments(0)) + “&User=” + encodeURIComponent(NowSMSUserName) + “&password=” + encodeURIComponent(NowSMSPassword) + “&text=”;

for (i=1; i<WScript.Arguments.Count(); i++)

{

if (i > 1) {

strRequest += “%20″;

}

strRequest += encodeURIComponent(WScript.Arguments(i));

}

WScript.Echo(HTTPGET(strRequest));

—end sms.js—

For comments and further discussion, please click here to visit the NowSMS Technical Forums (Discussion Board)...