Dialogflow detect intent "payload" not working - node.js

I want to send custom parameters to the webhook. According to the documentation I set it under "payload" parameter. But I don't see values I set on response object.
https://dialogflow.com/docs/reference/api-v2/rpc/google.cloud.dialogflow.v2#google.cloud.dialogflow.v2.QueryParameters
Here is my code
function detectIntent(query, sessionId, contextData, callback) {
let projectId = config.get('projectId');
let languageCode = 'en-US';
let sessionPath = sessionClient.sessionPath(projectId, sessionId);
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
}
},
queryParams: {
contexts: [
contextData
],
payload: { foo: "bar" }
},
};
sessionClient
.detectIntent(request)
.then(responses => {
const result = responses[0].queryResult;
callback(null, result);
})
.catch(err => {
callback(err, null);
});
}
Here is the response I'm getting
{
"fulfillmentMessages": [
{
"platform": "PLATFORM_UNSPECIFIED",
"text": {
"text": [
""
]
},
"message": "text"
}
],
"outputContexts": [
{
"name": "projects/ddddd-102d1/agent/sessions/blvy6skjngu4kvt/contexts/blvy6skjngu4kvu",
"lifespanCount": 2,
"parameters": {
"fields": {
"msisdn": {
"stringValue": "773959698",
"kind": "stringValue"
}
}
}
},
{
"name": "projects/ddddd-102d1/agent/sessions/blvy6skjngu4kvt/contexts/actionshow_card-followup",
"lifespanCount": 2,
"parameters": null
}
],
"queryText": "internet slow",
"speechRecognitionConfidence": 0,
"action": "action.show_card",
"parameters": {
"fields": {}
},
"allRequiredParamsPresent": true,
"fulfillmentText": "",
"webhookSource": "",
"webhookPayload": null,
"intent": {
"inputContextNames": [],
"events": [],
"trainingPhrases": [],
"outputContexts": [],
"parameters": [],
"messages": [],
"defaultResponsePlatforms": [],
"followupIntentInfo": [],
"name": "projects/ddddd-102d1/agent/intents/e378b17a-d899-4e69-8dfd-4b938b0222a5",
"displayName": "action.show_card",
"priority": 0,
"isFallback": false,
"webhookState": "WEBHOOK_STATE_UNSPECIFIED",
"action": "",
"resetContexts": false,
"rootFollowupIntentName": "",
"parentFollowupIntentName": "",
"mlDisabled": false
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"fields": {
"webhook_latency_ms": {
"numberValue": 161,
"kind": "numberValue"
}
}
},
"languageCode": "en-us"
}
I tried with formatting the payload json by doing structjson.jsonToStructProto({foo: 'bar'}) as mention in following link
Send parameters to webhook on dialogflow sdk v2
but no success.

Related

Filter data using nodejs and elasticsearch

I'm currently facing an issue with my datatable implemented in ReactJS. I'm retrieving data from elasticsearch and populating the datatable with it. The data retrieval process works fine without the filter applied, however, when I apply filters to the data, the datatable remains empty, even though the data _source has matching records.
The structure of the parameters I am sending is as follows:
{
pageIndex: 1,
pageSize: 10,
sort: { order: '', key: '' },
query: '',
filterData: {
analysis: [ '0', '1', '2', '3' ],
threat_level_id: [ '1', '2', '3', '4' ],
}
}
EndPoint:
POST /api/v1/events/public/list
Controller:
exports.getPublicEvents = async (req, res) => {
try {
client.ping()
const { pageIndex, pageSize, sort, query, filterData } = req.body
let esQuery = {
index: 'ns_*',
body: {
query: {
bool: {
must: [
{
match_all: {},
},
],
filter: [],
},
},
from: (pageIndex - 1) * pageSize,
size: pageSize,
},
}
if (query) {
esQuery.body.query.bool.must = [
{
match: {
'Event.info': {
query: query,
fuzziness: 'AUTO',
},
},
},
]
}
if (filterData.analysis.length > 0) {
esQuery.body.query.bool.filter.push({
terms: {
'Event.analysis': filterData.analysis,
},
})
}
if (filterData.threat_level_id.length > 0) {
esQuery.body.query.bool.filter.push({
terms: {
'Event.threat_level_id': filterData.threat_level_id,
},
})
}
let esResponse = await client.search(esQuery)
let data = esResponse.hits.hits.map((hit) => hit._source)
let total = esResponse.hits.total.value
res.status(200).json({
status: 'success',
data: data,
total: total,
})
} catch (error) {
res.status(500).json({
error: 'Error connecting to Elasticsearch',
errorMessage: error.message,
})
}
}
The controller below is without filters and it works just fine.
exports.getPublicEvents = async (req, res) => {
try {
client.ping()
const { pageIndex, pageSize, sort, query } = req.body
let esQuery = {
index: 'ns_*',
body: {
query: {
match_all: {},
},
from: (pageIndex - 1) * pageSize,
size: pageSize,
},
}
if (query) {
esQuery.body.query = {
match: {
'Event.info': {
query: query,
fuzziness: 'AUTO',
},
},
}
}
let esResponse = await client.search(esQuery)
let data = esResponse.hits.hits.map((hit) => hit._source)
let total = esResponse.hits.total.value
res.status(200).json({
status: 'success',
data: data,
total: total,
})
} catch (error) {
res.status(500).json({
error: 'Error connecting to Elasticsearch',
errorMessage: error.message,
})
}
}
ElasticSearech version: 7.17.8
Result of: console.log(JSON.stringify(esQuery))
{
"index": "INDEX_NAME",
"body": {
"query": {
"bool": {
"must": [{ "match_all": {} }],
"filter": [
{ "terms": { "Event.analysis": ["0", "1", "2"] } },
{ "terms": { "Event.threat_level_id": ["1", "2", "3", "4"] } }
]
}
},
"from": 0,
"size": 10
}
}
Data in elascticsearch schema
{
"#version": "1",
"#timestamp": "2023-02-01T14:43:09.997Z",
"Event": {
"info": ".......................",
"description": ".......................",
"analysis": 0,
"threat_level_id": "4",
"created_at": 1516566351,
"uuid": "5a64f74f0e543738c12bc973322",
"updated_at": 1675262417
}
}
Index Mapping
{
"index_patterns": ["INDEX_NAME"],
"template": "TEMPLATE_NAME",
"settings": {
"number_of_replicas": 0,
"index.mapping.nested_objects.limit": 10000000
},
"mappings": {
"dynamic": false,
"properties": {
"#timestamp": {
"type": "date"
},
"Event": {
"type": "nested",
"properties": {
"date_occured": {
"type": "date"
},
"threat_level_id": {
"type": "integer"
},
"description": {
"type": "text"
},
"is_shared": {
"type": "boolean"
},
"analysis": {
"type": "integer"
},
"uuid": {
"type": "text"
},
"created_at": {
"type": "date"
},
"info": {
"type": "text"
},
"shared_with": {
"type": "nested",
"properties": {
"_id": {
"type": "text"
}
}
},
"updated_at": {
"type": "date"
},
"author": {
"type": "text"
},
"Attributes": {
"type": "nested",
"properties": {
"data": {
"type": "text"
},
"type": {
"type": "text"
},
"uuid": {
"type": "text"
},
"comment": {
"type": "text"
},
"category": {
"type": "text"
},
"value": {
"type": "text"
},
"timestamp": {
"type": "date"
}
}
},
"organisation": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"uuid": {
"type": "text"
}
}
},
"Tags": {
"type": "nested",
"properties": {
"color": {
"type": "text"
},
"name": {
"type": "text"
}
}
},
"TLP": {
"type": "nested",
"properties": {
"color": {
"type": "text"
},
"name": {
"type": "text"
}
}
}
}
}
}
}
}
Event is a nested field, so you need to use nested queries, like this:
{
"index": "INDEX_NAME",
"body": {
"query": {
"bool": {
"must": [{ "match_all": {} }],
"filter": [
{
"nested": {
"path": "Event",
"query": {"terms": { "Event.analysis": ["0", "1", "2"] }}
}
},
{
"nested": {
"path": "Event",
"query": {"terms": { "Event.threat_level_id": ["1", "2", "3", "4"] }}
}
}
]
}
},
"from": 0,
"size": 10
}
}

mongodb FINDBYIDANDUPDATE() is not returning the updated document

hello I am trying to update a document on mongodb but everytime I run the lambda function below it does not return the new document but returns a http response 200. I have tried to update the logic in so many forms but I get the response 200 but the update does not happen. The lambda function takes 2 path parameters i.e the userid and the date to find the specific plan then updates it... It neither updates nor returns the new document. Please help!
const Plan = require('../../model/planModel');
const User = require('../../model/userModel');
const { connectDB } = require('../../config/db');
const { verify, decode } = require('jsonwebtoken');
const { decode_token } = require('../../utils');
const { findByIdAndUpdate } = require('../../model/planModel');
exports.handler = async (event) => {
try {
await connectDB();
//console.log(event)
const { userid, date } = event.pathParameters;
const exists = await Plan.findOne(userid, date);
console.log('I made it here: ', exists);
const {
excitedAbout,
gratefulForMorning,
mainThreeTodo,
dailyToDo,
meals,
water,
exercise,
meditation,
relaxation,
happyScale,
happyMoment,
gratefulForTonight,
dailyQuestion,
} = event.body;
const updatedPlan = await Plan.findByIdAndUpdate(
{ _id: exists._id },
{
excitedAbout: excitedAbout,
gratefulForMorning: gratefulForMorning,
mainThreeTodo: mainThreeTodo,
dailyToDo: dailyToDo,
meals: meals,
water: water,
exercise: exercise,
meditation: meditation,
relaxation: relaxation,
happyScale: happyScale,
happyMoment: happyMoment,
gratefulForTonight: gratefulForTonight,
dailyQuestion: dailyQuestion,
},
{ new: true }
);
console.log('this is a plan', updatedPlan);
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(updatedPlan),
};
} catch (error) {
console.log(error);
}
};
This is the data I want to update:
{
"user": "62d1a251806d10095ff0f0a6",
"date": "2022-09-09T18:30:00.000Z",
"excitedAbout": "Excited About something",
"gratefulForMorning": "",
"mainThreeTodo": [
{
"text": "HELLO I survived",
"checked": false
},
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
}
],
"dailyToDo": [
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
}
],
"meals": {
"breakfast": [
{
"meal": "",
"calories": ""
}
],
"lunch": [
{
"meal": "",
"calories": ""
}
],
"dinner": [
{
"meal": "skuma",
"calories": ""
}
]
},
"water": 7,
"exercise": false,
"relaxation": false,
"meditation": false,
"happyScale": 0,
"happyMoment": "",
"gratefulForTonight": "",
"dailyQuestion": {
"question": "",
"answer": ""
}
}
and this is what I'm getting
{
"meals": {
"breakfast": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c3"
}
],
"lunch": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c2"
}
],
"dinner": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c1"
}
]
},
"dailyQuestion": {
"question": "",
"answer": ""
},
"_id": "631ba3e559b8a53d732a1958",
"user": "62d1a251806d10095ff0f0a6",
"date": "2022-09-09T18:30:00.000Z",
"excitedAbout": "",
"gratefulForMorning": "",
"mainThreeTodo": [
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bb"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bc"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bd"
}
],
"dailyToDo": [
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2be"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bf"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2c0"
}
],
"water": 0,
"exercise": false,
"meditation": false,
"relaxation": false,
"happyScale": 0,
"happyMoment": "",
"gratefulForTonight": "",
"createdAt": "2022-09-09T20:36:53.680Z",
"updatedAt": "2022-09-13T12:01:02.966Z",
"__v": 0
}
it's like it is never passed
Your query seems to be missing the update operators, that's why it might not be working for you:
const updatedPlan = await Plan.findByIdAndUpdate(
{ _id: exists._id },
{
$set: {
excitedAbout: excitedAbout,
gratefulForMorning: gratefulForMorning,
mainThreeTodo: mainThreeTodo,
dailyToDo: dailyToDo,
meals: meals,
water: water,
exercise: exercise,
meditation: meditation,
relaxation: relaxation,
happyScale: happyScale,
happyMoment: happyMoment,
gratefulForTonight: gratefulForTonight,
dailyQuestion: dailyQuestion,
}
},
{ new: true }
);
Try this, here we have specified the $set update operator.

GCP DialogFlow, how can i pass inputcontext as value in webhook parameters

The below is the request body that is received by webhook. The intent i have has multiple input contexts. How do i know, from which context, is this intent called? There is no value that says, from which input context, this intent/action is called.
{
"responseId": "a218fe93-f2d1-4bdd-bcdf-618195838f53-9a925d01",
"queryResult": {
"queryText": "asEvent",
"action": "asknAction",
"parameters": {
},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
"outputContexts": [
{
"name": "projects//contexts/asknumbers-followup",
"lifespanCount": 5,
"parameters": {
}
},
{
"name": "projects/b/contexts/defaultwelcomeintent-next-followup",
"lifespanCount": 5,
"parameters": {
}
},
{
"name": "projects/b/contexts/askmultiplicationevent",
"parameters": {
}
},
{
"name": "projects/b/contexts/genmultiplicationdummy-followup",
"lifespanCount": 4,
"parameters": {
}
},
{
"name": "projects/mlkaggle-288509/agent/sessions/4db2f820-cca0-989e-a2e7-fe7a9f6cbc2b/contexts/__system_counters__",
"parameters": {
}
}
],
"intent": {
"name": "projects/m09/agent/intents/192c043b-78a2-4af9-838e-ceebd89dc6c5",
"displayName": "askMultiplication"
},
"intentDetectionConfidence": 1,
"languageCode": "en"
},
"originalDetectIntentRequest": {
"source": "DIALOGFLOW_CONSOLE",
"payload": {}
},
"session": "projects/9/agent/sessions/4db2f820-cca0-989e-a2e7-fe7a9f6cbc2b"
}
Based on the input context, i would like to save data into database different values.

Dialogflow textToSpeech fulfilment not reading aloud the text

I am providing users with a response on an audio only device (e.g. google home), when I respond with a textToSpeech field within a simpleResponse, the speech is not read out in the simulator.
Has anyone experienced this and know how to fix?
I've tried different response types but none of them read out the textToSpeech field.
Also tried ticking/unticking end conversation toggle in Dialogflow and expectUserInput true/false when responding with JSON to no avail.
The response is currently fulfilled by a webhook which responds with JSON v2 fulfilment blob and the simulator receives the response with no errors but does not read it out.
RESPONSE -
{
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "Here are the 3 closest restaurants that match your criteria,"
}
}
]
}
}
}
}
REQUEST -
{
"responseId": "404f3b65-73a5-47db-9c17-0fc8b31560a5",
"queryResult": {
"queryText": "actions_intent_NEW_SURFACE",
"parameters": {},
"allRequiredParamsPresent": true,
"outputContexts": [
{
"name": "projects/my-project/agent/sessions/sessionId/contexts/findrestaurantswithcuisineandlocation-followup",
"lifespanCount": 98,
"parameters": {
"location.original": "Shoreditch",
"cuisine.original": "international",
"cuisine": "International",
"location": {
"subadmin-area": "Shoreditch",
"subadmin-area.original": "Shoreditch",
"subadmin-area.object": {}
}
}
},
{
"name": "projects/my-project/agent/sessions/sessionId/contexts/actions_capability_account_linking"
},
{
"name": "projects/my-project/agent/sessions/sessionId/contexts/actions_capability_audio_output"
},
{
"name": "projects/my-project/agent/sessions/sessionId/contexts/google_assistant_input_type_voice"
},
{
"name": "projects/my-project/agent/sessions/sessionId/contexts/actions_capability_media_response_audio"
},
{
"name": "projects/my-project/agent/sessions/sessionId/contexts/actions_intent_new_surface",
"parameters": {
"text": "no",
"NEW_SURFACE": {
"#type": "type.googleapis.com/google.actions.v2.NewSurfaceValue",
"status": "CANCELLED"
}
}
}
],
"intent": {
"name": "projects/my-project/agent/intents/0baefc9d-689c-4c33-b2b8-4e130f626de1",
"displayName": "Send restaurants to mobile"
},
"intentDetectionConfidence": 1,
"languageCode": "en-us"
},
"originalDetectIntentRequest": {
"source": "google",
"version": "2",
"payload": {
"isInSandbox": true,
"surface": {
"capabilities": [
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.MEDIA_RESPONSE_AUDIO"
},
{
"name": "actions.capability.ACCOUNT_LINKING"
}
]
},
"requestType": "SIMULATOR",
"inputs": [
{
"rawInputs": [
{
"query": "no",
"inputType": "VOICE"
}
],
"arguments": [
{
"extension": {
"#type": "type.googleapis.com/google.actions.v2.NewSurfaceValue",
"status": "CANCELLED"
},
"name": "NEW_SURFACE"
},
{
"rawText": "no",
"textValue": "no",
"name": "text"
}
],
"intent": "actions.intent.NEW_SURFACE"
}
],
"user": {
"userStorage": "{\"data\":{}}",
"lastSeen": "2019-04-12T14:31:23Z",
"locale": "en-US",
"userId": "userID"
},
"conversation": {
"conversationId": "sessionId",
"type": "ACTIVE",
"conversationToken": "[\"defaultwelcomeintent-followup\",\"findrestaurantswithcuisineandlocation-followup\",\"findrestaurantswithcuisineandlocation-followup-2\"]"
},
"availableSurfaces": [
{
"capabilities": [
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
},
{
"name": "actions.capability.WEB_BROWSER"
}
]
}
]
}
},
"session": "projects/my-project/agent/sessions/sessionId"
}
I expect the simulator to read out the result of textToSpeech but currently does not.

dialogflow webhook not considering payload using fulfillmentText, fulfillmentMessages

FULFILLMENT REQUEST
{
"responseId": "4955f972-058c-44c2-a9c6-fe2c1d846fcd",
"queryResult": {
"queryText": "dsnaf",
"action": "intentNotMatched",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentText": "I think I may have misunderstood your last statement.",
"fulfillmentMessages": [
{
"text": {
"text": [
"I'm afraid I don't understand."
]
}
}
],
"outputContexts": [
{
"name": "****",
"lifespanCount": 1
}
],
"intent": {
"name": "****",
"displayName": "Default Fallback Intent",
"isFallback": true
},
"intentDetectionConfidence": 1,
"languageCode": "en"
},
"originalDetectIntentRequest": {
"payload": {}
},
"session": "****"
}
FULFILLMENT RESPONSE
{
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "I'm sorry. I didn't quite grasp what you just said."
}
}
]
},
"userStorage": "{\"data\":{}}"
}
},
"outputContexts": [
{
"name": "***",
"lifespanCount": 99,
"parameters": {
"data": "{}"
}
}
]
}
RAW API RESPONSE
{
"responseId": "4955f972-058c-44c2-a9c6-fe2c1d846fcd",
"queryResult": {
"queryText": "dsnaf",
"action": "intentNotMatched",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
"I'm afraid I don't understand."
]
}
}
],
"webhookPayload": {
"google": {
"userStorage": "{\"data\":{}}",
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "I'm sorry. I didn't quite grasp what you just said."
}
}
]
},
"expectUserResponse": true
}
},
"outputContexts": [
{
"name": "*****",
"lifespanCount": 99,
"parameters": {
"data": "{}"
}
},
{
"name": "******",
"lifespanCount": 1
}
],
"intent": {
"name": "****",
"displayName": "Default Fallback Intent",
"isFallback": true
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 286
},
"languageCode": "en"
},
"webhookStatus": {
"message": "Webhook execution successful"
}
}
Google Assistance response
USER SAYS dsnaf
DEFAULT RESPONSE I'm afraid I don't understand.
CONTEXTS
_actions_on_google,initial_chat
INTENT Default Fallback Intent
IN Google Assistance response is default fulfillmentText instead payload google richresponse
Where are you testing this? If you're in the test console, it's always going to show you the simple text response. You'll need to specifically select Google Assistant in the test console to see the rich response for that platform:

Resources