如何使用 Dialogflow fulfillment 中的 Slack 块?

How do I use Slack blocks from Dialogflow fullfillment?

我正在使用对话流编写一个 Slack 机器人。我想使用 Slack blocks 回复履行请求。不幸的是,每当我在回复中包含 blocks 时,我的机器人就会停止工作。

这是我现在拥有的:

return {
    status_code: 200,
    headers: { "Content-Type": "application/json" },
    body: {
        payload: {
            slack: {
                blocks: [{
                  type: "section",
                  text: {
                    type: "mrkdwn",
                    text: "_No logs matched_  :iggy-ghastly:"
                  }
                }]
            }
        }
    }
};

此代码 return 带有 JSON 正文的 HTTP 200。当我使用 text 有效载荷时它工作得很好:

payload: {
    slack: {
        text: "this shows up"
    }
}

Dialogflow 是否支持 Slack 块?网上有这样的例子吗?我怎样才能更好地调试与 Dialogflow 的交互?

Block kit 负载需要用于 Dialogflow 的响应负载的附件。

{
  "slack": {
    "attachments": [
      {
        "blocks":[
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": "_No logs matched_"
            }
          }
        ]
      }
    ]
  }
}
  • 如果您不需要像 Block Kit 这样花哨的 UI 并且您对附件格式的响应没有问题,请查看 slack attachements 可以做什么。
    您可以通过在 dialogflow

  • 中添加响应 custom payload 来测试它
  • 如果您想使用 Block Kit 而不是附件格式。您可以使用空响应来响应履行请求。然后使用 slack API chat.postMessage methods 到 post 一条消息直接从服务器发送到块套件格式的 slack。

const rp = require('request-promise');

let postOptions = {
    uri: 'https://slack.com/api/chat.postMessage',
    method: 'POST',
    headers: {
        'Content-type': 'application/json',
        'Authorization': `Bearer ${YOUR_BOT_TOKEN}`,
    },
    json: {
        //Watch out dialogflow has different fulfillment request body 
        depends on if the user typed or clicked buttons

        "channel": req.OriginalRequest.data.event.channel,
        "blocks": [
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "_No logs matched_"
                }
            }
        ]
    }
}
rp(postOptions, (error, response, body) => {
    if (error) {
        console.log(error)
    }
})

希望以上信息对您有所帮助,回答您的问题。