使用网络挂钩的松弛模态消息未在松弛通道中打开

时间:2020-09-17 00:25:08

标签: slack slack-api

我正在使用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",
        "emoji": true
    },
    "title": {
        "type": "plain_text",
        "text": "Your accommodation",
        "emoji": true
    },
    "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": "|"
                },
                {
                    "type": "mrkdwn",
                    "text": "$220 / night"
                }
            ]
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Choose",
                        "emoji": true
                    },
                    "value": "click_me_123"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "View Details",
                        "emoji": true
                    },
                    "value": "click_me_123"
                }
            ]
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "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."
            },
            "accessory": {
                "type": "image",
                "image_url": "https://api.slack.com/img/blocks/bkb_template_images/redwoodcabin.png",
                "alt_text": "Redwood Suite"
            }
        },
        {
            "type": "context",
            "elements": [
                {
                    "type": "mrkdwn",
                    "text": "1x King Bed"
                },
                {
                    "type": "mrkdwn",
                    "text": "|"
                },
                {
                    "type": "mrkdwn",
                    "text": "$350 / night"
                }
            ]
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "✓ Your Choice",
                        "emoji": true
                    },
                    "style": "primary",
                    "value": "click_me_123"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "View Details",
                        "emoji": true
                    },
                    "value": "click_me_123"
                }
            ]
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*Luxury Tent*\n*One person only*. Shared modern bathrooms and showers in lounge building. Temperature control with heated blankets. Lights and electrical outlets."
            },
            "accessory": {
                "type": "image",
                "image_url": "https://api.slack.com/img/blocks/bkb_template_images/tent.png",
                "alt_text": "Redwood Suite"
            }
        },
        {
            "type": "context",
            "elements": [
                {
                    "type": "mrkdwn",
                    "text": "1x Queen Bed"
                },
                {
                    "type": "mrkdwn",
                    "text": "|"
                },
                {
                    "type": "mrkdwn",
                    "text": "$260 / night"
                }
            ]
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Choose",
                        "emoji": true
                    },
                    "value": "click_me_123"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "View Details",
                        "emoji": true
                    },
                    "value": "click_me_123"
                }
            ]
        },
        {
            "type": "divider"
        }
    ]
};
attachments.push(attachment);
message.attachments = attachments;
print(JSON.stringify(message));

1 个答案:

答案 0 :(得分:1)

您需要一个trigger_id才能打开模式。基本上,只有当用户说“向我显示模态!”时,Slack才允许您显示模态!这样可以防止开发人员将烦人的有害模式发送给毫无戒心的用户。当用户与应用“ entry points”之一互动时,您可以获取trigger_id:

  1. 捷径
  2. 斜杠命令
  3. 应用程序用户界面中的交互式元素(如按钮)

当用户与这些入口点之一进行交互时,他们告诉您他们想要查看模式,因此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",
      "emoji": true
    },
    "title": {
      "type": "plain_text",
      "text": "Your accommodation",
      "emoji": true
    },
    "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  │◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜└─────────────┘◜◜◜└─────────────┘◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
└────────────────────────────────────────────────────────────────────────────────────────┘