How to retrieve the context from the agent in Dialogflow - dialogflow-es

I have managed to retrieve the context variables from the webhook using dialogflow-fulfillment agent.context.get() function however it does not provide the context variables that aren't set as input in that specific intent.
Intent A: "What city are you in?"
Through Dialogflow UI, the output context placeholder 'city' is set .
User answers => "Montréal"
Here I set the output context 'city' with the following code through the fulfiller:
agent.context.set('city', 100, {cityName: "Montréal"})
Intent B: "When will it freeze?"
Intentionally, no input/output context is set on Dialogflow. As I want the user to be able to ask this question without context.
When the intent is matched, Dialogflow sends the following request to my fullfiler.
{ "responseId": "496a4ed8-8421-4297-b464-adc54632cc93-32d6a6f2", "queryResult": { "queryText": "when will it freeze", "parameters": {}, "allRequiredParamsPresent": true, "fulfillmentText": "It will freeze next week", "fulfillmentMessages": [ { "text": { "text": [ "It will freeze next week" ] } } ], "intent": { "name": "projects/project-name/locations/global/agent/intents/7df0bb1d-f4ef-4e69-b9c4-4b6a2f91c95f", "displayName": "zone.freezing_dates via Fulfiller" }, "intentDetectionConfidence": 1, "diagnosticInfo": { "webhook_latency_ms": 59 }, "languageCode": "fr", "sentimentAnalysisResult": { "queryTextSentiment": { "score": 0.1, "magnitude": 0.1 } } }, "webhookStatus": { "message": "Webhook execution successful" }, "agentId": "5d8b71c7-cd3a-453a-b446-6d00334f1ae8" }
As you can see, the context parameters aren't present in the webhook request even though the city parameter has been set by intent A).
Therefore, how do we retrieve the context variables from the user's session (and not from the intent's webhook)?
I have a feeling it has to do with the agent session path as described here but in this sample, it's part of the intent (again) as I expect a way to retrieve the agent context outside of the intent's scope.
I will also add as a note, I know the city context var exists because I can see it through the Dialogflow UI when I add it to the response text.

Related

Having trouble with Google Assistant repeating previous message in Dialogflow

I'm working on a very simple Dialogflow with about 15-20 intents. All of these intents use a text response except one. The only intent that does not use a text response is called 'repeat'. The intent (repeat) should be able to repeat whatever was previously said by Google Assistant.
I've tried to set this up using Multivocal but have not been successful. When I type a command into the test simulator I'll get the initial response, but when I follow up with 'repeat' the default response of 'Not available' is returned. The webhook times out when I look at the Diagnostic Info. My sense is that I've configured something wrong because I've read these answers and not been able to solve my problem:
How to repeat last response of bot in dialogflow
Dialogflow - Repeat last sentence (voice) for Social Robot Elderly
Use multivocal libary to configure repeat intent in Dialogflow for VUI
I'm using the inline editor within Dialogflow my index.js looks like:
const Multivocal = require('multivocal');
const conf = {
Local: {
en: {
Response: {
"Action.multivocal.repeat": "Let me try again",
}
}
}
};
new Multivocal.Config.Simple( conf );
exports.webhook = Multivocal.processFirebaseWebhook;
exports.dialogflowFirebaseFulfillment = Multivocal.processFirebaseWebhook;
And my package.json includes the Multivocal dependency:
"multivocal": "^0.15.0"
My understanding based on the above SO questions is that these config values would be enough and I don't need to do any coding, but I'm clearly screwing something (many things?) up. How can I get the prior response in Google Assistant to repeat when a user says 'repeat' or something similar? Multivocal seems like a simple solution, if I can do it that way.
Additional logs:
Fulfillment request (removed project id information):
{
"responseId": "--",
"queryResult": {
"queryText": "repeat",
"action": "multivocal.repeat",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
"outputContexts": [
{
"name": "project info",
"parameters": {
"no-input": 0,
"no-match": 0
}
}
],
"intent": {
"name": "project info",
"displayName": "repeat"
},
"intentDetectionConfidence": 1,
"languageCode": "en"
},
"originalDetectIntentRequest": {
"payload": {}
},
"session": "project info"
}
Raw API response (removed project and response id)
{
"responseId": "",
"queryResult": {
"queryText": "repeat",
"action": "multivocal.repeat",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
"intent": {
"name": "projects info",
"displayName": "repeat"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 4992
},
"languageCode": "en"
},
"webhookStatus": {
"code": 4,
"message": "Webhook call failed. Error: DEADLINE_EXCEEDED."
}
}
My simple intent that I've added based on the recommendation that for repeat to work on an intent it must use fulfillment and not based text response in Dialogflow
Here is my index.js file using the inline editor with suggestion to add text response in the config:
const conf = {
Local: {
en: {
Response: {
"Intent.help": [
"I'm sorry, I'm not able to help you.",
"You, John, Paul, George, and Ringo ey?"
],
"Action.multivocal.repeat": "Let me try again"
}
}
}
};
This line at the end of my index.js seems odd to me, but may be unrelated:
exports.webhook = Multivocal.processFirebaseWebhook;
exports.dialogflowFirebaseFulfillment = Multivocal.processFirebaseWebhook;
It sounds like you're triggering the Fallback Intent. You also need an Intent defined in Dialogflow that has an Action set to "multivocal.repeat". That might look something like this:
In the dialogflow directory of the npm package (or on github) you'll find a zip file with this and several other "standard" Intents that you can use with mulivocal.
Additionally, all the other Intents that you want to be repeated must use fulfillment to send the response (the library doesn't know what might be sent unless it can send it itself). The simplest way to do this is to enable fulfillment on each, and move the text responses from their Dialogflow screens into the configuration under an entry such as "Intent.name" (replacing "name" with the name of the Intent) or "Action.name" if you set an action name for them.
So your configuration might be something like
const conf = {
Local: {
en: {
Response: {
"Intent.welcome": [
"Hello there!",
"Welcome to my Action!"
],
"Action.multivocal.repeat": [
"Let me try again"
]
}
}
}
};

Dialogflow Fulfillment URLnot sending any data

I am building a bot for google assistant. I have enabled fulfillment section for some intents. Dialog flow sends the request to the fulfillment url. The url is executed and a hard coded response is returned. I can see the response in the assistant simulator. Everything works fine except one thing. The request is empty.I can't access fields that are supposed to be present in the request.
I have accessed the same url using post request from a python code and it displays the parameters. So, there are no issues in the code. I think I am missing some configuration option.
I was expecting the post body in the following format:
POST body:
{
"responseId": "ea3d77e8-ae27-41a4-9e1d-174bd461b68c",
"session": "projects/your-agents-project-id/agent/sessions/88d13aa8-2999-4f71-b233-39cbf3a824a0",
"queryResult": {
"queryText": "user's original query to your agent",
"parameters": {
"param": "param value"
},
"allRequiredParamsPresent": true,
"fulfillmentText": "Text defined in Dialogflow's console for the intent that was matched",
"fulfillmentMessages": [
{
"text": {
"text": [
"Text defined in Dialogflow's console for the intent that was matched"
]
}
}
],
"outputContexts": [
{
"name": "projects/your-agents-project-id/agent/sessions/88d13aa8-2999-4f71-b233-39cbf3a824a0/contexts/generic",
"lifespanCount": 5,
"parameters": {
"param": "param value"
}
}
],
"intent": {
"name": "projects/your-agents-project-id/agent/intents/29bcd7f8-f717-4261-a8fd-2d3e451b8af8",
"displayName": "Matched Intent Name"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {},
"languageCode": "en"
},
"originalDetectIntentRequest": {}
}
But when I print the post data using print(request.POST), the actual post request shown is
One more thing: Does dialog flow append the action at the end of the fulfillment url? If so, I will have to handle the logic separately. I have done it without considering the action name. But a lot of my stuff is hacked, so I just want to be sure.
On another note, is dialogflow good enough? It has worked fine on a few examples similar to what it was trained on. How many training samples does it need to work properly? What is the underlying algorithm used in dialogflow? Or should I use the fulfillment url and handle everything on my own? I am inclined towards the later. I do not have too much faith in the existing chatbots.
Any help is appreciated.
If the Fallback Intent is the one being triggered, then you wouldn't get any parameters since this means that nothing else matched.
Got it. Used request.body. This solves the problem. Then parsed it using json.loads and accessed the parameters.

How do I set context in the response json payload?

I have setup a nodeJS server to return responses to a dialogflow webhook. I want to be able to send back a context in the JSON response so that dialogflow logic can move to the next desired Intent in the conversation flow.
In the response payload, I tried inserting "outputContexts" array in order to set the context. However this is not working in this form. The response Json is pasted below. The variable contextName holds the desired value of context to be sent back.
var simpleResponse = {
"payload": {
"google": {
"expectUserResponse": true,
"outputContexts": [
{
"name": "projects/my-agent-v2/agent/sessions/" + sessionId + "/contexts/" + contextName,
"lifespanCount": 1,
"parameters": {
"param": "param value"
}
}
],
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "Simple response",
"displayText": "Simple response"
}
}]}}}};
The control should move to intent = awaiting_more_details. The above returned context is the "input context" to this intent.
I hope this gives an understanding about what I want to achieve.
Your response has the wrong format, the outputContexts array must be in the top level object, next to payload. See Dialogflow's Discovery document for the authoritative specification: https://www.googleapis.com/discovery/v1/apis/dialogflow/v2beta1/rest

How to take Multi-value from one sentence using API.AI?

I'm new to Api.ai , i read the doc. but i didn't understand how Api.ai works better with many parameters.
I'll try to explain by an example :
I have a Management software which manages the members/actions/projects , where i can get the actions of any member at any project using the normal interface.
let's replace this with a smart bot where the chat will run as i expected below,
USER : i want to see my actions for ANY PROJECT NAME HERE
bot : your action is XXXXXX.
OR
USER: give me all the members of the project ANY PROJECT NAME
Bot: Members are "1-2-3-4-5-...."
i think you got what i mean , if you need more i can explain more.How can i let Api.ai understands this ?
For API.ai to 'remember' values (ie store and retrieve information such as the names of projects, actions and team members) you will need to connect API.ai to a webhook/database of your own, there isn't anyway for API.ai to do this on its own.
Once you connect API.ai to a custom webhook/database you can use the variables that API.ai will parse for you to run your query. You simply need to build the intents corresponding to the search and parameters involved
Here's how the process would flow:
User asks "I want to see my actions for [ANY PROJECT NAME HERE]"
API ai logic recognizes this as the intent 'search-action' for $project_name, you having set this up in API.ai like this
Your custom webhook receives JSON response from API.ai that in this case would look like this:
{
"id": "REDACTED",
"timestamp": "2017-04-19T03:18:18.028Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "I want to see my actions for project Unicorn",
"action": "search-action",
"actionIncomplete": false,
"parameters": {
"project_name": "project Unicorn"
},
"contexts": [],
"metadata": {
"intentId": "REDACTED",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Search - Actions"
},
"fulfillment": {
"speech": "",
"messages": [
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "REDACTED"
}
So, your webhook has logic that recognizes when result.action is 'search-action' is should run a database search for actions in project result.parameters.project_name
Your webhook fulfills the API.ai request, or alternatively, sends message to message platform directly (ie Facebook messenger)

Insert a card that can be directly responded to

update !important: The API has changed a lot, this question shouldn't be taken into consideration anymore
I am trying to use the REST api (via Node.js API) to create cards that the user can respond to and create an interaction in this way.
Reading the docs the creator attribute is not really specified anywhere, so I have no idea how to insert that.
Also this video doesn't help. Nor this guide =)
I believe there is an URL I should set as callback somehow? I'd like to know how to get these responses, please.
update
This is the card I am sending.
{
bundleId: 'veryuniqueBundle',
id: 'veryuniqueBundle:reply',
text: "want to hear moar?",
menuItems: [
{action: "REPLY"}
]
}
that's the response I get:
{
"collection": "timeline",
"itemId": "119c4dc8-c0ce-4a83-aa76-41aab4e8dbe1",
"operation": "INSERT",
"verifyToken": "42",
"userToken": "id:520ef63cde31145deb000001",
"userActions": [
{
"type": "REPLY"
}
]
}
The problem is, I can't see what the user responded (an text) and the reference to the original card id (or bundle) that was responded to. How can I get those
Cards do not provide a direct callback. Instead, when a user selects a menu item it causes the card to be updated with their menu selection. This change subsequently triggers a notification ping to your timeline subscription.
Follow these steps to detect a menu item selection:
Subscribe to notifications for changes in the timeline collection
{
"collection": "timeline",
"userToken": "awesome_kitty",
"verifyToken": "random_hash_to_verify_referer",
}
Insert a timeline card with a custom menu item
{
"text": "Hello world",
"menuItems": [
{
"action": "CUSTOM",
"id": "complete"
"values": [{
"displayName": "Complete",
"iconUrl": "http://example.com/icons/complete.png"
}]
}
]
}
Select the item on Glass
Receive the notification on your subscription URL
{
"collection": "timeline",
"itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"operation": "UPDATE",
"userToken": "harold_penguin",
"userActions": [
{
"type": "CUSTOM",
"payload": "PING"
}
]
}
Do cool stuff in your code
???
Profit

Resources