gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:13:35 24 4
gpt4 key购买 nike

我想在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com