// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Microsoft.Bot.Builder; using Microsoft.Bot.Builder.Dialogs; using Microsoft.Bot.Connector.Authentication; using Microsoft.Bot.Schema; namespace PABot.Dialogs { /// /// A dialog that handles user logout. /// public class LogoutDialog : ComponentDialog { /// /// Initializes a new instance of the class. /// /// The dialog ID. /// The connection name configured in Azure Bot service. public LogoutDialog(string id, string connectionName) : base(id) { ConnectionName = connectionName; } /// /// Gets the configured connection name in Azure Bot service. /// protected string ConnectionName { get; } /// /// Called when the dialog is started and pushed onto the parent's dialog stack. /// /// The inner DialogContext for the current turn of conversation. /// Initial information to pass to the dialog. /// Propagates notification that operations should be canceled. /// A task representing the asynchronous operation. protected override async Task OnBeginDialogAsync( DialogContext innerDc, object options, CancellationToken cancellationToken = default(CancellationToken)) { DialogTurnResult result = await InterruptAsync(innerDc, cancellationToken); if (result != null) { return result; } return await base.OnBeginDialogAsync(innerDc, options, cancellationToken); } /// /// Called when the dialog is continued, where it is the active dialog and the user replies with a new activity. /// /// The inner DialogContext for the current turn of conversation. /// Propagates notification that operations should be canceled. /// A task representing the asynchronous operation. protected override async Task OnContinueDialogAsync( DialogContext innerDc, CancellationToken cancellationToken = default(CancellationToken)) { DialogTurnResult result = await InterruptAsync(innerDc, cancellationToken); if (result != null) { return result; } return await base.OnContinueDialogAsync(innerDc, cancellationToken); } /// /// Called when the dialog is interrupted, where it is the active dialog and the user replies with a new activity. /// /// The inner DialogContext for the current turn of conversation. /// Propagates notification that operations should be canceled. /// A task representing the asynchronous operation. private async Task InterruptAsync( DialogContext innerDc, CancellationToken cancellationToken = default(CancellationToken)) { if (innerDc.Context.Activity.Type == ActivityTypes.Message) { string text = innerDc.Context.Activity.Text.ToLowerInvariant(); // Allow logout anywhere in the command if (text.Contains("logout")) { // The UserTokenClient encapsulates the authentication processes. UserTokenClient userTokenClient = innerDc.Context.TurnState.Get(); await userTokenClient.SignOutUserAsync(innerDc.Context.Activity.From.Id, ConnectionName, innerDc.Context.Activity.ChannelId, cancellationToken).ConfigureAwait(false); await innerDc.Context.SendActivityAsync(MessageFactory.Text("You have been signed out."), cancellationToken); return await innerDc.CancelAllDialogsAsync(cancellationToken); } } return null!; } } }