我正在尝试将参数传递给意图,并通过事件名称来调用意图。
我知道必须将参数对象从Json转换为Struct,但是由于某些原因,参数没有通过。我在做什么错了?
请注意,因为我要导出到Firebase Functions,所以必须将Google提供的“ structjson.js”复制并粘贴到与index.js相同的目录中。 constjson.js中某些函数内部的常量,因此我可以导出这些函数以在index.js文件中使用。
对话框流的预期响应: “您的用户ID为:00000001”
Dialogflow的实际答复: “您的用户ID是:$ {n_digit}”
请求对话流代码
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);
});
提前谢谢!
答案 0 :(得分:1)
您可以发送类似参数
{
"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":{ }
}
使用NodeJS client可以保存
之类的参数let param1 = [];
let param2 = {};
let ctx = {'name': '<context name>', 'lifespan': 5, 'parameters': {'param1':param1, 'param2': param2}};
agent.setContext(ctx);
并访问这些参数,例如
let params = agent.getContext("<context name>").parameters;
let param1 = params.param1;
let param2 = params.param2;
查看我的complete answer here。