【问题标题】:Unable to enable Slack's "Enable Events" through Google Scripts无法通过 Google 脚本启用 Slack 的“启用事件”
【发布时间】:2020-09-19 07:14:22
【问题描述】:

所以我正在尝试创建一个 SlackBot,它可以跟踪对它发布的消息的反应。不过,我也在尝试从 Google 表格中读取数据,因此机器人的代码将位于连接到表单的 Google 脚本中。

我显然需要启用事件才能看到反应,第一步是接收和响应 Slack 发送的 POST 请求:

Our Request:
POST
"body": { 
     "type": "url_verification",
     "token": "---",
     "challenge": "---"
}

我必须做出如下回应:

HTTP 200 OK, 
Content-type: text/plain, 
"the challenge code"

当我收到 POST 请求时,它会覆盖 Google 表格的 1,1 方格,这不是很好,但更令人担忧的是,我的 doPost 函数没有运行。

这是我从 Slack 得到的反馈:

Your Response:
"code": 200
"error": "challenge_failed"
"body": {
 <!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">The script completed but did not return anything.</div></body></html> 
}

现在,这是我的 Google 脚本中的所有代码。

function doPost(request){

  var postJSON = request.postData.getDataAsString();

  var sheet = SpreadsheetApp.getActiveSheet();
  
  // doesn't happen
  sheet.getRange(5, 3).setValue("YEAH");
  // when I send a req using CURL, I get the message
  // "The script completed but did not return anything"
  return postJSON;
}

它已经部署为一个网络应用程序,而且我每次更改代码时都会更新它,所以这不是问题。

任何建议将不胜感激,谢谢!

【问题讨论】:

    标签: javascript post google-apps-script slack-api


    【解决方案1】:

    我认为在这种情况下,需要返回challenge的值。那么下面的修改呢?

    发件人:

    var postJSON = request.postData.getDataAsString();
    

    收件人:

    var postJSON = JSON.parse(request.postData.getDataAsString());
    

    另外,请进行如下修改。

    发件人:

    return postJSON;
    

    收件人:

    return ContentService.createTextOutput(postJSON.challenge);
    

    注意:

    • 在此修改中,假设您当前脚本中的postJSON{"type": "url_verification", "token": "---", "challenge": "---"}。请注意这一点。
    • 此外,它假设 Web 应用程序部署为 Execute the app as: MeWho has access to the app: Anyone, even anonymous
    • Web Apps脚本修改后,请重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 2012-05-04
      相关资源
      最近更新 更多