i'm trying to create a context with some parameters passed into it so it can be used when the intent is resolved.
The issue is the context is created but the parameters don't show up.
I have this function for creating the context.
const struct = require('./struct');
const createContext = (context, senderId, lifespan, parameters) => {
const sessionPath = contextClient.sessionPath(
process.env.DIALOGFLOW_PROJECT_ID,
senderId
);
const contextPath = contextClient.contextPath(
process.env.DIALOGFLOW_PROJECT_ID,
senderId,
context
);
const contextRequest = {
parent: sessionPath,
context: {
name: contextPath,
lifespanCount: lifespan
}
};
if (parameters) contextRequest.context.parameters = struct(parameters);
return contextClient.createContext(contextRequest);
};
struct.js
const jsonToStructProto = function jsonToStructProto(json) {
const fields = {};
for (const k in json) {
fields[k] = jsonValueToProto(json[k]);
}
return { fields };
};
const 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;
};
const 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;
};
const 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;
}
};
module.exports = jsonToStructProto;
any help would be very much appreciated!
Having the exact same issue! Parameters are either removed or Dialogflow will replace them with defaults. This happens across the entire array of contexts.
Any updates on this??
You need to create an event in your dialogflow agent ex.'init'. Pass this as parameter to sendQuery() method below . Can use the following format for requestBody while calling DetectIntent() method. Try with the following code
const dialogflow = require('dialogflow');
const struct = require('./struct');
async function createContext(sessionId, contextId, parameters) {
const sessionPath = contextsClient.sessionPath(projectId, sessionId);
const contextPath = contextsClient.contextPath(
projectId,
sessionId,
contextId
);
const request = {
parent : sessionPath,
context : {
name : contextPath,
parameters : struct(parameters),
lifespanCount : 5
}
}
const context = await contextsClient.createContext(request);
return context;
}
function sendQuery(sessionId, query, context) {
const session = sessionsClient.sessionPath(projectId, sessionId);
const request = {
session,
queryInput : {
event : {
name : query,
languageCode: 'en-US'
}
},
queryParameters : {
contexts : context,
'resetContexts' : true
}
};
return sessionsClient.detectIntent(request);
}
(async() => {
const parameters = { name: 'John' };
const sessionId = 'my-session';
const context = await createContext(sessionId,[YOUR-CONTEXT], parameters);
const response = await sendQuery(sessionId, [QUERY], context);
console.log(response[0].queryResult.parameters);
})();
Let me know if this solution works for you.
Haven't seen much activity here lately, I'm going to assume that @vishald123's solution did the trick. If that's not the case please let us know and we'll be happy to continue investigating 馃槑
Most helpful comment
Having the exact same issue! Parameters are either removed or Dialogflow will replace them with defaults. This happens across the entire array of
contexts.