如何将消息推送到Dialogflow会话?

时间:2018-05-02 14:36:47

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

我正在使用Dialogflow API V2beta1来构建Messenger + Google Assistant bot。

根据他们的文档,我可以派遣一个事件来触发意图。我能够成功触发事件,并且(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"
    }
}]

1 个答案:

答案 0 :(得分:0)

在评论中向Nick Felker表示感谢。

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