如何将对话流结果集成到LineBot(Node.js)?



我正在使用node.js创建聊天机器人(Line(集成Google Dialogflow。

我只能从用户输入文本和回显一些文本来创建线路聊天机器人。

我可以从用户输入发送命令到谷歌对话流创建代码,并使用对话流 给用户的 NLU 技术响应文本。

但是我需要用户输入发送到对话流的文本并响应文本(A(,然后将文本(A((在代码添加一些模板按钮的代码后(发送到行机器人创建向用户显示一些模板按钮。

如何集成两部分代码实现用户输入文本和通过对话流结果,使用结果发送到线路机器人服务器?

用户输入 -> 对话流 ->mycode(添加一些模板按钮调用行( ->linbot ->bot 向用户显示模板按钮

谢谢。

//----------------------------------

我的对话流代码:

var express = require('express');
var bodyParser = require('body-parser'); 
var app = express();
app.use(
bodyParser.urlencoded({
extended: true
})
)
app.use(
bodyParser.json()
)

app.post("/webhook", function(req,res){
console.log(JSON.stringify(req.body, null, 2))
var intent = req.body.queryResult.intent.displayName;
var entityCity =  req.body.queryResult.parameters["geo-city"];
if(intent === 'myIntent')
{
//  here I need call bot.on method, but I don't known how to do.
// return res.json({
fulfillmentText: `I known your mean.`
});
}
else
{
return res.json({
fulfillmentText: `i am not sure your mean`
});
}
})
app.listen(process.env.PORT || 5566, function(){
console.log('server start ...');
})

//----------------------------------

我的线路聊天机器人代码:

var linebot = require('linebot');
var express = require('express');
var app = express();
const bot = linebot({
channelId: 'mychannelId',
channelSecret: 'mychannelSecret',
channelAccessToken: 'mychannelAccessToken'
});
bot.on('message',function(event) {
console.log('bot');
console.log(event); 
var msg = event.message.text;
// here can add some template button code and reply to user.
});
const linebotParser = bot.parser();
app.post('/webhook', linebotParser);
var server = app.listen(process.env.PORT || 8080, function() {
var port = server.address().port;
});

//--------------------

我的线路聊天机器人代码其他版本:

const line = require('@line/bot-sdk');
const express = require('express');
const lineConfig = {
channelAccessToken: process.env.HEROKU_LINE_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.HEROKU_LINE_CHANNEL_SECRET
};
const client = new line.Client(lineConfig);
const app = express();
app.post('/webhook', line.middleware(lineConfig), function(req, res) {
Promise
.all(req.body.events.map(handleEvent))
.then(function(result) {
res.json(result);
});
});
function handleEvent(event) {
switch (event.type) {
case 'join':
case 'follow':
return client.replyMessage(event.replyToken, {
type: 'text',
text: 'hello~'
});   
case 'message':
switch (event.message.type) {
case 'text':
return client.replyMessage(event.replyToken, {
type: 'text',
text: (event.message.text+'~yu')
});
}
}
}
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
const line = require('@line/bot-sdk');
const express = require('express');
const dialogflow = require('dialogflow');
const uuid = require('uuid');
const lineConfig = {
channelAccessToken: process.env.HEROKU_LINE_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.HEROKU_LINE_CHANNEL_SECRET
};
const client = new line.Client(lineConfig);
const app = express();
app.post('/webhook', line.middleware(lineConfig), function(req, res) {
Promise
.all(req.body.events.map(handleEvent))
.then(function(result) {
res.json(result);
});
});
async function handleEvent(event) {
switch (event.type) {
case 'join':
case 'follow':
return client.replyMessage(event.replyToken, {
type: 'text',
text: 'hello~'
});   
case 'message':
switch (event.message.type) {
case 'text':
const response = await queryDF(event.message.text)
// you will get response from DF here
return client.replyMessage(event.replyToken, {
type: 'text',
text: (event.message.text+'~yu')
});
}
}
}

async function queryDF(message, projectId = 'your-project-id') {
// A unique identifier for the given session
const sessionId = uuid.v4();
// Create a new session
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: message,
// The language used by the client (en-US)
languageCode: 'en-US',
},
},
};
// Send request and log result
const responses = await sessionClient.detectIntent(request);
return responses[0].queryResult;
}
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});

你需要用户对话流npm检测意图方法

最新更新