hook.io microserviceを使用して、スラッシュスラッシュコマンドボットを作成しようとしています。 docsによると、すぐに応答を送信し、後で別のPOSTを送信できるはずです。しかし、私は即座の応答と後のPOSTを両方とも動作させることはできません。slackコマンドに即時応答と遅れ応答の両方を返すにはどうすればいいですか?
ここは私のテストコードです。
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()
})
};
}
POSTを意味した後、遅延応答が送信されますが、それが生成されるまでres.end()初期の即時応答が送信されることが、POSTは(res.endを遅らせる一方で起こることはありません)を呼び出し、コメントのように詳細なその間のスラックからのタイムアウトエラー。
私はjavascriptの初心者ですので、うまくいけば、私が見落とした簡単な解決策があります。
なぜ3.5秒の遅延で開始するのですか?スラックのドキュメントによれば、応答時間が3秒以上になると、スラックはタイムアウトエラーをスローします。 – Marak
'hook.res.write'の引数として' delay'関数を渡しているようです。 'hook.res.write'はコールバックパラメータを受け付けないので正しくありません。 – Marak
@ Marak- 3.5秒が意図的にスラックタイムアウトを超えています。私の目的は、即座に対応する方法を見つけることですが、バックグラウンドでデータを処理し続け、その結果を別のトランザクションとして返信します。上記のコードは、説明したように動作します(post_responseは3.5秒後に正常に完了します)。私はhook.res.write()がコールバックを受け付けないというあなたの主張によってちょっと混乱します。 – rw950431