如何 return 立即和延迟响应 slack slash 命令?

How to return both immediate and delayed response to slack slash command?

我正在尝试使用 hook.io microservice to make a slack slash command bot. According to the 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()
    })
};
}

如评论中所述,尽早调用 res.end() 意味着会立即发送响应,但 POST 永远不会发生,而将 res.end() 延迟到 [=24= 之后] 表示已发送延迟响应,但同时会从 slack 生成超时错误。

我是 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

我知道回答自己问题的错误形式,但以下使用 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);