private async Task SendEventNotofication(ITurnContext turnContext, string eventMessage) { PasswordNotfications notifications = await _passwordNotificationsPropertyAccessor.GetAsync(turnContext, () => new PasswordNotfications()); PasswordEventNotification passwordEventNotification = JsonConvert.DeserializeObject <PasswordEventNotification>(eventMessage); PasswordNotfications.NotificationData eventUserData = notifications[passwordEventNotification.PasswordEvent.UserID]; await SendNotificationsAsync(turnContext.Adapter, AppId, eventUserData, passwordEventNotification); }
// Creates the turn logic to use for the proactive message. private BotCallbackHandler CreateCallback(PasswordNotfications.NotificationData notification, PasswordEventNotification passwordEventNotification) { return(async(turnContext, token) => { // Get the job log from state, and retrieve the job. PasswordNotfications notifications = await _passwordNotificationsPropertyAccessor.GetAsync(turnContext, () => new PasswordNotfications()); // Set the new property await _passwordNotificationsPropertyAccessor.SetAsync(turnContext, notifications); // Now save it into the JobState await _notificationState.SaveChangesAsync(turnContext); // Send the user a proactive confirmation message. await turnContext.SendActivityAsync($"Password for {notification.UserID} is about to expire on {passwordEventNotification.PasswordEvent.PasswordExpirationDate}."); }); }
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) { if (turnContext.Activity.Type != ActivityTypes.Message) { // Handle non-message activities. await OnSystemActivityAsync(turnContext); } else { //var msgText = turnContext.Activity.Text.Trim().ToLowerInvariant(); var msgText = turnContext.Activity.Text.Trim(); if (turnContext.Activity.ChannelId == "emulator" || turnContext.Activity.ChannelId == "directline") { //Test the message to determine if it contains a notificaiton commands if (msgText.Contains("PasswordEventNotification")) { await SendEventNotofication(turnContext, msgText); await turnContext.SendActivityAsync("Notifications Sent"); return; } } PasswordNotfications notifications = await _passwordNotificationsPropertyAccessor.GetAsync(turnContext, () => new PasswordNotfications()); // Run the DialogSet - let the framework identify the current state of the dialog from // the dialog stack and figure out what (if any) is the active dialog. var dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken); var results = await dialogContext.ContinueDialogAsync(cancellationToken); switch (msgText) { case "notify": case "notify me": // If the DialogTurnStatus is Empty we should start a new dialog. if (results.Status == DialogTurnStatus.Empty) { // A prompt dialog can be started directly on the DialogContext. The prompt text is given in the PromptOptions. await dialogContext.PromptAsync( "EmailAddress", new PromptOptions { Prompt = MessageFactory.Text("Please enter your email.") }, cancellationToken); } break; case "show": // Display information for all jobs in the log. if (notifications.Count > 0) { await turnContext.SendActivityAsync($"There are {notifications.Count} notifications registered in the system"); } else { await turnContext.SendActivityAsync("The notifications collection is empty."); } break; default: if (results.Status == DialogTurnStatus.Complete) { // Check for a result. if (results.Result != null) { string userId = turnContext.Activity.Text; // Create a new notification for the user. PasswordNotfications.NotificationData notification = CreateNotification(turnContext, notifications, userId); // Set the new property await _passwordNotificationsPropertyAccessor.SetAsync(turnContext, notifications); // Now save it into the Notification State await _notificationState.SaveChangesAsync(turnContext); await turnContext.SendActivityAsync( $"Created notification for {results.Result}. We'll notify you when a password expiration is about to occur."); } } else { await turnContext.SendActivityAsync("I didn't understand your input. Try again."); } break; } // Save the new turn count into the conversation state. await _accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken); } }
private PasswordNotfications.NotificationData CreateNotification(ITurnContext turnContext, PasswordNotfications notifications, string userId) { PasswordNotfications.NotificationData notification = new PasswordNotfications.NotificationData { UserID = userId, CreatedTimeStamp = DateTime.Now, Conversation = turnContext.Activity.GetConversationReference(), }; notifications[notification.UserID] = notification; return(notification); }