How can i pass parameters through NodeJS request? I would like to pass, for example, the name from my code and dialogflow automatically answer with a response which contains the parameters i passed, as "Hi $name".
Mine actual request:
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
EDIT 4 [index.js], this is new right code After the answer
const projectId = 'your-project-id';
const sessionId = 'session-id';
const query = 'your-query';
const languageCode = 'your-language';
const j = require("./structjson.js"); //download It from the answer
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient({keyFilename: './THIS-IS-RIGHT.json'});
// Define session path
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
async function request() {
const contextClient = new dialogflow.ContextsClient({ keyFilename: './your-path-to-file.json' });
const contextData = {
name: contextClient.contextPath(projectId, sessionId, 'your-context'),
parameters: j.jsonToStructProto({ name: 'John' }),
lifespanCount: 1
};//An example for the name
const context = await contextClient.createContext({
parent: sessionPath,
context: contextData
});
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode
}
},
queryParams: {
contexts: context // You may want to add the other contexts here
}
};
const result = await sessionClient.detectIntent(request);
console.log(result);
}
request();
You can pass parameters when sending an event
instead of text
.
You will need to convert a javascript object to proto struct. There is a package pb-util that will handle the encoding/decoding
const { struct } = require('pb-util');
const request = {
session: sessionPath,
queryInput: {
event: {
name: eventName,
parameters: struct.encode({ name: 'John' }),
languageCode
}
}
};
After that you will need to create a parameter with the following syntax on your intent. #eventName.name
Another way to do it, is creating a context, using dialogflow.ContextsClient
& client.createContext()
and add the parameters to the context, and then send that context with the queryInput
request.
async function request() {
const contextClient = new dialogflow.ContextsClient({ keyFilename: '..' });
const sessionClient = new dialogflow.SessionsClient({ keyFilename: '..' });
const contextData = {
name: contextClient.contextPath('[PROJECT]', '[SESSION]', '[YOUR-CONTEXT]'),
parameters: struct.encode({ name: 'John' }),
lifespanCount: 1
};
const context = await contextClient.createContext({
parent: sessionPath,
context: contextData
});
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode
}
},
queryParams: {
contexts: context // You may want to add the other contexts here
}
};
const result = await sessionClient.detectIntent(request);
console.log(result);
}
And now you will need to create a parameter, which value is: #your-context.name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With