Dialogflow检测意图实现



Hi我创建了一个dialogflow nodejs后端,它使用nodejs的客户端库检测意图。

const sessionPath = this.sessionClient.sessionPath(this.configService.get('dialogFlowProjectId'), sessionId);
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: "en-US"
}
}
};
// Send request and log result
Logger.log(request);
const responses = await this.sessionClient.detectIntent(request);

这很好用,但我也想触发某些意图的实现。

我已经设置了一个webhook url-当你在dialogflow控制台中使用聊天时,这很好。但是,当我使用我创建的方法向dialogflow发送请求时,webhook不会被调用,而是进入回退意图。我通过对话流控制台聊天和我自己的API调用了相同的意图。

当我使用客户端库API时,如何触发webhook调用?

首先,请记住,无论是通过测试控制台还是通过API,都不会"调用Intent"。可以发送应该与Intent匹配的查询文本。

如果您设置了Intent,使其调用Fulfillment网络挂钩,那么只要Intent本身被触发,无论来源如何,都应该发生这种情况。

相反,Fallback Intent被触发表明查询文本的某些内容与您认为应该匹配的Intent不匹配。我会进行检查,以确保您在query中发送的文本应与有问题的Intent的训练短语匹配,并且您没有输入上下文,并且您的代理已设置为"en-US"语言代码。

要通过webhook路由多个意图,您需要一个包含express路由器的handler.js文件…

const def = require('./intents/default')
const compression = require('compression')
const serverless = require('serverless-http')
const bodyParser = require('body-parser')
const { WebhookClient } = require('dialogflow-fulfillment')
const express = require('express')
const app = express()
// register middleware
app.use(bodyParser.json({ strict: false }))
app.use(function (err, req, res, next) {
res.status(500)
res.send(err)
})
app.use(compression())
app.post('/', async function (req, res) {
// Instantiate agent
const agent = new WebhookClient({ request: req, response: res })
const intentMap = new Map()
intentMap.set('Default Welcome Intent', def.defaultWelcome)
await agent.handleRequest(intentMap)
})
module.exports.server = serverless(app)

正如您所看到的,这个handler.js使用的是无服务器http和express。

你的intent.js函数可能看起来像这样。。。

module.exports.defaultWelcome = async function DefaultWelcome (agent) {
const responses = [
'Hi! How are you doing?',
'Hello! How can I help you?',
'Good day! What can I do for you today?',
'Hello, welcoming to the Dining Bot. Would you like to know what's being served? Example: 'What's for lunch at cronkhite?'',
'Greetings from Dining Bot, what can I do for you?(Hint, you can ask things like: 'What's for breakfast at Annenberg etc..' or 'What time is breakfast at Anennberg?')',
'greetings',
'hey',
'long time no see',
'hello',
"lovely day isn't it"
]
const index = Math.floor((Math.random() * responses.length) + 1)
console.log(responses[index].text)
agent.add(responses[index].text)
}

这为您提供了通过webhook路由多个请求的基本功能。希望这能有所帮助。

最新更新