我有一个连接到MongoDB并获取结果的对话框。如果没有结果,则将控制传递给另一个对话框以获取有效的订单号。 对话框完成后,收集订单ID控件必须传递回上一个对话框,但在对话框的顶部。我如何实现这一目标?
OR 有没有更好的方法来做我想做的事情
拨打对话:
bot.dialog('Get_order_id', [
function (session, args) {
if (args && args.reprompt) {
builder.Prompts.text(session, "Enter a valid order number number ")
} else {
builder.Prompts.text(session, "What's your order number?");
}
},
function (session, results) {
var m1 = new RegExp(/5-7-[0-9]{1,12}/);
var m2 = new RegExp(/1-[0-9]{1,12}/);
var matched1 = results.response.match(m1)
var matched2 = results.response.match(m2)
if (matched1 || matched2) {
session.conversationData.order_id_prospect=session.message.text;
session.endDialogWithResult({ response: session.message.text });
} else {
session.replaceDialog('Get_order_id', { reprompt: true });
}
}]);
获取订单ID:
bot.dialog('get_order_details',[
function(session){
session.conversationData.order_id_prospect=session.conversationData.order_id_prospect.replace(/ /g,'');
var order_obj1=mongoose.model('order_mod', order_schema);
order_obj1.find({ 'ORDER_NUM': session.conversationData.order_id_prospect }, 'ORDER_NUM CREATED LAST_UPD COMPLETION_DATE APPOINTED ORDER_STATUS LINE_ITEM_STATUS KCI1_Sent KCI2_Sent KCI3_Sent CUSTOMER_REQUIRED_DATE CUSTOMER_COMMITTED_DATE ACTUAL_CCD',
function (err, athletes) {
if (err) return handleError(err);
if (!athletes.length){
// 'athletes' contains the list of athletes that match the criteria.
delete session.conversationData.order;
session.send("Order details not found. Restarting Order Status Workflow. Type Cancel to exit to MainMenu. ")
session.beginDialog('Get_order_id');
}else{
session.send('Order id: **_%s_** was last updated on **_%s_**. <br/> KCI1 Sent Date: **_%s_**',athletes[0].ORDER_NUM,athletes[0].LAST_UPD,athletes[0].KCI1_Sent);
session.endDialogWithResult({response:'order details'});
}
}).lean()
}])
enter code here
答案 0 :(得分:0)
我试图在对话框之间重构逻辑,希望使其更清晰。
为什么不考虑get_order_details
对话框来调用Get_order_id
,我制作了一个简单的示例来尝试重现您的方案,请考虑以下代码段:
var bot = new builder.UniversalBot(connector, [(session) => {
session.replaceDialog('get_order_details');
}])
bot.dialog('Get_order_id', [
(session, args) => {
if (args && args.reprompt) {
builder.Prompts.text(session, "Enter a valid order number number ")
} else {
builder.Prompts.text(session, "What's your order number?");
}
},
(session, results) => {
var m1 = new RegExp(/5-7-[0-9]{1,12}/);
var m2 = new RegExp(/1-[0-9]{1,12}/);
var matched1 = results.response.match(m1)
var matched2 = results.response.match(m2)
if (matched1 || matched2) {
session.conversationData.order_id_prospect = session.message.text;
session.endDialogWithResult({
response: session.message.text
});
} else {
session.replaceDialog('Get_order_id', {
reprompt: true
});
}
}
])
bot.dialog('get_order_details', [
(session, args, next) => {
console.log(session.conversationData.order_id_prospect)
if (!session.conversationData.order_id_prospect || args.get_order) {
session.beginDialog('Get_order_id')
} else {
next();
}
},(session,args,next)=>{
Promise.resolve(true).then(() => {
const n = Math.round(Math.random());
if (n) {
session.endDialog(`${n}`);
} else {
session.replaceDialog('get_order_details',{get_order:true})
}
})
}
])