NodeJs
Javascript
Visual Studio Code
ejs
create new Agent
https://dialogflow.cloud.google.com/
create new Service Account https://console.cloud.google.com/iam-admin/
set GOOGLE_APPLICATION_CREDENTIALS=/Users/suhee/Documents/pugshop_secret/storelink-bot-house-e8a921586acc.json
{
"name": "bot-house",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"@google-cloud/dialogflow": "^3.5.0",
"async": "^3.2.0",
"axios": "^0.21.1",
"cookie-parser": "^1.4.5",
"debug": "~2.6.9",
"ejs": "^3.1.6",
"express": "~4.16.1",
"express-session": "^1.17.1",
"firebase": "^8.5.0",
"firebase-admin": "^9.7.0",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"mysql2": "^2.2.5",
"uuid": "^8.3.2"
},
"devDependencies": {
"swagger-jsdoc": "^6.0.0",
"swagger-ui-express": "^4.1.6"
}
}
/**
* TODO(developer): UPDATE these variables before running the sample.
*/
// projectId: ID of the GCP project where Dialogflow agent is deployed
// const projectId = 'PROJECT_ID';
// sessionId: String representing a random number or hashed user identifier
// const sessionId = '123456';
// queries: A set of sequential queries to be send to Dialogflow agent for Intent Detection
// languageCode: Indicates the language Dialogflow agent should use to detect intents
const languageCode = 'ko-KR';
var express = require('express');
var router = express.Router();
var async = require('async');
// Imports the Dialogflow library
const dialogflow = require('@google-cloud/dialogflow');
const uuid = require('uuid');
/**
* @swagger
* paths:
* /df/event:
* post:
* tags:
* - "DialogFlow"
* summary: "google dialogflow process (event)"
* description: "google ai "
* consumes:
* - "application/json"
* produces:
* - "application/json"
* parameters:
* - in: "body"
* name: "body"
* description: "구글 다이얼로그 플로우 로직"
* required: true
* schema:
* $ref: "#/definitions/dialogflow"
*/
/* POST dialogFlow */
router.post('/evnet', async function(req, res, next) {
var projectId = "storelink-bot-house";
var sessionId = uuid.v4();
var queries = req.body.question;
console.log("===queries==" +queries);
const sessionClient = new dialogflow.sessionClient
const sessionPath = sessionClient.projectAgentSessionPath(
projectId,
sessionId
);
const request = {
session: sessionPath,
queryInput: {
event: {
name: req.body.evnet,
languageCode: languageCode,
},
},
};
const response = await dialogflow.sessionsClient.detectIntent(request);
console.log('Detected intent');
const result = responses[0].queryResult;
console.log(` Query: ${result.queryText}`);
console.log(` response: ${result.fulfillmentText}`);
res.send(result);
});
/**
* @swagger
* paths:
* /df:
* post:
* tags:
* - "DialogFlow"
* summary: "google dialogflow process (query)"
* description: "google ai "
* consumes:
* - "application/json"
* produces:
* - "application/json"
* parameters:
* - in: "body"
* name: "body"
* description: "구글 다이얼로그 플로우 로직"
* required: true
* schema:
* $ref: "#/definitions/dialogflow"
*/
/* POST dialogFlow */
router.post('/', async function(req, res, next) {
var projectId = "storelink-bot-house";
var sessionId = uuid.v4();
var queries = req.body.question;
console.log("===queries==" +queries);
// Create a new session
const sessionClient = new dialogflow.SessionsClient();
// const sessionPath = sessionClient.projectAgentSessionPath(projectId, sessionId);
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,
},
queryParams: {
timeZone: "Asia/Seoul",
sentimentAnalysisRequestConfig: {
analyzeQueryTextSentiment: true
}
}
},
};
if (contexts && contexts.length > 0) {
request.queryParams = {
contexts: contexts,
};
}
const responses = await sessionClient.detectIntent(request);
return responses[0];
}
async function executeQueries(projectId, sessionId, queries, languageCode) {
// Keeping the context across queries let's us simulate an ongoing conversation with the bot
let context;
let intentResponse;
// for (const query of queries) {
try {
console.log(`Sending Query: ${queries}`);
intentResponse = await detectIntent(
projectId,
sessionId,
queries,
context,
languageCode
);
console.log('Detected intent');
console.log(
`Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}`
);
try {
const obj = intentResponse.queryResult.fulfillmentMessages[0].simpleResponses.simpleResponses[0].textToSpeech;
const json = JSON.stringify(obj).replace(/\\\\/gi, );
res.json([{
resultCode: '0000',
resultMsg: 'ok',
data: json? json: `${intentResponse.queryResult.fulfillmentText}`
}]);
} catch {
res.json([{
resultCode: '0000',
resultMsg: 'ok',
data: `${intentResponse.queryResult.fulfillmentText}`
}]);
}
// Use the context from this response for next queries
context = intentResponse.queryResult.outputContexts;
return context
} catch (error) {
console.log(error);
}
// }
}
executeQueries(projectId, sessionId, queries, languageCode);
});
module.exports = router;