node.js - 如何使用 Node 在 MS bot 中动态创建流程

标签 node.js nodes botframework

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

流程是:当用户从A、B、C和D中按A时,将显示A1、A2、A3和A4,当用户按A1时,将显示A11、A12、A13和A14,当用户按A12时,将显示A121、A122、A123和A124同样连续流动。

enter image description here

以下是完整流程 enter image description here

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

我需要使用问题、请求、返回、访问、房间类型、预订引擎、最佳灵活房价、虚拟房价代码、库存计数等,而不是 A、B、C。出于理解目的,我使用了 A、B、C。

使用这些输入来建立关系,而不是 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?"
    }
);

最佳答案

请引用以下代码片段:

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?"
    }
);

更新

Okay. Take this inputs for relationship instead of 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']

所以你的需求有明确的映射关系,所以你可以尝试先构建一个映射器对象或数组来进行映射使用。请引用以下代码片段:

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?"
    }
);

关于node.js - 如何使用 Node 在 MS bot 中动态创建流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48314484/

相关文章:

angularjs - 使用 Satellizer.js 注销

node.js - Node.js 流可以做成协程吗?

php - 具有嵌套节点的递归数组解析

bots - 如何在本地计算机上运行在 QnA Maker 的帮助下创建的机器人?

node.js - 在 Microsoft Luis 中,如何对实体数组建模?

javascript - ES6类中类/模块类

javascript - 无法从外部 IP 访问 Node.js

java - 双向链表节点问题

c++ - 将节点指针设置为空

c# - Azure Bot Framework、QnA Maker API、如何在 QnA Dialogue 中获取查询文本