0

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

<%
' 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”之类的东西。任何帮助或相关链接将不胜感激。提前致谢。

4

1 回答 1

0

下面的函数创建一个格式正确的POST. 如果没有有效的凭据,我可以确认它会收到 200 OK HTTP 响应,并且服务器会报告23|invalid credentials (username was: your_username)|. 所以看起来它应该可以工作,并填写了正确的细节。

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

如果您使用一组测试值进行此操作,那么下一步就是将其更改为接受和使用参数 - 我将把它留给您。

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

参考

于 2013-03-28T02:42:33.267 回答