如何使用节点在MS bot中动态创建流程

时间:2018-01-18 05:37:16

标签: node.js nodes botframework

我想像第一张图片一样在MS bot中创建 N no.of流。我添加了流程图(第二张图片)。

流量为:当用户从A,B,C& A按A时D,它将显示A1,A2,A3和amp; A4,当用户按下A1时,它将显示A11,A12,A13& A14当用户按下A12时,它将显示A121,A122,A123& A124同样是连续流动的。

enter image description here

以下是完整的流程 enter image description here

我使用以下代码创建了此流程。但最终的代码有超过2000行。所有都是重复功能。所以,我想用最少的代码来实现它。有什么想法吗?

而不是A,B,CI需要使用发行,请求,返回,访问,房间类型,预订引擎,最佳弹性费率,虚拟费率代码,库存盘点等。为了理解目的,我使用A,B,下进行。

而不是A,B,C将这些输入用于关系

['PvtBank',GovtBank'] 

PvtBank=>['TBM','CUB','KVB'], 
GovtBank=>['IOB','CBI','BOB'], 
TBM => ['OUTSIDE INDIA','INSIDE INDIA'], 
INSIDE INDIA => ['DELHI','MUMBAI','PUNE'],
OUTSIDE INDIA => ['US','UK','CHINA'], 
DELHI => ['INDIA GATE','NEW DELHI'], 
US=> ['NEW YORK','LOS ANGELES'] and etc

bot.dialog('mainFlow', [
    function (session, results, next) {
        builder.Prompts.choice(session, "Whould you like me to taks about?", "A|B|C|D", { listStyle: builder.ListStyle.button });
    },
    function (session, results, next) {
        session.userData.TravelType = results.response;
        if (results.response.entity === 'A') {
            session.beginDialog('flowA');
        } else if (results.response.entity === 'B') {
            session.beginDialog('flowB');
        } else if (results.response.entity === 'C') {
            session.beginDialog('flowC');
        } else if (results.response.entity === 'D') {
            session.beginDialog('flowD');
        }
    }
]).endConversationAction("stop",
    "",
    {
        matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
        // confirmPrompt: "This will cancel your order. Are you sure?"
    }
);

bot.dialog('flowA', [
    function (session, results, next) {
        builder.Prompts.choice(session, "Whould you like me to taks about?", "A1|A2|A3|A4", { listStyle: builder.ListStyle.button });
    },
    function (session, results, next) {
        session.userData.TravelType = results.response;
        if (results.response.entity === 'A1') {
            session.beginDialog('flowA1');
        } else if (results.response.entity === 'A2') {
            session.beginDialog('flowA2');
        } else if (results.response.entity === 'A3') {
            session.beginDialog('flowA3');
        } else if (results.response.entity === 'A4') {
            session.beginDialog('flowA4');
        }
    }
]).endConversationAction("stop",
    "",
    {
        matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
        // confirmPrompt: "This will cancel your order. Are you sure?"
    }
);

1 个答案:

答案 0 :(得分:1)

请参阅以下代码段:

bot.dialog('mainFlow', [(session, args, next) => {
    let currentChoice = session.conversationData.TravelType;
    let promits = currentChoice ? [`${currentChoice}1`, `${currentChoice}2`, `${currentChoice}3`, `${currentChoice}4`].join(`|`) : `A|B|C|D`;
    builder.Prompts.choice(session, "Whould you like me to taks about?", promits, {
        listStyle: builder.ListStyle.button
    });
}, (session, args, next) => {
    session.conversationData.TravelType = args.response.entity;
    session.replaceDialog('mainFlow');
}]).endConversationAction("stop",
    "", {
        matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
        // confirmPrompt: "This will cancel your order. Are you sure?"
    }
);

更新

  

好。把这个输入用于关系而不是A,B,C ['PvtBank',GovtBank'] PvtBank => ['TBM','CUB','KVB'],GovtBank => ['IOB','CBI' ,'BOB'],TBM => ['外部印度','内部印度'],内部印度=> ['DELHI','MUMBAI','PUNE'],外部印度=> ['US','UK','CHINA'],DELHI => ['INDIA GATE','NEW DELHI'],US => ['纽约','洛杉矶']

因此,您的需求具有显式的映射关系,因此您可以尝试先构建映射器对象或数组以用于映射用法。请参阅以下代码片段:

const _ = require('lodash');
let getRegion = () => {
    return {
        'OUTSIDE INDIA': getOutInd(),
        'INSIDE INDIA': getInInd()
    }
}
let getOutInd = () => {
    return {
        'US': ['NEW YORK', 'LOS ANGELES'],
        'UK': [],
        'CHINA': []
    }
}
let getInInd = () => {
    return {
        'DELHI': ['INDIA GATE', 'NEW DELHI'],
        'MUMBAI': [],
        'PUNE': []
    }
}
let map = {
    'PvtBank': {
        'TBM': getRegion(),
        'CUB': getRegion(),
        'KVB': getRegion()
    },
    'GovtBank': ['IOB', 'CBI', 'BOB']
}
bot.dialog('mainFlow', [(session, args, next) => {
    if(!_.isArray(session.conversationData.choices)){
        session.conversationData.choices = new Array();
    }
    if(session.conversationData.TravelType){
        session.conversationData.choices.push(session.conversationData.TravelType)
        session.conversationData.currentChoice = _.last(session.conversationData.choices)
    }
    session.conversationData.currentMap = session.conversationData.currentChoice? _.get(session.conversationData.currentMap,session.conversationData.currentChoice):map;
    let promits = _.isArray(session.conversationData.currentMap) ? _.values(session.conversationData.currentMap).join(`|`): _.keys(session.conversationData.currentMap).join(`|`);
    builder.Prompts.choice(session, "Whould you like me to taks about?", promits, {
        listStyle: builder.ListStyle.button
    });
}, (session, args, next) => {
    session.conversationData.TravelType = args.response.entity;
    session.replaceDialog('mainFlow');
}]).endConversationAction("stop",
    "", {
        matches: /^cancel$|^goodbye$|^exit|^stop|^close/i
        // confirmPrompt: "This will cancel your order. Are you sure?"
    }
);