How to set a custom platform in Dialogflow NodeJS

2019-05-23 17:19发布

问题:

I have created a webhook using dialogflow-fulfillment to correctly return different data depending on the platform, including a custom one I created for another service. I've tested my webhook and know that if I change the originalDetectIntentRequest.source to the platform used in my custom payload it works.

{
    "payload": {
        "jokes-api": {
            "success": true,
            "text": "These are some jokes",
        }
    },
    "outputContexts": []
}

I am able to use dialogflow-nodejs-client-v2's sessions.detectIntent to get a response, but the fullfilment comes back with the platform set as PLATFORM_UNSPECIFIED, and not the custom payloads I want.

[{
    "responseId": "c56c462f-bb3b-434a-b318-3739f58e6f6d",
    "queryResult": {
        "fulfillmentMessages": [{
            "platform": "PLATFORM_UNSPECIFIED",
            "card": {
                "buttons": [],
                "title": "Jokes",
                "subtitle": "These are some jokes",
                "imageUri": ""
            },
            "message": "card"
        }],
        "queryText": "tell me a joke",
        /* ... */
        "intent": {
            "name": "projects/my-jokes/agent/intents/56887375-3047-4993-8e14-53b20dd02697",
            "displayName": "Jokes",
            /* ... */
        },
        "intentDetectionConfidence": 0.8999999761581421,
    }
}]

Looking at the logs for my webhook, I can see that the originalDetectIntentRequest object is present, but source is not set.

{
  "responseId": "c56c462f-bb3b-434a-b318-3739f58e6f6d",
  "queryResult": {
    "queryText": "tell me a joke",
    "speechRecognitionConfidence": 0.9602501,
    /* ... */
    "intent": {
      "name": "projects/my-jokes/agent/intents/56887375-3047-4993-8e14-53b20dd02697",
      "displayName": "Jokes"
    },
    /* ... */
  },
  "originalDetectIntentRequest": {
    "payload": {
    }
  },
  "session": "projects/my-jokes/agent/sessions/12345"
}

How can I set the platform or source in dialogflow-nodejs-client-v2 to get the desired responses?

回答1:

sessions.detectIntent API has queryParams parameter which has a field called payload. That's where the "original payload" goes. The name of the platform goes into the source field of that structure. So your detectIntent call should look something like this:

sessionClient.detectIntent({
    session: sessionClient.sessionPath('<project_id>', '<session_id>'),
    queryInput: {
        text: {
            text: '<query>',
            languageCode: 'en-US'
        }
    },
    queryParams: {
        payload: {
            fields: {
                source: {
                    stringValue: '<platform>',
                    kind: 'stringValue'
                }
            }
        }
    }
})

I was able to simulate Slack integration call by using 'slack' as a platform name. I was also able to return a custom payload for a custom platform name. The payload is returned in queryResult.webhookPayload field.