我正在尝试使用 hook.io microservice制作一个松弛的斜线命令机器人.根据 docs我应该能够发送立即响应然后单独的POST.但我无法得到立即响应,后来的POST也都工作了. 这是我的测试代码. modu
这是我的测试代码.
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() }) }; }
在注释中详细说明res.end()早期意味着立即响应被发送但POST永远不会发生,而延迟res.end()直到POST后意味着延迟响应被发送但它从松弛产生超时错误与此同时.
我是一个javascript新手,所以希望有一个我忽略的简单解决方案.
一旦你在hook.io中调用res.end(),脚本将立即中止并结束处理.它相当于调用process.exit.如果你未能结束请求,hook.io最终会点击它自己的 Time-out Limit.hook.io应该能够在Slack要求的三秒内响应Slack.
这是一个可能有用的指南:Making a Custom Slack Slash Command with hook.io