我正在尝试将 DialogFlow 机器人与环聊聊天(用于 G Suite)集成。我已经在 DialogFlow 上启用了集成,并且基本意图运行良好。
为了使用实现执行后端操作,我创建了一个 firebase 云函数并将其添加为 DialogFlow 实现页面上的 webhook URL。
我编写了云函数代码来识别意图,并为简单的文本响应生成 Webhook 响应格式。这是有效的,我看到 Firestore 数据正在根据意图进行修改。
但是,对于更复杂的意图,我希望使用 Chat 提供的更多基于动态卡片的响应。为了实现这一点,我查看了对话框流卡响应的文档。
我在https://cloud.google.com/dialogflow/docs/integrations/hangouts看到了以下代码。当我将其粘贴到环聊自定义有效负载下的对话流意图编辑器 UI 中时(禁用 webhook 集成后),它可以工作
{
"hangouts": {
"header": {
"title": "Pizza Bot Customer Support",
"subtitle": "pizzabot@example.com",
"imageUrl": "..."
},
"sections": [{
"widgets": [{
"keyValue": {
"icon": "TRAIN",
"topLabel": "Order No.",
"content": "12345"
}
},
{
"keyValue": {
"topLabel": "Status",
"content": "In Delivery"
}
}]
},
{
"header": "Location",
"widgets": [{
"image": {
"imageUrl": "https://dummyimage.com/600x400/000/fff"
}
}]
},
{
"header": "Buttons - i could leave the header out",
"widgets": [{
"buttons": [{
"textButton": {
"text": "OPEN ORDER",
"onClick": {
"openLink": {
"url": "https://example.com/orders/..."
}
}
}
}]
}]
}]
}
}
这正是我所需要的,但我需要来自 webhook 的响应。我没有得到正确的响应格式来映射两者。
当我尝试将相同的代码与 webhook 集成时,我在环聊聊天中没有得到任何回复。当我检查对话流 UI 的历史记录部分时,这里是原始交互日志中提到的响应结构
{
"queryText": "<redacted>",
"parameters": {},
"intent": {
"id": "<redacted>",
"displayName": "<redacted>",
"priority": 500000,
"webhookState": "WEBHOOK_STATE_ENABLED"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 284
},
"languageCode": "en",
"slotfillingMetadata": {
"allRequiredParamsPresent": true
},
"id": "<redacted>",
"sessionId": "<redacted>",
"timestamp": "2020-07-30T12:05:29.094Z",
"source": "agent",
"webhookStatus": {
"webhookUsed": true,
"webhookPayload": {
"hangouts": {
"header": {
"subtitle": "pizzabot@example.com",
"title": "Pizza Bot Customer Support",
"imageUrl": "..."
},
"sections": [
{
"widgets": [
{
"keyValue": {
"content": "12345",
"topLabel": "Order No.",
"icon": "TRAIN"
}
},
{
"keyValue": {
"topLabel": "Status",
"content": "In Delivery"
}
}
]
},
{
"widgets": [
{
"image": {
"imageUrl": "https://dummyimage.com/600x400/000/fff"
}
}
],
"header": "Location"
},
{
"widgets": [
{
"buttons": [
{
"textButton": {
"text": "OPEN ORDER",
"onClick": {
"openLink": {
"url": "https://example.com/orders/..."
}
}
}
}
]
}
],
"header": "Buttons - i could leave the header out"
}
]
}
},
"webhookStatus": {
"message": "Webhook execution successful"
}
},
"agentEnvironmentId": {
"agentId": "<redacted>",
"cloudProjectId": "<redacted>"
}
}
我还在聊天文档上找到了这个链接,它解释了如何显示基于交互式卡片的 UI https://developers.google.com/hangouts/chat/how-tos/cards-onclick。但是我无法理解如何将其与 webhook 集成。
更新 我遵循了https://www.leeboonstra.com/Bots/custom-payloads-rich-cards-dialogflow/上的教程,并且能够使用他们提到的示例代码显示卡响应。它正在使用这个已弃用的库(https://github.com/dialogflow/dialogflow-fulfillment-nodejs)。这是使它工作的代码,
let payload = new Payload("hangouts", json, {
rawPayload: true,
sendAsMessage: true,
});
agent.add(payload);
这里的json变量应该是我之前提到的JSON结构。所以现在,我可以使用已弃用的 API 映射到正确的响应格式。但是,我无法获得将正确响应发送到后端的按钮。这是我从之前的 json 修改的按钮字段,
"buttons": [
{
"textButton": {
"text": "Click Me",
"onClick": {
"action": {
"actionMethodName": "snooze",
"parameters": [
{
"key": "time",
"value": "1 day"
},
{
"key": "id",
"value": "123456"
}
]
}
}
}
}
]