DialogFlow:如何处理具有多个路由的NodeJS服务器?
我在DialogFlow和NodeJS中创建了一个项目,希望在其中使用WebHook调用我的实现。 在我的NodeJS服务器中,我有多个用于不同功能/意图的路由。例如,/getWeather调用一个天气API来返回关于特定城市天气的响应。或者/getMovie调用API返回电影信息。
DialogFlow只允许一个WebHook API,所以我的问题是,我如何调用一个泛型API"/",它可以处理所有不同的路由并在需要时调用正确的路由?
我可以使用DialogFlow上的内联编辑器以正确的路由调用每个API;但是,我希望使用单个Web挂钩,而不是使用FireBase函数来调用正确的意图。
在使用通用路由处理多个路由的情况下,我似乎在网上找不到这样的示例。Image of my Code Stack
index.js:
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3000;
const server = http.createServer(app);
server.listen(port);
server.post
app.js
const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose= require('mongoose');
const issuesRoutes = require('./API/Routes/issues');
const movieRoute = require('./API/Routes/getmovie');
const resolvedtaskroute = require('./API/Routes/resolvedtask');
const newtaskRoute = require('./API/Routes/newtask');
mongoose.connect('link', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('MongoDB connected...'))
.catch(err => console.log(err));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use((req, res, next) => {
res.header('Acces-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', '*');
if (req.method === 'OPTIONS'){
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({});
}
next();
});
//routes to handle requests
app.use('/issues', issuesRoutes);
app.use('/newtask', newtaskRoute);
app.use('/resolvedtask', resolvedtaskroute);
app.use('/getmovie', movieRoute);
//error handling
app.use((req, res, next) => {
const error = new Error('Not Found');
error.status = 404;
next(error);
})
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
})
})
module.exports = app;
我的其中一个路由示例:getMovie.js
const express = require('express');
const router = express.Router();
const http = require('http');
router.post('/', (req, res, next) => {
const movieToSearch = req.body.queryResult.parameters.movie;
const API_KEY = 'XXXXX';
const reqUrl = `http://www.omdbapi.com/?t=${movieToSearch}&apikey=${API_KEY}`
http.get(
reqUrl,
responseFromAPI => {
let completeResponse = ''
responseFromAPI.on('data', chunk => {
completeResponse += chunk
})
responseFromAPI.on('end', () => {
const movie = JSON.parse(completeResponse)
let dataToSend = movieToSearch
dataToSend = `${movie.Title} was released in the year ${movie.Year}. It is directed by ${
movie.Director
} and stars ${movie.Actors}.
}`
return res.json({
fulfillmentText: dataToSend,
source: 'getmovie'
})
})
},
error => {
return res.json({
fulfillmentText: 'Could not get results at this time',
source: 'getmovie'
})
}
)
})
module.exports = router;
Dialogflow
很明显,推荐答案允许一个WebHookPOST URL,其中每个意图调用都是在这里进行的。如果您想在内部使用不同的API服务,那么您应该定义一个WebHook,并且在WebHook内部只使用intentMAP调用与意图相关的函数。在每个函数上,调用外部API并将响应返回到Dialogflow。我将使用DIALOGFLOW-FULFULATION对其进行详细描述。
您首先需要一个用于处理对话流请求和响应的WebHook POST路由,在它内部,您需要将意图映射到它的特定功能,如下所示:
const { WebhookClient } = require("dialogflow-fulfillment");
const movieService= require("your function for movie API");
router.post("/", async (req, res, next) => {
const agent = new WebhookClient({ request: req, response: res });
const movie = new movieService(agent);
let intentMap = new Map();
intentMap.set("Movie Intent", () => {
//make an api call inside this function
return movie.getinfo();
});
if (agent.intent) {
agent.handleRequest(intentMap);
}
});
现在为外部API调用创建另一个文件,如下所示
async getMovie(){
// get all required paramters from dialogflow here and call APIS and return back response using
agent.add("The info about movie is");
}
相关文章