-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogflow.js
41 lines (31 loc) · 1.01 KB
/
dialogflow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { SessionsClient } = require('@google-cloud/dialogflow');
const path = require('path');
const keyFilePath = path.join(__dirname, '../markerdaollmagent.json');
const sessionClient = new SessionsClient({
keyFilename: keyFilePath,
});
const projectId = 'markerdaollmagent-aipc';
async function detectIntent(message, sessionId) {
try {
const sessionPath = sessionClient.projectAgentSessionPath(projectId, sessionId);
const params = {
session: sessionPath,
queryInput: {
text: {
text: message,
languageCode: 'en',
},
},
};
const responses = await sessionClient.detectIntent(params);
const result = responses[0]?.queryResult;
if (!result || !result.fulfillmentText) {
throw new Error('No response from Dialogflow');
}
return result.fulfillmentText;
} catch (error) {
console.error('Error in DialogflowService:', error);
throw new Error('Failed to process message in Dialogflow');
}
}
module.exports = { detectIntent };