// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema;
namespace PABot.Bots
{
///
/// This IBot implementation can run any type of Dialog. The use of type parameterization allows multiple different bots
/// to be run at different endpoints within the same project. This can be achieved by defining distinct Controller types
/// each with dependency on distinct IBot types, this way ASP Dependency Injection can glue everything together without ambiguity.
/// The ConversationState is used by the Dialog system. The UserState isn't, however, it might have been used in a Dialog implementation,
/// and the requirement is that all BotState objects are saved at the end of a turn.
///
/// The type of the dialog.
public class DialogBot : TeamsActivityHandler where T : Dialog
{
protected readonly BotState _conversationState;
protected readonly Dialog _dialog;
protected readonly ILogger _logger;
protected readonly BotState _userState;
///
/// Initializes a new instance of the class.
///
/// The conversation state.
/// The user state.
/// The dialog.
/// The logger.
public DialogBot(ConversationState conversationState, UserState userState, T dialog, ILogger> logger)
{
_conversationState = conversationState;
_userState = userState;
_dialog = dialog;
_logger = logger;
}
///
/// Handles an incoming activity.
///
/// Context object containing information cached for a single turn of conversation with a user.
/// Propagates notification that operations should be canceled.
/// A task that represents the work queued to execute.
///
/// Reference link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.bot.builder.activityhandler.onturnasync?view=botbuilder-dotnet-stable.
///
public override async Task OnTurnAsync(
ITurnContext turnContext,
CancellationToken cancellationToken = default(CancellationToken))
{
await base.OnTurnAsync(turnContext, cancellationToken);
// Save any state changes that might have occurred during the turn.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}
///
/// Handles when a message is addressed to the bot.
///
/// Context object containing information cached for a single turn of conversation with a user.
/// Propagates notification that operations should be canceled.
/// A Task resolving to either a login card or the adaptive card of the Reddit post.
///
/// For more information on bot messaging in Teams, see the documentation
/// https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/conversation-basics?tabs=dotnet#receive-a-message.
///
protected override async Task OnMessageActivityAsync(
ITurnContext turnContext,
CancellationToken cancellationToken)
{
_logger.LogInformation("Running dialog with Message Activity.");
await _dialog.RunAsync(turnContext, _conversationState.CreateProperty(nameof(DialogState)), cancellationToken);
}
}
}