首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在松弛通道中未打开使用web-hook的松弛模态消息

在松弛通道中未打开使用web-hook的松弛模态消息
EN

Stack Overflow用户
提问于 2020-09-17 08:25:08
回答 1查看 466关注 0票数 0

我使用web-hook在Slack频道中发布了一条模式交互消息,但该消息没有显示在Slack频道中。

Slack文档中有一些关于触发器id的信息,但这些信息非常混乱。

下面是我的JSON有效负载,我正在使用来自Slack的预构建模板,并使用web-hook将消息发送到Slack通道。

代码语言:javascript
复制
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));

EN

回答 1

Stack Overflow用户

发布于 2020-09-17 09:51:09

你需要一个trigger_id来打开一个模式。基本上,只有当用户说“显示模式!”时,Slack才会让你显示模式!这可以防止开发人员向毫无戒心的用户发送恼人的、不想要的模态。您可以在用户与其中一个应用程序“entry points”交互时获取trigger_id:

  1. 快捷方式
  2. 斜杠命令
  3. 应用程序UI中的交互式元素(如按钮)

当用户与其中一个入口点进行交互时,他们告诉您他们想要查看该模式,因此Slack将向您发送一个带有trigger_id的interaction payload。对于全局快捷方式,该有效负载可能类似于this

代码语言:javascript
复制
{
  "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秒内过期。

代码语言:javascript
复制
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
        }
      }
    ]
  }
};

下面是描述谁做什么以及按什么顺序做的整个流程:

代码语言:javascript
复制
┌────────────────────────────────────────────────────────────────────────────────────────┐
│◜◜◜◜◜◜◜◜◜◜◜◜◜◜┌─────────────┐◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜┌─────────────┐◜│
│◜┌─────────┐◜◜│  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  │◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
│◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜└─────────────┘◜◜◜└─────────────┘◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜◜│
└────────────────────────────────────────────────────────────────────────────────────────┘
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63929599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档