【问题标题】:How can I push a message to a Dialogflow session?如何将消息推送到 Dialogflow 会话?
【发布时间】:2018-10-12 17:16:32
【问题描述】:

我正在使用 Dialogflow API V2beta1 构建一个 Messenger + Google Assistant 机器人。

根据他们的文档,我可以调度一个事件来触发一个意图。我能够成功触发事件,并且 (200) 响应记录在 Firebase 函数中,但消息从未推送到聊天本身。

如何确保将响应推送到当前会话?

// requries...
const app = express();
const sessionClient = new dialogflow.SessionsClient();

app.post('/detect', (req, res) => {
    console.log('handler');

    res.header('Content-Type', 'application/json');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Origin', 'my-domain');

    const bdy = JSON.parse(req.body);

    const sessionPath = sessionClient.sessionPath(projectId, bdy.session);

    const request = {
        session: sessionPath,
        queryParams: {
            session: sessionPath,
            contexts: [{
                name: `${sessionPath}/contexts/mycontext`,
                lifespan: 5,
                parameters: structjson.jsonToStructProto({ 'Model': bdy.model })
            }],
        },
        queryInput: {
            event: {
                name: 'my_event',
                parameters: structjson.jsonToStructProto(
                {
                    Model: bdy.model,
                }),
                languageCode: languageCode,
            },
        }
    };

    // Send request and log result
    console.log('md request: ', request);

    // here is where the intent is detected, and my handler for that successfully invoked
    sessionClient
        .detectIntent(request)
        .then(responses => {
            console.log('event response ', JSON.stringify(responses));
            const result = responses[0].queryResult;

            if (result.intent) {
                res.send(responses);
            } else {
                console.log(`  No intent matched.`);
                res.send({ text: 'not match' })
            }
        })
        .catch(err => {
            console.error('ERROR:', err);
            res.send(err);
        });

});

exports.myHandler = functions.https.onRequest(app);

这是我的回复,其中一些参数被混淆了:

[{
    "responseId": "c87b3c74-5c74-478b-8a5a-a0b90f059787",
    "queryResult": {
        "fulfillmentMessages": [{
            "platform": "PLATFORM_UNSPECIFIED",
            "text": {
                "text": ["You you have a BMW X1.\n        Next, I just need to know what computer you have"]
            },
            "message": "text"
        }],
        "outputContexts": [{
            "name": "projects/<my-project>/agent/sessions/<my-session>/contexts/<my-context>",
            "lifespanCount": 5,
            "parameters": {
                "fields": {
                    "Model.original": {
                        "stringValue": "",
                        "kind": "stringValue"
                    },
                    "Model": {
                        "stringValue": "BMW X1",
                        "kind": "stringValue"
                    }
                }
            }
        }],
        "queryText": "my_event",
        "speechRecognitionConfidence": 0,
        "action": "",
        "parameters": {
            "fields": {
                "Model": {
                    "stringValue": "",
                    "kind": "stringValue"
                }
            }
        },
        "allRequiredParamsPresent": true,
        "fulfillmentText": "You you have a BMW X1.\n        Next, I just need to know what computer you have",
        "webhookSource": "",
        "webhookPayload": null,
        "intent": {
            "inputContextNames": [],
            "events": [],
            "trainingPhrases": [],
            "outputContexts": [],
            "parameters": [],
            "messages": [],
            "defaultResponsePlatforms": [],
            "followupIntentInfo": [],
            "name": "projects/<my-project>/agent/intents/<intent-id>",
            "displayName": "my_event",
            "priority": 0,
            "isFallback": false,
            "webhookState": "WEBHOOK_STATE_UNSPECIFIED",
            "action": "",
            "resetContexts": false,
            "rootFollowupIntentName": "",
            "parentFollowupIntentName": "",
            "mlDisabled": false
        },
        "intentDetectionConfidence": 1,
        "diagnosticInfo": {
            "fields": {
                "webhook_latency_ms": {
                    "numberValue": 6065,
                    "kind": "numberValue"
                }
            }
        },
        "languageCode": "en-us"
    },
    "webhookStatus": {
        "details": [],
        "code": 0,
        "message": "Webhook execution successful"
    }
}]

【问题讨论】:

  • 关于 Google 助理,您无法将消息推送到对话中。它们仅在用户发送查询后同步显示。
  • 哎哟。我有一个案例,我向我的用户发送一个链接,抓取非 PII 数据,然后将他们弹回聊天中。我的意图是将“确认”消息推送到聊天后的聊天记录中。助理不可能吗?很想听听一个好的选择。
  • 您可以发送通知,或缓存响应,并允许他们在检索到数据时再次查询您的操作。
  • 通知仅适用于移动设备,并非适用于所有 Google 助理平台。
  • @NickFelker 谢谢,有道理。我会添加一个建议“一旦你打开链接就打我”或类似的内容。

标签: actions-on-google dialogflow-es google-assist-api


【解决方案1】:

感谢 Nick Felker 在 cmets 中的表现。

回答:您无法将消息推送到 Google 助理对话中。相反,请使用推送通知和类似“我回来了”的响应将用户引导回对话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-17
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多