【发布时间】:2017-10-05 07:30:21
【问题描述】:
我正在尝试使用hook.io microservice 来制作一个斜杠命令机器人。根据docs,我应该能够立即发送回复,然后再发送单独的 POST。但我无法立即得到响应,而后来的 POST 都可以正常工作。
这是我的测试代码。
module['exports'] = function testbot(hook) {
var request = require('request');
// The parameters passed in via the slash command POST request.
var params = hook.params;
data = {
"response_type": "ephemeral",
"text": "Immediate Response"
}
hook.res.setHeader('Content-Type', 'application/json');
console.log("returning immediate response")
hook.res.write(JSON.stringify(data), 'utf8', delay(params));
//calling end() here sends the immediate response but the POST never happens.
// but if end() is called below instead slack gives a timeout error but the POST succeeds.
//hook.res.end()
//test with 3.5 second delay
function delay(params) {
setTimeout(function () {post_response(params)},3500);
}
function post_response(params) {
console.log("posting delayed response")
// Set up the options for the HTTP request.
var options = {
// Use the Webhook URL from the Slack Incoming Webhooks integration.
uri: params.response_url,
method: 'POST',
// Slack expects a JSON payload with a "text" property.
json: {"response_type":"in_channel", "text":"Delayed response","parse":"full"}
};
// Make the POST request to the Slack incoming webhook.
request(options, function (error, response, body) {
// Pass error back to client if request endpoint can't be reached.
if (error) {
console.log(error);
hook.res.end(error.message);
} else {
console.log("post OK");
}
// calling end() here sends the POST but the immediate response is lost to a slack timeout error.
hook.res.end()
})
};
}
正如 cmets 中详述的那样,提前调用 res.end() 意味着立即发送响应,但 POST 永远不会发生,而将 res.end() 延迟到 POST 之后意味着发送延迟响应,但它会从期间松懈。
我是 javascript 新手,所以希望有一个我忽略的简单解决方案。
【问题讨论】:
-
为什么要延迟 3.5 秒开始?根据 slack 文档,如果您等待超过 3 秒的响应时间,slack 会抛出超时错误。
-
您似乎将
delay函数作为参数传递给hook.res.write。这是不对的,因为hook.res.write不接受回调参数。 -
@Marak- 3.5 秒故意超过了松弛超时。我的目标是找到一种立即响应的方法,然后继续在后台处理数据并将结果作为单独的事务发布回来。上面显示的代码按描述工作(post_response 在 3.5 秒后成功完成)所以我对你声称 hook.res.write() 不接受回调有点困惑。
-
hook.res.write不接受回调,由于 JavaScript 的工作方式,delay方法将立即执行。无法在后台处理结果或进行单独的交易。您必须在 3 秒内回复 Slack。如果您必须使用延迟响应 API(您不应该),您可能必须将响应 URL 存储在数据存储中以供以后使用。最好尝试立即回复。