// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require( 'firebase-functions' );
const mysql = require( 'mysql' );
const {WebhookClient} = require( 'dialogflow-fulfillment' );
const {Text, Card, Image, Suggestion, Payload} = require( 'dialogflow-fulfillment' );
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
const mysiteurl = 'https://en.wikipedia.org/wiki/Narendra_Modi';
exports.dialogflowFirebaseFulfillment = functions.https.onRequest( ( request, response ) =>{
const agent = new WebhookClient( {request, response} );
console.log( 'Dialogflow Request headers: ' + JSON.stringify( request.headers ) );
console.log( 'Dialogflow Request body: ' + JSON.stringify( request.body ) );
let action = request.body.result['action'];
function welcome( agent ){
agent.add( `Welcome to infohub personal assistant, my name is Isobel` );
agent.add( new Card( {
title: `mysite`,
imageUrl: mysiteurl,
text: `Did you know already mysite if not visit it now! `,
buttonText: 'mysite',
buttonUrl: mysiteurl
} )
);
agent.add( `I can help you get information already contained in mysite` );
agent.add( new Suggestion( `population` ) );
agent.add( new Suggestion( `avgincome` ) );
agent.add( new Suggestion( `thisyeargdp` ) );
}
//Call the callDBJokes method
function navigationdistance( agent ){
// Get parameters from Dialogflow to convert
const from = agent.parameters.from;
const to = agent.parameters.to;
console.log( `User requested to get info on ${to} in ${from}` );
if (action === 'get.data') {
// Call the callDBJokes method
callDB().then((output) => {
// Return the results of the weather API to API.AI
response.setHeader('Content-Type', 'application/json');
response.send(JSON.stringify(output));
}).catch((error) => {
// If there is an error let the user know
response.setHeader('Content-Type', 'application/json');
response.send(JSON.stringify(error));
});
}
// Sent the context to store the parameter information
// and make sure the followup
agent.setContext( {
name: 'navigationdistance',
lifespan: 3,
parameters: {from: from, to: to}
} );
// Compile and send response
agent.add( ` ${to} in ${from} ` );
agent.add( `Would you like to know something else?` );
agent.add( new Suggestion( `population` ) );
agent.add( new Suggestion( `avgincome` ) );
agent.add( new Suggestion( `thisyeargdp` ) );
}
function fallback( agent ){
agent.add( `I didnt get that, can you try again?` );
}
function callDB( to, from ){
return new Promise( ( resolve, reject ) =>{
try{
var connection = mysql.createConnection( {
host: "localhost",
user: "root",
password: "",
database: "test"
} );
var sql = "INSERT INTO mocktable (from, to) VALUES ('$from', '$to')";
connection.query( sql, function( error, results, fields ){
if( !error ){
let response = "The solution is: " + results[0];
response = response.toString();
let output = {'speech': response, 'displayText': response};
console.log( output );
resolve( output );
} else{
let output = {
'speech': 'Error. Query Failed.',
'displayText': 'Error. Query Failed.'
};
console.log( output );
reject( output );
}
} );
connection.end();
} catch
( err ){
let output = {
'speech': 'try-cacth block error',
'displayText': 'try-cacth block error'
};
console.log( output );
reject( output );
}
});
}
let intentMap = new Map(); // Map functions to Dialogflow intent names
intentMap.set( 'Default Welcome Intent', welcome );
intentMap.set( 'get info about mycountry', navigationdistance );
intentMap.set( 'Default Fallback Intent', fallback );
agent.handleRequest( intentMap );
})
;
这是我用来将dialogflow连接到数据库的代码。我在dialogflow中使用了内联编辑器,并对其进行了修改以将其连接到数据库。我无法连接到数据库。还有其他方法可以将dialogflow连接到数据库。
TIA,
答案 0 :(得分:0)
如果我是我,我只会使用Firestore数据库。免费,而且还是Google产品! https://firebase.google.com/docs/firestore/quickstart
答案 1 :(得分:0)
您似乎正在本地计算机上运行MySQL数据库,并尝试通过主机名“ localhost”访问它。使用Dialogflow内置代码编辑器执行此操作时会遇到一些问题:
您可以通过几种方法解决此问题: