Last active
August 18, 2022 09:40
-
-
Save savelee/c3ade80883ae042bcd54d3c3c0185e22 to your computer and use it in GitHub Desktop.
Example Dialogflow implementation, branching on parameters and returning text strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config() //load environemnt vars | |
const projectId = process.env.GCLOUD_PROJECT; //your project name | |
const uuidv1 = require('uuid/v1'); | |
const sessionId = uuidv1(); | |
const languageCode = 'en-US'; | |
const server = require('http').createServer((request, response) => { | |
response.writeHead(200, {"Content-Type": "text/html"}); | |
response.write(`Chat Server Running`); | |
response.end(); | |
}); | |
const io = require('socket.io')(server); | |
io.set('origins', '*:*'); | |
const structjson = require('./structjson'); | |
const dialogflow = require('dialogflow'); | |
const sessionClient = new dialogflow.SessionsClient(); | |
const sessionPath = sessionClient.sessionPath(projectId, sessionId); | |
function detectIntent(request, cb){ | |
// Send request and log result | |
sessionClient.detectIntent(request) | |
.then(responses => { | |
var result = responses[0].queryResult; | |
if(result){ | |
var dialogs = result.fulfillmentMessages; | |
for (var i = 0, len = dialogs.length; i < len; i++) { | |
var messageType = dialogs[i].message; //text || payload | |
var answer = dialogs[i]["text"].text; | |
} | |
// set the action in Dialogflow to Mortgage | |
if(result.action && result.action == 'mortgage') { | |
// imagine the string literals are retrieved from a database | |
var dataMap_en = new Map(); | |
dataMap_en.set('H001', "Buying a house."); | |
dataMap_en.set('H002', "Buying a villa."); | |
dataMap_en.set('H003', "Buying a apartment."); | |
dataMap_en.set('H004', "Buying a bungalow. Is this your second mortgage?"); | |
dataMap_en.set('Fallback', "That's not an option. You can choose between a house, villa, apartment or bungalow."); | |
var dataMap_nl = new Map(); | |
dataMap_nl.set('H001', "Een huis kopen."); | |
dataMap_nl.set('H002', "Een villa kopen."); | |
dataMap_nl.set('H003', "Een appartement kopen."); | |
dataMap_nl.set('H004', "Een recreatie woning kopen. Is dit uw tweede hypotheek?"); | |
dataMap_nl.set('Fallback', "Dit is helaas geen optie. U kunt een huis, villa, appartement of bungalow kiezen."); | |
var dataMap = {}; | |
dataMap['en-us'] = dataMap_en; | |
dataMap['nl-nl'] = dataMap_nl; | |
// @housing_type entity in Dialogflow | |
var param = p['housing_type']; | |
// language code from Dialogflow | |
var lang = result.languageCode; | |
switch( param ){ | |
case 'H004': | |
answer = dataMap[lang].get('H004'); | |
// FOLLOW UP QUESTIONS | |
// Just start a new intent with training phrases | |
// Or fire an event to move to the next intent | |
break; | |
default: | |
if(param) { | |
answer = dataMap[lang].get(param); | |
} else { | |
answer = dataMap[lang].get('Fallback'); | |
} | |
} | |
console.log(answer); | |
} | |
if(answer){ | |
//console.log(result.intent.intentDetectionConfidence); | |
cb(answer, result.intent.intentDetectionConfidence, "ChatBot"); | |
console.log(answer[0]); | |
} | |
} | |
}) | |
.catch(err => { | |
console.error('ERROR:', err); | |
io.emit('systemerror', { | |
username: "ChatBot", | |
message: err | |
}); | |
}); | |
} | |
io.on('connection', function(client){ | |
// when the client emits 'msg', this listens and executes | |
client.on('msg', function(data){ | |
// The text query request. | |
const request = { | |
session: sessionPath, | |
queryInput: { | |
text: { | |
text: data, | |
languageCode: languageCode, | |
}, | |
}, | |
}; | |
detectIntent(request, function(botAnswer, confidence, botName){ | |
// we tell the client to execute 'agentmsg' | |
io.emit('agentmsg', { | |
username: botName, | |
message: botAnswer, | |
confidence: confidence, | |
session: client.id | |
}); | |
}); | |
}); | |
client.on('disconnect', function(){ | |
console.log("Close Connection"); | |
}); | |
}); | |
server.listen(3000); //TODO | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment