2017-10-05 18 views
3

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の初心者ですので、うまくいけば、私が見落とした簡単な解決策があります。

+0

なぜ3.5秒の遅延で開始するのですか?スラックのドキュメントによれば、応答時間が3秒以上になると、スラックはタイムアウトエラーをスローします。 – Marak

+0

'hook.res.write'の引数として' delay'関数を渡しているようです。 'hook.res.write'はコールバックパラメータを受け付けないので正しくありません。 – Marak

+0

@ Marak- 3.5秒が意図的にスラックタイムアウトを超えています。私の目的は、即座に対応する方法を見つけることですが、バックグラウンドでデータを処理し続け、その結果を別のトランザクションとして返信します。上記のコードは、説明したように動作します(post_responseは3.5秒後に正常に完了します)。私はhook.res.write()がコールバックを受け付けないというあなたの主張によってちょっと混乱します。 – rw950431

答えて

0

res.end()をhook.ioの中に呼び出すと、スクリプトは直ちに中断して処理を終了します。それはprocess.exitを呼び出すのと同じです。リクエストを終了できない場合、hook.ioは最終的にそれ自身のTime-out Limitにヒットします。

hook.ioは3秒以内にスラックに応答する必要があります。スラックが必要です。ここで

を助けるかもしれないガイドです:Making a Custom Slack Slash Command with hook.io

+0

ありがとう@マラク - 私が疑ったことはthats。 hook.ioはすばらしい製品ですが、このアプリケーションには適していません。なぜなら、データベースルックアップに時間がかかることがあるためです。この場合、https://webtask.io/docs/model「カスタムプログラミングモデル」が適しているという初期の兆候があります。 – rw950431

+0

Webtask APIはまったく同じように動作します。あなたがただちに応答してslack URLをhook.ioデータストアまたはWebtaskストレージに保存しない限り、解決策を見つけることはできません。 – Marak

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); 
+0

ありがとうございます。ドキュメントに基づいて、私は今30秒まで応答が終了した後もWebtaskがスクリプトを実行し続けることがわかります。私は調査するつもりですが、最初の応答後に後処理を可能にするためにhook.ioで同じことを実行できるはずです。 – Marak

関連する問題