使用谷歌应用脚​​本的第三方短信

时间:2013-03-27 17:01:20

标签: google-apps-script sms-gateway

我想使用邮件列表通过第三方提供商发送短信。以下是他们提供的代码示例:

<%
' 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)
%>

我不确定如何在GAS中做到这一点(我真的是业余程序员......)。从谷歌搜索似乎我需要使用“UrlFetchApp.fetch”之类的东西。任何帮助或相关链接将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

以下功能可创建格式正确的POST。如果没有有效的凭据,我可以确认它获得了200 OK的HTTP响应,并且服务器报告23|invalid credentials (username was: your_username)|。所以它看起来应该有效,并填写了正确的细节。

我为contentType添加了application/x-www-form-urlencoded,虽然这不是必需的,因为它是默认值。

如果您使用一组测试值,那么下一步就是将其更改为接受并使用参数 - 我会留给您。

/*
 * 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});
}

参考