问题描述
我正在使用Web挂钩在Slack频道中发布模态交互式消息,但该消息未显示在Slack频道中。
Slack文档中有一些有关触发器ID的信息,但这非常令人困惑。
下面是我的JSON有效负载,我在这里使用Slack中的prebuild模板,并使用网络挂钩将消息发送到Slack频道。
var message={};
var attachments = [];
var attachment = {
"type": "modal","submit": {
"type": "plain_text","text": "Submit","emoji": true
},"close": {
"type": "plain_text","text": "Cancel","title": {
"type": "plain_text","text": "Your accommodation","blocks": [
{
"type": "section","text": {
"type": "plain_text","text": "Please choose an option where you'd like to stay from Oct 21 - Oct 23 (2 nights).","emoji": true
}
},{
"type": "divider"
},{
"type": "section","text": {
"type": "mrkdwn","text": "*Airstream Suite*\n*Share with another person*. Private walk-in bathroom. TV. Heating. Kitchen with microwave,basic cooking utensils,wine glasses and silverware."
},"accessory": {
"type": "image","image_url": "https://api.slack.com/img/blocks/bkb_template_images/Streamline-Beach.png","alt_text": "Airstream Suite"
}
},{
"type": "context","elements": [
{
"type": "mrkdwn","text": "1x Queen bed"
},{
"type": "mrkdwn","text": "|"
},"text": "$220 / night"
}
]
},{
"type": "actions","elements": [
{
"type": "button","text": {
"type": "plain_text","text": "Choose","emoji": true
},"value": "click_me_123"
},{
"type": "button","text": "View Details","value": "click_me_123"
}
]
},"text": "*Redwood Suite*\n*Share with 2 other person*. Studio home. Modern bathroom. TV. Heating. Full kitchen. Patio with lounge chairs and campfire style fire pit and grill."
},"image_url": "https://api.slack.com/img/blocks/bkb_template_images/redwoodcabin.png","alt_text": "Redwood Suite"
}
},"text": "1x King bed"
},"text": "$350 / night"
}
]
},"text": "✓ Your Choice","style": "primary","text": "*Luxury Tent*\n*One person only*. Shared modern bathrooms and showers in lounge building. Temperature control with heated blankets. Lights and electrical outlets."
},"image_url": "https://api.slack.com/img/blocks/bkb_template_images/tent.png","text": "$260 / night"
}
]
},{
"type": "divider"
}
]
};
attachments.push(attachment);
message.attachments = attachments;
print(JSON.stringify(message));
解决方法
您需要一个trigger_id才能打开模式。基本上,只有当用户说“向我显示模态!”时,Slack才允许您显示模态!这样可以防止开发人员将烦人的有害模式发送给毫无戒心的用户。当用户与应用“ entry points”之一互动时,您可以获取trigger_id:
- 捷径
- 斜杠命令
- 应用程序用户界面中的交互式元素(如按钮)
当用户与这些入口点之一进行交互时,他们告诉您他们想要查看模式,因此Slack将向您发送带有trigger_id的interaction payload。对于全局快捷方式,该有效负载可能类似于this:
{
"type": "shortcut","token": "XXXXXXXXXXXXX","action_ts": "1581106241.371594","team": {
"id": "TXXXXXXXX","domain": "shortcuts-test"
},"user": {
"id": "UXXXXXXXXX","username": "aman","team_id": "TXXXXXXXX"
},"callback_id": "shortcut_create_event","trigger_id": "944799105734.773906753841.38b5894552bdd4a780554ee59d1f3638"
}
因此,基本上,您需要先创建其中之一,然后设置您的应用以接收来自Slack的POST请求。
一旦您知道用户想要出现模态,就可以告诉Slack打开模态。创建一个view payload(代码中的附件)并包括您收到的trigger_id。将此有效负载发送到views.open端点。请注意,您不像在代码中一样发送消息有效负载,请记住,trigger_id在3秒内到期。
var payload = {
trigger_id: "944799105734.773906753841.38b5894552bdd4a780554ee59d1f3638",view: {
"type": "modal","submit": {
"type": "plain_text","text": "Submit","emoji": true
},"close": {
"type": "plain_text","text": "Cancel","title": {
"type": "plain_text","text": "Your accommodation","blocks": [
{
"type": "section","text": {
"type": "plain_text","text": "Please choose an option where you'd like to stay from Oct 21 - Oct 23 (2 nights).","emoji": true
}
}
]
}
};
以下是描述谁以什么顺序执行的整个流程:
┌────────────────────────────────────────────────────────────────────────────────────────┐
│◜◜◜◜◜◜◜◜◜◜◜◜◜◜┌─────────────┐◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜┌─────────────┐◜│
│◜┌─────────┐◜◜│ Selects a │◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│ Sees the │◜│
│◜│ USER │◜◜│ shortcut │─┐◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜┌─▶│ modal │◜│
│◜└─────────┘◜◜│ │◜│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│◜◜│ │◜│
│◜◜◜◜◜◜◜◜◜◜◜◜◜◜└─────────────┘◜│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│◜◜└─────────────┘◜│
├──────────────────────────────▼──────────────────────────────────────┴──────────────────┤
│ ┌─────────────┐ ┌─────────────┐ │
│ ┌──────────┐ │ Generates │ │ Opens the │ │
│ │ SLACK │ │ interaction │ │ modal │ │
│ └──────────┘ │ payload │ │ │ │
│ └─────────────┘ └─────────────┘ │
├──────────────────────────────┬──────────────────────────────────────▲──────────────────┤
│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│◜◜◜┌─────────────┐◜◜◜┌─────────────┐◜◜│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
│◜┌──────────┐◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│◜◜◜│ Extracts │◜◜◜│ Sends modal │◜◜│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
│◜│ YOUR APP │◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜└──▶│ trigger_id │──▶│ data with │──┘◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
│◜└──────────┘◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│ │◜◜◜│ trigger_id │◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜└─────────────┘◜◜◜└─────────────┘◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
└────────────────────────────────────────────────────────────────────────────────────────┘