Skip to content

Instantly share code, notes, and snippets.

@dgkanatsios
Last active October 30, 2016 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgkanatsios/f87ad0f25bf26e57384807351a74ea1a to your computer and use it in GitHub Desktop.
Save dgkanatsios/f87ad0f25bf26e57384807351a74ea1a to your computer and use it in GitHub Desktop.
[Serializable]
public class DateTimeInputDialog : IDialog<DateTime>
{
/// <summary>
/// If it is null, we're looking for DateTimeFrom
/// Else, we're looking for DateTimeTo
/// </summary>
private DateTime? inputDateTime;
private ParkingAreaDetails selectedParkingLocationDetails;
string selectedDateString, selectedTimeString;
DateTime selectedDateTime;
public DateTimeInputDialog(DateTime? inputdt, ParkingAreaDetails selectedPLD)
{
inputDateTime = inputdt;
selectedParkingLocationDetails = selectedPLD;
}
public async Task StartAsync(IDialogContext context)
{
await PostPromptDateInputMessageAsync(context);
context.Wait(DateSelected);
}
/// <summary>
/// Parses user's input. If it's "today" or "tomorrow" then we're good to go. Otherwise, it tries to parse the date. If all fail, it asks again for input.
/// </summary>
/// <param name="context"></param>
/// <param name="argument"></param>
/// <returns></returns>
public virtual async Task DateSelected(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
bool success = false;
var message = await argument;
if (message.Text.CompareInput(StringMessages.Today)) //"today" as input
{
success = true;
var date = DateTime.UtcNow.AddHours(selectedParkingLocationDetails.UtcTimeDiff).ToString("dd/MM/yyy");
selectedDateString = date;
}
else if (message.Text.CompareInput(StringMessages.Tomorrow))//"tomorrow" as input
{
success = true;
var date = DateTime.UtcNow.Date.AddDays(1).AddHours(selectedParkingLocationDetails.UtcTimeDiff).ToString("dd/MM/yyy");
selectedDateString = date;
}
else if (Utilities.DateRegex.IsMatch(message.Text))
{
var selectedDate = Utilities.ConvertDateTimeStringToDate(message.Text, null);
if (selectedDate < DateTime.UtcNow.AddHours(selectedParkingLocationDetails.UtcTimeDiff).Date)
{
success = false;
await context.PostAsync(StringMessages.SelectedDateLessThanNow);
}
else
{
success = true;
selectedDateString = message.Text;
}
}
else //no valid input
{
success = false;
await context.PostAsync(StringMessages.WrongInputDate);
}
if (success)
{
await PostPromptTimeInputMessageAsync(context);
}
else
{
await PostPromptDateInputMessageAsync(context);
context.Wait(DateSelected);
}
}
public virtual async Task TimeSelected(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
if (Utilities.TimeRegex.IsMatch(message.Text))
{
selectedTimeString = message.Text;
selectedDateTime = Utilities.ConvertDateTimeStringToDate(selectedDateString, selectedTimeString);
//check if selected date is in the past
if (selectedDateTime < DateTime.Now)
{
await context.PostAsync(StringMessages.SelectedTimeLessThanNow);
context.Wait(TimeSelected);
}
//we're checking the "to" date and it's less than "from"
else if (inputDateTime.HasValue && selectedDateTime < inputDateTime)
{
await context.PostAsync(StringMessages.DepartureDateLessThanArrival);
context.Wait(DateSelected);
}
else
{
context.Done(selectedDateTime);
}
}
else
{
await context.PostAsync(StringMessages.WrongInputTime);
context.Wait(TimeSelected);
}
}
private async Task PostPromptTimeInputMessageAsync(IDialogContext context)
{
string msg = inputDateTime.HasValue ? StringMessages.SelectTimeTo : StringMessages.SelectTimeFrom;
await context.PostAsync(msg);
context.Wait(TimeSelected);
}
private async Task PostPromptDateInputMessageAsync(IDialogContext context)
{
string msg = inputDateTime.HasValue ? StringMessages.SelectDateToList : StringMessages.SelectDateFromList;
var message = context.MakeMessage();
message.AttachmentLayout = AttachmentLayoutTypes.List;
var attachments = new List<Attachment>();
//we're looking for the arrival date
if (inputDateTime == null)
{
var actions = new List<CardAction>()
{
Utilities.CreateCardAction(StringMessages.Today,StringMessages.Today),
Utilities.CreateCardAction(StringMessages.Tomorrow,StringMessages.Tomorrow),
};
attachments.Add(Utilities.CreateHeroCardAttachment(msg, StringMessages.UpcomingDates, null, null, actions));
}
else //we're looking for the departure date
//add 3 and 5 days to the arrival date and suggest them to the user
{
var after3days = inputDateTime.Value.Date.AddDays(3).ToString("dd/MM/yyyy");
var after5days = inputDateTime.Value.Date.AddDays(5).ToString("dd/MM/yyyy");
var actions = new List<CardAction>()
{
Utilities.CreateCardAction(after3days,after3days),
Utilities.CreateCardAction(after5days,after5days),
};
attachments.Add(Utilities.CreateHeroCardAttachment(msg, StringMessages.UpcomingDatesReturn, null, null, actions));
}
message.Attachments = attachments;
await context.PostAsync(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment