Skip to content

Instantly share code, notes, and snippets.

@prrraveen
Created February 5, 2021 17:38
Show Gist options
  • Save prrraveen/d7af79301581eecc0476f2eb7eb4b111 to your computer and use it in GitHub Desktop.
Save prrraveen/d7af79301581eecc0476f2eb7eb4b111 to your computer and use it in GitHub Desktop.
process.env.GOOGLE_APPLICATION_CREDENTIALS = "./auth.json";
const dialogflow = require('@google-cloud/dialogflow');
const projectId = "caloriestracking-ebxh"
const sessionId = "123453232121"
// Instantiates a session client
const sessionClient = new dialogflow.SessionsClient();
async function detectIntent(
projectId,
sessionId,
query,
contexts,
languageCode
) {
// The path to identify the agent that owns the created intent.
const sessionPath = sessionClient.projectAgentSessionPath(
projectId,
sessionId
);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
if (contexts && contexts.length > 0) {
request.queryParams = {
contexts: contexts,
};
}
const responses = await sessionClient.detectIntent(request);
return responses[0];
}
async function executeQueries(projectId, sessionId, query, languageCode) {
// Keeping the context across queries let's us simulate an ongoing conversation with the bot
}
// exports.handler = async (event) => {
const handler = async (event) => {
const query = "hello"
const languageCode = "en-US"
let context;
let intentResponse;
try {
console.log(`Sending Query: ${query}`);
intentResponse = await detectIntent(
projectId,
sessionId,
query,
context,
languageCode
);
console.log('Detected intent');
console.log(
`Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}`
);
return {
statusCode: 200,
body: JSON.stringify(`Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}`)
};
// Use the context from this response for next queries
context = intentResponse.queryResult.outputContexts;
return intentResponse
} catch (error) {
console.log(error);
return {
statusCode: 200,
body: JSON.stringify(`Something went wrong`)
};
}
// const response = {
// statusCode: 200,
// body: JSON.stringify(`Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}`)
// };
// return response;
};
handler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment