问题描述
我是Bot Framework的新手,如果遇到任何错误,如何解决?
I'm new to Bot Framework, if i made any mistake, how to correct it?
我的Bot用于航班预订的简单场景,机器人从用户那里获取输入,用于输入城市,城市,出发日期,返回日期.收到返回日期后,我必须验证给定日期是否大于出发日期.
A simple scenario of my Bot which is used for flight booking, bot gets input from user for from city, to city, departure date, return date. After receiving return date, i have to validate whether the given date is greater than departure date.
如何将值从StepContext传递到Promptvalidatorcontext?
How to pass values from StepContext to Promptvalidatorcontext?
public class Travel_FlightBookingDialog : ComponentDialog
{
public Travel_FlightBookingDialog(string dialogId) : base(nameof(Travel_FlightBookingDialog))
{
AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
AddDialog(new ConfirmPrompt("confirmPrompt"));
AddDialog(new TextPrompt("FROMCITY", FromCityValidationAsync));
AddDialog(new TextPrompt("DEPARTUREDATE", DepartureValidationAsync));
AddDialog(new TextPrompt("ARRIVALDATE", ArrivalValidationAsync));
AddDialog(new TextPrompt("PAXCOUNT", PaxCountValidationAsync));
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
BOOKINGTYPE,
FROMCITY,
TOCITY,
DEPARTUREDATE,
ARRIVALDATE,
PAXCOUNT,
CLASSOFBOOKING,
DIRECTORCONN,
LCCORFSC,
CONFIRM,
FINAL
}));
InitialDialogId = nameof(WaterfallDialog);
}
private async Task<bool> DepartureValidationAsync(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
{
var DepartureDate = promptContext.Recognized.Value;
string valifinfo = ValidateDate(DepartureDate);
if (valifinfo == "")
{
return true;
}
else
{
await promptContext.Context.SendActivityAsync(valifinfo);
return false;
}
}
private async Task<bool> ArrivalValidationAsync(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
{
**//HOW DO WE GET THE VALUE OF DEPARTURE DATE HERE BECAUSE
//HERE NEED TO VALIDATE - RETURN DATE SHOULD BE GREATER THAN DEPARTURE DATE**
}
private async Task<bool> FromCityValidationAsync(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
{
var FromcityValidation = promptContext.Recognized.Value;
var count = Convert.ToString(promptContext.Recognized.Value).Length;
List<string> range;
if (count != 3)
{
range = IataHelper1.findAirport("", FromcityValidation);
var strlist = "";
foreach (string item in range)
{
if (item != "NODATA")
{
strlist += item + Environment.NewLine;
}
}
if (range.Count > 0)
{
if (range.Count == 1)
{
return true;
}
else if (range.Count > 1)
{
await promptContext.Context.SendActivityAsync($"Kindly Choose any one Airport from suggestion List :{Environment.NewLine} {strlist}");
return false;
}
}
else
{
await promptContext.Context.SendActivityAsync("Please Enter valid City name.....!!!!!");
return false;
}
}
else
{
range = IataHelper1.findAirport(FromcityValidation, "");
if (range != null)
{
foreach (string item in range)
{
if (item != "NODATA")
{
///return Task.FromResult(true);
return true;
}
}
}
else
{
await promptContext.Context.SendActivityAsync("Please Enter valid City Code.....!!!!!");
return false;
}
}
return false;
}
private async Task<DialogTurnResult> BOOKINGTYPE(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var pmoptions = new PromptOptions
{
Prompt = MessageFactory.Text($"Please Choose Booking Type :"),
Choices = GetBookingType()
};
return await stepContext.PromptAsync(nameof(ChoicePrompt), pmoptions, cancellationToken);
}
private async Task<DialogTurnResult> FROMCITY(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["BOOKINGTYPE"] = stepContext.Result;
var promptFromCity = stepContext.PromptAsync("FROMCITY", new PromptOptions()
{
Prompt = MessageFactory.Text($"Please enter Departure City Code or City Name (Eg: DEL or Delhi)"),
RetryPrompt = MessageFactory.Text("Please enter the valid City Code or City Name.....")
}, cancellationToken);
return await promptFromCity;
}
private async Task<DialogTurnResult> TOCITY(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["FROMCITY"] = stepContext.Result;
var promptToCity = stepContext.PromptAsync("FROMCITY", new PromptOptions()
{
Prompt = MessageFactory.Text("Please enter Arrival City Code or City Name (Eg: MAA or Chennai)"),
RetryPrompt = MessageFactory.Text("Please enter the valid City Code or City Name.....")
}, cancellationToken);
return await promptToCity;
}
private async Task<DialogTurnResult> DEPARTUREDATE(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["TOCITY"] = stepContext.Result;
var promptDepartureDate = stepContext.PromptAsync("DEPARTUREDATE", new PromptOptions()
{
Prompt = MessageFactory.Text("Please enter Departure Date : (Eg: DD-MM-YYYY format)..."),
RetryPrompt = MessageFactory.Text("Please enter the valid Date.....")
}, cancellationToken);
return await promptDepartureDate;
}
private async Task<DialogTurnResult> ARRIVALDATE(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["DEPARTUREDATE"] = stepContext.Result;
var bookingtype = (FoundChoice)stepContext.Values["BOOKINGTYPE"];
if (bookingtype.Value == "ONEWAY")
{
return await stepContext.NextAsync(null, cancellationToken);
}
var promptArrivalDate = stepContext.PromptAsync("ARRIVALDATE", new PromptOptions()
{
Prompt = MessageFactory.Text("Please enter Arrival Date in case of Roundtrip : (Eg: DD-MM-YYYY format)..."),
RetryPrompt = MessageFactory.Text("Please enter the valid Date.....")
}, cancellationToken);
return await promptArrivalDate;
}
private async Task<DialogTurnResult> PAXCOUNT(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["ARRIVALDATE"] = stepContext.Result;
var promptPaxCount = stepContext.PromptAsync("PAXCOUNT", new PromptOptions()
{
Prompt = MessageFactory.Text("Please enter No.Of Passengers : (Eg: AdultCount,ChildCount,InfantCount)..."),
}, cancellationToken);
return await promptPaxCount;
}
public string ValidateDate(string Date)
{
DateTime dateTimeObj = DateTime.ParseExact(Date, "dd-MM-yyyy", CultureInfo.InvariantCulture);
if (Date != "" && dateTimeObj < DateTime.Today)
{
return "Date should be Future or equal To Today Date";
}
return "";
}
}
推荐答案
您应该使用turnContext将信息从stepContext传递到hintContext.promptContext.context.turnState.=
You should use turnContext to pass information from stepContext to promptContext.promptContext.context.turnState. =
这篇关于如何在机器人瀑布对话框模型中将值从StepContext传递到Promptvalidatorcontext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!