0

I would like to use a mail list to send SMS through a third party provider. Here is the code samples that they provide:

<%
' This simple ASP Classic code sample is provided as a starting point. Please extend your
' actual production code to properly check the response for any error statuses that may be
' returned (as documented for the send_sms API call).

username = "your_username"
password = "your_password"
recipient = "44123123123"
message = "This is a test SMS from ASP"
postBody = "username=" & Server.URLEncode(username) & "&password=" &     Server.URLEncode(password) & "&msisdn=" & recipient & "&message=" & Server.URLEncode(message)

set httpRequest = CreateObject("MSXML2.ServerXMLHTTP")
httpRequest.open "POST", "http://bulksms.2way.co.za/eapi/submission/send_sms/2/2.0", false
httpRequest.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
httpRequest.send postBody
Response.Write (httpRequest.responseText)
%>

I am not sure how to do this in GAS (I am an amateur programmer really...). From googling it seems that I will need to use something like "UrlFetchApp.fetch". Any help or relevant links would be appreciated. Thanks in advance.

user2091307
  • 107
  • 1
  • 5

1 Answers1

0

The function below creates a properly formatted POST. Without valid credentials, I can confirm that it gets a 200 OK HTTP response, and the server reports 23|invalid credentials (username was: your_username)|. So it looks like it should work, with the right details filled in.

I've included application/x-www-form-urlencoded for the contentType, although this is not needed because it's the default.

If you get this working with a set of test values, then the next step would be to change it to accept and use parameters - I'll leave that to you.

/*
 * Sends an HTTP POST to provider, to send a SMS.
 *
 * @param {tbd} paramName To be determined.
 *
 * @return {object} Results of POST, as an object. Result.rc is the
 *                  HTTP result, an integer, and Result.serverResponse
 *                  is the SMS Server response, a string.
 */
function sendSMS() {
  var url = "http://bulksms.2way.co.za/eapi/submission/send_sms/2/2.0";
  var username = "your_username";
  var password = "your_password";
  var recipient = "44123123123";
  var message = "This is a test SMS from ASP";
  var postBody = {
    "username" : encodeURIComponent(username),
    "password" : encodeURIComponent(password),
    "msisdn" : encodeURIComponent(recipient),
    "message" : encodeURIComponent(message)
  };

  var options =
  {
    "method" : "post",
    "contentType" : "application/x-www-form-urlencoded", 
    "payload" : postBody
  };

  // Fetch the data and collect results.
  var result = UrlFetchApp.fetch(url,options);
  var rc = result.getResponseCode();     // HTTP Response code, e.g. 200 (Ok)
  var serverResponse = result.getContentText(); // SMS Server response, e.g. Invalid Credentials
  debugger;  // Pause if running in debugger
  return({"rc" : rc, "serverResponse" : serverResponse});
}

References

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275