node.js - 自定义操作 Bot 框架
问题描述
我使用 node.js 开发了一个带有 Bot Framework 的聊天机器人。我已经使用自定义操作和缩略图卡来创建按钮,并根据正则表达式匹配它会调用其他函数。每当用户输入 yes/No 时,Bot 必须转到上一个对话框的操作。例如。我正在使用两个对话框 /main 和 /final,带有两个是/否自定义操作(Onselect 和 imback 卡),如果我打开 /main,它将打开 /main 的是/否自定义操作对话框,它应该执行以下操作/主要的。在我的情况下,它没有发生,因为两个自定义操作具有相同的按钮名称和相同的 Imback 消息。我将正则表达式用作 /yes|y/gi &/no|n/gi 用于自定义操作。
bot.dialog('/',function (session, args, results) {
if (!session.userData.name)
{
console.log('Start conversation');
name = session.message.text;
session.userData.name = name;
//session.send("Hello ,"+userName +"\nI can help you with the following")
session.send("I can help you with the following")
session.beginDialog('/main');
}
else if(!session.userData.conformation)
{
console.log('execute command');
dialog_flow(session);
}
dialog_flow(session);
}
);
bot.dialog('/main',function (session, args, results) {
const card = new builder.ThumbnailCard(session)
.title('Bot for Global Service Desk')
.text(' You can choose one of the options below')
.buttons([
builder.CardAction.imBack(session, 'Network Support','Network Support')
]);
const message = new builder.Message(session)
.addAttachment(card)
session.send(message);
option = session.message.text;
session.userData.option = option;
}
);
bot.customAction({ matches:/Network/gi,
onSelectAction: (session, args, next) => {
const card = new builder.ThumbnailCard(session)
.text("You have Selected Network Support")
.buttons([
builder.CardAction.imBack(session,'Internet Connectivity Check','Internet Connectivity Check')
]);
const message = new builder.Message(session)
.addAttachment(card);
session.send(message);
option = session.message.text
session.userData.option = option }}
);
bot.customAction({
matches:/Internet Connectivity Check/gi,
onSelectAction: (session, args, next) => {
option = session.message.text
itsmdata["subcategory"] = option
session.userData.option1 = option
session.send("You have Selected\t"+option+",\tPlease confirm?");
const card = new builder.ThumbnailCard(session)
.buttons([
builder.CardAction.imBack(session, 'YES', 'YES'),
builder.CardAction.imBack(session, 'NO', 'NO')
]);
const message = new builder.Message(session)
.addAttachment(card);
session.endConversation(message);
}
}
);
bot.dialog('/final',function (session, args, results) {
const card = new builder.ThumbnailCard(session)
.text('Do you want to Continue')
.buttons([
builder.CardAction.imBack(session, 'YES', 'YES'),
builder.CardAction.imBack(session, 'NO', 'NO')
]);
const message = new builder.Message(session)
.addAttachment(card);
session.endConversation(message);
}
);
function dialog_flow(session){
//session.sendTyping();
conformation = session.message.text
option = session.userData.option1
session.userData.conformation = conformation
console.log('User Selected Option is %s', option);
console.log('User conformation %', conformation);
var yesconfor = /Yes|Y|ok/gi;
var noconfor = /cancel|no|n/gi;
if(conformation.match(yesconfor)){
session.clearDialogStack();
session.beginDialog('/main');
}
else if(conformation.match(noconfor)){
session.clearDialogStack();
session.send("Thank you,\t Please Close the session");
session.endDialog();
}
else if(conformation.match(noconfor)){
session.clearDialogStack();
session.beginDialog('/main');
}
else if(conformation.match(yesconfor))
{
session.sendTyping();
postdata(itsmdata, "snow", session);
session.sendTyping();
}
}
解决方案
如果在每个对话框中设置了session.userData.option ,则dialog_flow方法中的是/否检查应检查该值以确定用户响应的是哪个是/否答案。像这样的东西:
var builder = require('botbuilder');
var restify = require('restify');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot and listen to messages
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
var inMemoryStorage = new builder.MemoryBotStorage();
var bot = new builder.UniversalBot(connector)
.set('storage', inMemoryStorage); // Register in memory storage
bot.dialog('/', function (session, args, results) {
if (!session.userData.name) {
console.log('Start conversation');
name = session.message.text;
session.userData.name = name;
//session.send("Hello ,"+userName +"\nI can help you with the following")
session.send("I can help you with the following")
session.beginDialog('/main');
}
else if (!session.userData.conformation) {
console.log('execute command');
dialog_flow(session);
}
dialog_flow(session);
}
);
bot.dialog('/main', function (session, args, results) {
const card = new builder.ThumbnailCard(session)
.title('Bot for Global Service Desk')
.text(' You can choose one of the options below')
.buttons([
builder.CardAction.imBack(session, 'Network Support', 'Network Support')
]);
const message = new builder.Message(session)
.addAttachment(card)
session.send(message);
option = session.message.text;
session.userData.option = option;
}
);
bot.customAction({
matches: /Network/gi,
onSelectAction: (session, args, next) => {
const card = new builder.ThumbnailCard(session)
.text("You have Selected Network Support")
.buttons([
builder.CardAction.imBack(session, 'Internet Connectivity Check', 'Internet Connectivity Check')
]);
const message = new builder.Message(session)
.addAttachment(card);
session.send(message);
option = session.message.text
session.userData.option = option
}
}
);
bot.customAction({
matches: /Internet Connectivity Check/gi,
onSelectAction: (session, args, next) => {
option = session.message.text
//itsmdata["subcategory"] = option
session.userData.option1 = option
session.send("You have Selected\t" + option + ",\tPlease confirm?");
const card = new builder.ThumbnailCard(session)
.buttons([
builder.CardAction.imBack(session, 'YES', 'YES'),
builder.CardAction.imBack(session, 'NO', 'NO')
]);
const message = new builder.Message(session)
.addAttachment(card);
session.endConversation(message);
}
}
);
bot.dialog('/final', function (session, args, results) {
const card = new builder.ThumbnailCard(session)
.text('Do you want to Continue')
.buttons([
builder.CardAction.imBack(session, 'YES', 'YES'),
builder.CardAction.imBack(session, 'NO', 'NO')
]);
const message = new builder.Message(session)
.addAttachment(card);
session.endConversation(message);
}
);
function dialog_flow(session) {
//session.sendTyping();
conformation = session.message.text
option = session.userData.option1;
session.userData.conformation = conformation;
console.log('User Selected Option is %s', option);
console.log('User conformation %', conformation);
var yesconfor = /Yes|Y|ok/gi;
var noconfor = /cancel|no|n/gi;
if (conformation.match(yesconfor)) {
if(option == 'Internet Connectivity Check'){
session.clearDialogStack();
session.beginDialog('/main');
}else{
postdata(itsmdata, "snow", session);
session.sendTyping();
}
}
else if (conformation.match(noconfor)) {
if(option == 'Internet Connectivity Check'){
session.clearDialogStack();
session.send("Thank you,\t Please Close the session");
session.endDialog();
}
else{
session.clearDialogStack();
session.beginDialog('/main');
}
}
}
推荐阅读
- c# - TextBlock 未使用 ViewModel 的属性进行更新
- node.js - Winston Logger 中的冗余
- c# - C# 使文件只读
- javascript - 从每个数组的对象制作 pdf 文件
- windows - 如何向使用 sessionhost 的用户发送 RdsUserSessionMessage?
- electron - 从 NAS 运行时,Electron 应用程序变慢
- java - java - 如何在Java的XWPFDocument中添加新行?
- json - 快速不规则 json 解析
- c# - 将 JToken 添加到 JObject 中的特定 JsonPath
- javascript - 订阅后如何获取用户信息?