如何返回slack slash命令的立即响应和延迟响应?

时间:2017-10-05 07:30:21

标签: node.js slack hook.io

我正在尝试使用hook.io microservice制作一个松弛的斜杠命令机器人。根据{{​​3}}我应该能够发送立即响应然后单独的POST。但是我无法立即得到响应,后来的POST也都工作了。

这是我的测试代码。

2017-07-14T12:02:23.000+0000

详见调用res.end()的注释早期意味着立即响应被发送但POST永远不会发生,而延迟res.end()直到POST后意味着发送了延迟响应但是它产生了超时错误在此期间松懈。

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

2 个答案:

答案 0 :(得分:1)

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

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

以下是可能有用的指南:Making a Custom Slack Slash Command with hook.io

答案 1 :(得分: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);
相关问题