3

I'm using Dialogflow API V2beta1 to build a Messenger + Google Assistant bot.

According to their docs, I can dispatch an event to trigger an intent. I'm able to successfully trigger the event, and the (200) response is logged in Firebase Functions, but the message is never pushed into the chat itself.

How can I ensure the response is pushed into to the current session?

// 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);

And here's my response, with some params obfuscated:

[{
    "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"
    }
}]
Nick Felker
  • 11,536
  • 1
  • 21
  • 35
Jefftopia
  • 2,105
  • 1
  • 26
  • 45
  • 1
    With regards to the Google Assistant, you cannot push messages to the conversation. They only show up synchronously after the user sends a query. – Nick Felker May 02 '18 at 17:25
  • Ouch. I have a case where I send my users a link, scrape non-PII data, and pop them back into the chat. My intention is to push a "confirmation" message into the chat post-scrape. That's impossible on the assistant? Would love to hear about a good alternative. – Jefftopia May 02 '18 at 20:36
  • 1
    You can send a notification, or cache the response and allow them to query your action again when the data is retrieved. – Nick Felker May 02 '18 at 22:34
  • 2
    And notifications only work on mobile devices - not on all Assistant platforms. – Prisoner May 03 '18 at 09:46
  • @NickFelker Thanks, makes sense. I'll add a suggestion "Hit me up once you've opened the link" or something to that effect. – Jefftopia May 03 '18 at 13:23
  • @NickFelker what if its slack? And chatbot is made using dialogflow-fulfillment nodejs client library? How can we push a message? – Muhammad Naufil Aug 28 '20 at 20:34
  • That's an entirely different question that relates very specifically to Slack's platform. – Nick Felker Aug 31 '20 at 16:57

1 Answers1

1

Credit to Nick Felker's in the comments.

Answer: You can't push messages into Assistant conversations. Instead, lead the user back into the conversation using push notifications and "I'm back"-like responses.

Jefftopia
  • 2,105
  • 1
  • 26
  • 45