Slack事件订阅事件正在点击我的Firebase端点,然后,Firebase云端功能正在使用dialog_open
端点在Slack中打开一个对话框,其中包含来自Firebase的一些值。
当Firebase Cloud功能点击Slack的dialog_open
端点时,我在控制台日志中出现错误。
我正在获得body: { ok: false, error: 'trigger_expired' }
但是,Firebase中的日志显示往返时间小于500毫秒。但是,我没有看到将从第一个请求登录到trigger_id的触发器ID(请参阅下面的代码)。
4:57:12.443 PM | info | helloWorld | body: { ok: false, error: 'trigger_expired' }
4:57:04.163 PM | outlined_flag | helloWorld | Function execution took 254 ms, finished with status code: 200
4:57:03.910 PM | outlined_flag | helloWorld | Function execution started
当我在新部署后第二次,第三次或第四次触发Slack事件时,我得到body: { ok: false, error: 'invalid_trigger' }
5:31:29.757 PM helloWorld body: { ok: false, error: 'invalid_trigger' }
5:31:28.744 PM helloWorld Function execution took 9 ms, finished with status code: 200
5:31:28.740 PM helloWorld json.trigger_id: "405615464868.7467239747.e706f2732257c541c445ad3938a29fd3"
5:31:28.735 PM helloWorld Function execution started
这也发生得足够快(9ms),但是一个不同的触发错误,这次我确实看到来自Slack Event-Subscription事件的json.trigger_id
。
最后,我尝试了JSON.stringify:
trigger_id: JSON.stringify(json.trigger_id),
现在日志和错误是不同的:
5:33:24.512 PM | info | helloWorld | body: { ok: false, error: 'internal_error' }
5:33:23.565 PM | outlined_flag | helloWorld | Function execution took 13 ms, finished with status code: 200
5:33:23.559 PM | info | helloWorld | json.trigger_id: "406895248231.7467239747.7490e460213b3d65a44eef9f2e30c168"
5:33:23.553 PM | outlined_flag | helloWorld | Function execution started
我一定是在做一些愚蠢的事。任何猜测我的trigger_id
有什么问题?
以下是Firebase云功能:
import * as rp from "request-promise";
import * as functions from "firebase-functions";
export const helloWorld = functions.https.onRequest((request, response) => {
return new Promise((_resolve, _reject) => {
let json = JSON.parse(request.body.payload);
console.log("json.trigger_id:", json.trigger_id);
const options = {
method: "POST",
uri: "https://slack.com/api/dialog.open",
body: {
trigger_id: json.trigger_id,
dialog: {
callback_id: json.callback_id,
title: "Request a Ride",
submit_label: "Request",
elements: [
{ type: "text", label: "Pickup Location", name: "loc_origin" },
{ type: "text", label: "Dropoff Location", name: "loc_destination" }
]
}
},
json: true,
headers: {
"Content-type": "application/json; charset=utf-8",
Authorization:
"Bearer xoxp-secret"
}
};
rp(options)
.then(function(body) {
console.log("body:", body);
})
.catch(function(err) {
console.log("err:", err);
});
return response.status(200).json({ message: "Hello from Firebase" });
}).catch(err => response.status(500).send(err));
});
破碎的承诺:在我的例子中,承诺是错误的。
消息与对话:不需要或不适合此目标的对话。
代码气味:发送消息不需要web.chat.postMessage
。可以使用Firebase Cloud Function的res
将消息发送回Slack。
这是一个改进的例子。
...
exports.eventsSlackBot = functions.https.onRequest((req, res) => {
return new Promise((resolve, reject) => {
// asyc things happening
})
.then(() => {
resolve.status(200).send({
"text": "I am a test message http://slack.com",
"attachments": [{
"text": "And here’s an attachment!"
}]
});
})
.catch(console.error);
});
...