4

我正在尝试使用hook.io 微服务来制作一个斜杠命令机器人。根据文档,我应该能够立即发送响应,然后再发送单独的 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()
    })
};
}

正如评论中详述的那样,提前调用 res.end() 意味着立即发送响应,但 POST 永远不会发生,而将 res.end() 延迟到 POST 之后意味着发送延迟响应,但它会从 slack 中产生超时错误与此同时。

我是一个javascript新手,所以希望有一个我忽略的简单解决方案。

4

2 回答 2

1

一旦你res.end()在 hook.io 中调用,脚本将立即中止并结束处理。这相当于调用process.exit. 如果你未能结束请求,hook.io 最终会达到它自己的Time-out Limit

hook.io 应该能够在 Slack 要求的三秒内回复 Slack。

这是一个可能会有所帮助的指南:使用 hook.io 制作自定义 Slack 斜线命令

于 2017-10-06T19:38:48.537 回答
0

我知道回答自己的问题的方式不好,但以下内容对我使用webtask 有用,所以我将其包含在此处,以防其他人发现它有用。

var express = require('express');
var Webtask    = require('webtask-tools');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//visiting above url in browser is handled by get
app.get('/', function (req, res) {
    console.log(req.query)
    res.send('OK');
});

//post from slack handled here
app.post('/', function (req, res) {
    var params = req.body;
    console.log(params);
    var data = {
        "response_type": "ephemeral",
        "text": "Immediate Response"
    }
    res.setHeader('Content-Type', 'application/json');
    res.send(data);
    // deliberate delay longer than Slack timeout
    // in order to test delayed response.
    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 supplied by the slack request.
        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) {
        if (error) {
            console.log(error);
        } else {
            console.log("post OK");
        }
    })
};
module.exports = Webtask.fromExpress(app);
于 2017-10-07T06:05:25.693 回答