自定义动作Bot框架

时间:2018-10-17 06:41:33

标签: node.js botframework

enter image description here我已经使用Bot Framework使用node.js开发了一个聊天机器人。我使用了自定义操作和缩略图卡来创建按钮,并根据正则表达式匹配它会调用其他函数。无论何时用户键入yes / No,Bot都必须转到上一个对话框的操作。例如。我正在使用两个对话框/ main和/ final,具有两个yes / no自定义操作(Onselect和imback卡),如果我打开/ main,它将为/ main打开yes / no自定义操作对话框,它应该执行以下操作/主要。在我的情况下,由于两个具有相同按钮名称和相同Imback消息的自定义操作而没有发生。我将regex用作/ 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();       
    }
}

1 个答案:

答案 0 :(得分:0)

如果在每个对话框中都设置了 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');
        }
    }
}
相关问题