How to parse fulfillment of Dialogflow V1 - dialogflow-es

I am building an Android Application where i am passing the request from the app to Dialogflow. Dialog flow then passes it to a Node.js script and query a database for result and finally send the response to the Android device. Now the query results are stored inside "payload" of "messages". However I am unable o access the payload part. I mean I can access "messages" with getMessages but I cannot probe further into messages. The response from Dialogflow is as below and i want to access the elements tag in the below JSON. How can I do it? Thanks
RESPONSE RECEIVED IN ANDROID from Dialogflow.
{
"lang": "en",
"result": {
"source": "agent",
"fulfillment": {
"speech": "",
"messages": [
{
"type": 0,
"speech": ""
},
{
"type": 4,
"payload": {
"template_type": "list",
"elements": [
{
"NAME": "francis electrician",
"CONTACT": "98199 66928",
"ADDRESS": "sundar nagar, santacruz east, mumbai, maharashtra, india",
"AREA": "santacruz"
},
{
"NAME": "raj electrician\n",
"CONTACT": " 98205 45064",
"ADDRESS": "jdb seva sangh, gazdhar bandh sb patil road, govind nagar, santacruz west,, mumbai, maharashtra 400054, india",
"AREA": "santacruz"
}
]
}
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
}
}

You may use Android Dialogflow SDK. This may help you in building Android app integrated with Dialogflow. It will do the parsing on its own and give you objects to play with. Check out the documentation on their Github page for details.

Related

How to trigger Dialogflow ES intent when incoming message is atachment not a text

I have incoming message from Viber bot as attachment (location share) with no text in Dialogflow ES, so I'd like to trigger intent based on that message. Incoming request is like:
"payload": {
"attachments": [
{
"payload": {
"long": ##.####,
"address": "xxxxxxxx",
"lat": ##.#####
},
"type": "location"
}
],
"source": "viber",
"body": {
"sender": {
"language": "en-GB",
"api_version": 10,
"country": "xx",
"id": "xxxxxx",
"name": "xxxx xxxx"
},
"silent": false,
"event": "message",
"message_token": xxxx,
"chat_hostname": "xxxxx",
"message": {
"location": {
"lat": ##.#####,
"lon": ##.#####
},
"type": "location"
},
"timestamp": xxxxx
},
"contact": {
"channelId": xxxx,
"cId": "########",
"contactId": #######
}
}
}
I don't know how to trigger Dialogflow intent, because there are no words I can use as training phrases
I find an easy solution to this:
I only added "location" in events box and Dialogflow accept trigger the intent without any text in the message or training phrases to be marked
But I'm not sure what exactly triggers:
"type": "location"
or
"message": {
"location": {...
Any way it works.

Accumulating response parameters in Dialogflow conversation and then making a web-hook request by combining all parameters

I am making a basic travel chatbot using Dialogflow. Suppose that I get the boarding location in one response, landing location in 2nd response and the date of travel in third response from bot. Now I want to accumulate all three parameters and make a custom webhook response to some flight API. What's the correct way to do this?
Currently what I've done is increase the life span of each of the context to 15(max conversation length possible let's say) and then extract the parameters from outputContexts parameter from json. The json response has been attached below.
Another use case can be getting city and date in 2 follow up conversations and then making a combined web-hook response to fetch weather forecast.
{
"responseId": "some id",
"queryResult": {
"queryText": "London",
"action": "get_flights",
"parameters": {
"geo-city1": "London"
},
"allRequiredParamsPresent": true,
"fulfillmentText": "Here is a webhook response.",
"fulfillmentMessages": [
{
"text": {
"text": [
"Here is a webhook response."
]
}
}
],
"outputContexts": [
{
"name": "projects/project name/agent/sessions/some id/contexts/awaiting_flight_boolean",
"lifespanCount": 13,
"parameters": {
"date-time": "2020-11-14T12:00:00+05:30",
"geo-city1.original": "London",
"geo-city1": "London",
"date-time.original": "2 days from now"
}
},
{
"name": "projects/project name/agent/sessions/some id/contexts/await_landing_city",
"lifespanCount": 11,
"parameters": {
"geo-city1.original": "London",
"date-time.original": "2 days from now",
"BotName": "",
"from.original": "Dubai",
"date-time": "2020-11-14T12:00:00+05:30",
"geo-city1": "London",
"BotName.original": "",
"from": "Dubai"
}
},
{
"name": "projects/project name/agent/sessions/some id/contexts/awaiting_date_of_travel",
"lifespanCount": 12,
"parameters": {
"geo-city1": "London",
"from": "Dubai",
"geo-city1.original": "London",
"from.original": "Dubai",
"date-time.original": "2 days from now",
"date-time": "2020-11-14T12:00:00+05:30"
}
},
{
"name": "projects/project name/agent/sessions/some id/contexts/awaiting_boarding_location",
"lifespanCount": 14,
"parameters": {
"geo-city1": "London",
"geo-city1.original": "London"
}
}
],
"intent": {
"name": "projects/project name/agent/intents/some id",
"displayName": "BoardingTheFlight"
},
"intentDetectionConfidence": 1,
"languageCode": "en"
}
}
I am unable to come up with a better soultion as I am new to dialogflow. Can someone tell the correct/easier way to do this?
As you are new to the Dialogflow, I would like to recommend you Understand fulfillment by integrating Dialogflow with Calendar codelab, where you will be able to see the correct process of setting up fulfillment.
Your solution seems to be correct, additionally you can check this tutorial which is similar to your scenario.
Dialogflow also has some examples of requests and replies on Github.

Google chat custom cards using dialogflow fulfilment webhook

I am trying to integrate DialogFlow bot with Hangouts Chat (for G Suite). I have enabled the integration on DialogFlow and the basic intents are working fine.
In order to perform backend operations using fulfillment, I have created a firebase cloud function and added this as the webhook URL on DialogFlow fulfillment page.
I have written the cloud function code to identify the intent, and to generate the Webhook response format for a simple text response. This is working, and I am seeing the firestore data being modified in response to the intent.
However for a more complicated intent, I wish to use more of the dynamic card based response that Chat offers. In order to achieve this, I have looked at the documentation for dialogflow card response.
I saw this following code at https://cloud.google.com/dialogflow/docs/integrations/hangouts. When I paste this into the dialogflow intent editor UI under hangouts custom payload (after disabling webhook integration), it works
{
"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/..."
}
}
}
}]
}]
}]
}
}
This is exactly what I need, but I need this response from the webhook. I'm not getting the correct response format to map between the two.
When I try to integrate the same code with the webhook, I am not getting any reply on hangouts chat. When I check the history section on dialogflow UI, here is the response structure as mentioned in Raw interaction log
{
"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>"
}
}
I also found this link on chat docs which explains how to show an interactive card based UI https://developers.google.com/hangouts/chat/how-tos/cards-onclick. However I'm not able to understand how to integrate the same with the webhook.
UPDATE
I have followed a tutorial at https://www.leeboonstra.com/Bots/custom-payloads-rich-cards-dialogflow/ and was able to get the card response to show up using the sample code they mention. It is using this deprecated library (https://github.com/dialogflow/dialogflow-fulfillment-nodejs). Here is the code for that to work,
let payload = new Payload("hangouts", json, {
rawPayload: true,
sendAsMessage: true,
});
agent.add(payload);
Here the json variable should be the previous JSON structure I have mentioned. So now, I'm able to map to the correct response format using the deprecated API. However, I'm not able to get the button to send the right response to the back end. Here is the buttons field that I modified from the previous json,
"buttons": [
{
"textButton": {
"text": "Click Me",
"onClick": {
"action": {
"actionMethodName": "snooze",
"parameters": [
{
"key": "time",
"value": "1 day"
},
{
"key": "id",
"value": "123456"
}
]
}
}
}
}
]
As far as I know, responding to a Google Chat (formerly Hangouts Chat) button isn't possible when using the direct Dialogflow integration.
The problem is that the button response can be sent one of two ways:
An event will be sent back to the bot code indicating the click.
Using the onClick.openLink.url property, as most of your test show.
This will take the person clicking it to the URL in question. But once there, you're taken out of the bot flow.
However, the documentation for the Hangouts Chat integration with Dialogflow doesn't provide any information about how this event is passed to Dialogflow, and the last time I tested it - it isn't.
You can write your own integration using Google Chat's API on something like Cloud Functions or Apps Script and have your script call Dialogflow's Detect Intent API to determine what Intent would be triggered by the user (and determine replies or call the webhook for additional processing). Under this scheme, you can choose how to handle the onClick event. Making your own integration also provides you a way to do Incoming Webhooks, which isn't possible when using the Dialogflow integration.

Dialogflow - Fulfillment Webhook Response different from Documentation

I'm currently using Dialogflow in combination with fulfillment/webhooks.
In the documentation of the fulfillments there is an example POST request for webhooks:
POST body:
{
"contexts": [
string
],
"lang": string,
"query": string,
"sessionId": string,
"timezone": string
}
The request that I'm receiving on my end is different from what is defined in the documentation. Has somebody an idea why?
This is what I receive:
{
"id": "GUID",
"timestamp": "2018-01-12T12:25:32.202Z",
"lang": "de",
"result": {
"source": "agent",
"resolvedQuery": "Test",
"speech": "",
"action": "",
"actionIncomplete": false,
"parameters": {
"Nummer": ""
},
"contexts": [],
"metadata": {
"intentId": "XYZ",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"intentName": "Intent"
},
"fulfillment": {
"speech": "",
"messages": [{
"type": 0,
"speech": ""
}]
},
"score": 0.6700000166893005
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "GUID"
}
I've found that different invocation sources send slightly different data to the web hook.
For example the Google Action simulator will send different information in the context reply than say the test pane in an intent.
Im guessing that the answer may lie in where/how your calling the web hook.

How to determine the type of the platform in Dialogflow (API.AI)

How do to identify from which platform the message came?
I want to support different platforms like Telegram and Facebook Messenger, When my webhook receive a message, I want to reply according to the platform the message came form.
For example, if the message came from Telegram I want to return a text message but if the message came from messenger I want to return a card.
You have a property source in originalRequest object, see fulfillment docs here.
{
"lang": "en",
"status": {
"errorType": "success",
"code": 200
},
"timestamp": "2017-02-09T16:06:01.908Z",
"sessionId": "1486656220806",
"result": {
"parameters": {
"city": "Rome",
"name": "Ana"
},
"contexts": [],
"resolvedQuery": "my name is Ana and I live in Rome",
"source": "agent",
"score": 1.0,
"speech": "",
"fulfillment": {
"messages": [
{
"speech": "Hi Ana! Nice to meet you!",
"type": 0
}
],
"speech": "Hi Ana! Nice to meet you!"
},
"actionIncomplete": false,
"action": "greetings",
"metadata": {
"intentId": "9f41ef7c-82fa-42a7-9a30-49a93e2c14d0",
"webhookForSlotFillingUsed": "false",
"intentName": "greetings",
"webhookUsed": "true"
}
},
"id": "ab30d214-f4bb-4cdd-ae36-31caac7a6693",
"originalRequest": {
"source": "google",
"data": {
"inputs": [
{
"raw_inputs": [
{
"query": "my name is Ana and I live in Rome",
"input_type": 2
}
],
"intent": "assistant.intent.action.TEXT",
"arguments": [
{
"text_value": "my name is Ana and I live in Rome",
"raw_text": "my name is Ana and I live in Rome",
"name": "text"
}
]
}
],
"user": {
"user_id": "PuQndWs1OMjUYwVJMYqwJv0/KT8satJHAUQGiGPDQ7A="
},
"conversation": {
"conversation_id": "1486656220806",
"type": 2,
"conversation_token": "[]"
}
}
} }

Resources