Reputation: 507
I am trying to pass parameters to an intent and I am calling the intent by it's Event name.
I understand that the parameters object has to be converted from a Json to a Struct, but for some reason the parameters are not passing. What am I doing wrong?
Please note I had to copy and paste the 'structjson.js' provided by Google into the same directory as my index.js because I am exporting to Firebase Functions, and I had to place some constants inside of certain functions within the structjson.js so I could export those functions to use in my index.js file.
Expected response from Dialogflow: 'Your user ID is: 00000001'
Actual response from Dialogflow: 'Your user ID is: ${n_digit}'
Request to Dialogflow Code
import * as functions from 'firebase-functions';
import * as dialogflow from 'dialogflow';
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const register = functions.https.onRequest((request, response) => {
// Instantiates a session client
const projectId = [MY_PROJECT_ID];
const sessionId = '123456789';
const languageCode = 'en-US';
let config = {
credentials: {
private_key: [MY_PRIVATE_KEY],
client_email: [MY_CLIENT_EMAIL]
}
}
const sessionClient = new dialogflow.SessionsClient(config);
// The path to identify the agent that owns the created intent.
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
var structjson = require('./structjson')
// The text query request.
const req = {
session: sessionPath,
queryParams: {
"resetContexts": true,
},
queryInput: {
event: {
name: 'init',
parameters: structjson.jsonToStructProto({userID: '00000001'}),
languageCode: languageCode,
},
},
};
sessionClient
.detectIntent(req)
.then((responses) => {
const result = responses[0].queryResult;
response.send(result.fulfillmentText);
})
.catch(err => {
console.error('ERROR:', err);
});
})
structjson.js
module.exports.jsonToStructProto = function jsonToStructProto(json) {
var structjson = require('./structjson')
const fields = {};
for (const k in json) {
fields[k] = structjson.jsonValueToProto(json[k]);
}
return {fields};
}
module.exports.jsonValueToProto = function jsonValueToProto(value) {
const valueProto = {};
const JSON_SIMPLE_TYPE_TO_PROTO_KIND_MAP = {
[typeof 0]: 'numberValue',
[typeof '']: 'stringValue',
[typeof false]: 'boolValue',
};
if (value === null) {
valueProto.kind = 'nullValue';
valueProto.nullValue = 'NULL_VALUE';
} else if (value instanceof Array) {
valueProto.kind = 'listValue';
valueProto.listValue = {values: value.map(jsonValueToProto)};
} else if (typeof value === 'object') {
valueProto.kind = 'structValue';
valueProto.structValue = jsonToStructProto(value);
} else if (typeof value in JSON_SIMPLE_TYPE_TO_PROTO_KIND_MAP) {
const kind = JSON_SIMPLE_TYPE_TO_PROTO_KIND_MAP[typeof value];
valueProto.kind = kind;
valueProto[kind] = value;
} else {
console.warn('Unsupported value type ', typeof value);
}
return valueProto;
}
module.exports.structProtoToJson = function structProtoToJson(proto) {
if (!proto || !proto.fields) {
return {};
}
const json = {};
for (const k in proto.fields) {
json[k] = valueProtoToJson(proto.fields[k]);
}
return json;
}
module.exports.valueProtoToJson = function valueProtoToJson(proto) {
const JSON_SIMPLE_VALUE_KINDS = new Set([
'numberValue',
'stringValue',
'boolValue',
]);
if (!proto || !proto.kind) {
return null;
}
if (JSON_SIMPLE_VALUE_KINDS.has(proto.kind)) {
return proto[proto.kind];
} else if (proto.kind === 'nullValue') {
return null;
} else if (proto.kind === 'listValue') {
if (!proto.listValue || !proto.listValue.values) {
console.warn('Invalid JSON list value proto: ', JSON.stringify(proto));
}
return proto.listValue.values.map(valueProtoToJson);
} else if (proto.kind === 'structValue') {
return structProtoToJson(proto.structValue);
} else {
console.warn('Unsupported JSON value proto kind: ', proto.kind);
return null;
}
}
Dialogflow webhook
function receiveParams (agent) {
const parameter = request.body.queryResult.parameters;
const n_digit = parameter.userID;
agent.add('Your user ID is: ${n_digit}');
}
let intentMap = new Map();
intentMap.set('init', receiveParams);
agent.handleRequest(intentMap);
});
Thank you in advance!
Upvotes: 1
Views: 8880
Reputation: 5266
You can send parameters like
{
"fulfillmentText":"This is a text response",
"fulfillmentMessages":[ ],
"source":"example.com",
"payload":{
"google":{ },
"facebook":{ },
"slack":{ }
},
"outputContexts":[
{
"name":"<Context Name>",
"lifespanCount":5,
"parameters":{
"<param name>":"<param value>"
}
}
],
"followupEventInput":{ }
}
Using NodeJS client you can save params like
let param1 = [];
let param2 = {};
let ctx = {'name': '<context name>', 'lifespan': 5, 'parameters': {'param1':param1, 'param2': param2}};
agent.setContext(ctx);
and access these params like
let params = agent.getContext("<context name>").parameters;
let param1 = params.param1;
let param2 = params.param2;
Check out my complete answer here.
Upvotes: 1