public override Task <WaterfallDialog> GetWaterfallDialog(ITurnContext turnContext, CancellationToken cancellation) { return(Task.Run(() => { return new WaterfallDialog(Name, new WaterfallStep[] { async(dialogContext, cancellationToken) => { // Get which days they want to be contacted. return await dialogContext.PromptAsync( Prompt.DaysPrompt, new PromptOptions { Prompt = Phrases.Preferences.GetUpdateDays, RetryPrompt = Phrases.Preferences.GetUpdateDaysRetry }, cancellationToken); }, async(dialogContext, cancellationToken) => { // Get the result. This was already validated by the prompt. DayFlagsHelpers.FromString((string)dialogContext.Result, ",", out DayFlags dayFlags); var user = await api.GetUser(dialogContext.Context); user.ReminderFrequency = dayFlags; await this.api.Update(user); await Messages.SendAsync(Phrases.Preferences.UpdateDaysUpdated(dayFlags), dialogContext.Context, cancellationToken); return await dialogContext.EndDialogAsync(null, cancellationToken); } }); }));
public static PromptValidator <string> Create() { return(async(promptContext, cancellationToken) => { var success = DayFlagsHelpers.FromString(promptContext.Context.Activity.Text, ",", out DayFlags dayFlags); return await Task.FromResult(success); }); }
public async Task Update(string reminderDays, bool reminderDaysIsValid) { DayFlagsHelpers.FromString(reminderDays, ",", out var dayFlags); await CreateTestFlow(DaysDialog.Name) .Test("test", Phrases.Preferences.GetUpdateDays) .Test(reminderDays, reminderDaysIsValid ? Phrases.Preferences.UpdateDaysUpdated(dayFlags) : Phrases.Preferences.GetUpdateDaysRetry) .StartTestAsync(); if (reminderDaysIsValid) { var user = await this.Api.GetUser(this.turnContext); Assert.Equal(user.ReminderFrequency, dayFlags); } }
public static async Task DoWork(IConfiguration configuration, ILogger log = null) { var api = new CosmosInterface(configuration); var translator = new Translator(configuration); // The reminder message is static, so cache any translations to limit API calls. var translationCache = new Dictionary <string, string>(); // Get the current day. var day = DayFlagsHelpers.CurrentDay(); // Get all users. // TODO: this should likely be broken up so that it doesn't exceed the function time limit. var users = await api.GetUsers(); Helpers.LogInfo(log, $"Found {users.Count} users"); var queueHelper = new OutgoingMessageQueueHelpers(configuration.AzureWebJobsStorage()); foreach (var user in users) { // Check if the user should be reminded today. if (!user.ContactEnabled || !user.ReminderFrequency.HasFlag(day)) { continue; } // Check if the user should be reminded at this time. // Special case: use 6pm UTC (10am PST) if their reminder time isn't set. DateTime userReminderTime = string.IsNullOrEmpty(user.ReminderTime) ? DateTime.Parse("6:00pm") : DateTime.Parse(user.ReminderTime); // Using a 5 minute window in case the function triggers slightly early or late. if (Math.Abs((DateTime.UtcNow - userReminderTime).TotalMinutes) > 5) { continue; } var message = Phrases.Greeting.RemindToUpdate; // Check if the user's language is already cached. if (translationCache.TryGetValue(user.Language, out var translation)) { message = translation; } else { // Translate the message if necessary. if (translator.IsConfigured && user.Language != Translator.DefaultLanguage) { message = await translator.TranslateAsync(message, user.Language); } // Cache the message translationCache.Add(user.Language, message); } var data = new OutgoingMessageQueueData { PhoneNumber = user.PhoneNumber, Message = message }; await queueHelper.Enqueue(data); } }