microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/PABot/Bots/DialogBot.cs
80lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Bot.Builder; |
| 5 | using Microsoft.Bot.Builder.Dialogs; |
| 6 | using Microsoft.Bot.Builder.Teams; |
| 7 | using Microsoft.Bot.Schema; |
| 8 | |
| 9 | namespace PABot.Bots |
| 10 | { |
| 11 | /// <summary> |
| 12 | /// This IBot implementation can run any type of Dialog. The use of type parameterization allows multiple different bots |
| 13 | /// to be run at different endpoints within the same project. This can be achieved by defining distinct Controller types |
| 14 | /// each with dependency on distinct IBot types, this way ASP Dependency Injection can glue everything together without ambiguity. |
| 15 | /// The ConversationState is used by the Dialog system. The UserState isn't, however, it might have been used in a Dialog implementation, |
| 16 | /// and the requirement is that all BotState objects are saved at the end of a turn. |
| 17 | /// </summary> |
| 18 | /// <typeparam name="T">The type of the dialog.</typeparam> |
| 19 | public class DialogBot<T> : TeamsActivityHandler where T : Dialog |
| 20 | { |
| 21 | protected readonly BotState _conversationState; |
| 22 | protected readonly Dialog _dialog; |
| 23 | protected readonly ILogger _logger; |
| 24 | protected readonly BotState _userState; |
| 25 | |
| 26 | /// <summary> |
| 27 | /// Initializes a new instance of the <see cref="DialogBot{T}"/> class. |
| 28 | /// </summary> |
| 29 | /// <param name="conversationState">The conversation state.</param> |
| 30 | /// <param name="userState">The user state.</param> |
| 31 | /// <param name="dialog">The dialog.</param> |
| 32 | /// <param name="logger">The logger.</param> |
| 33 | public DialogBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger) |
| 34 | { |
| 35 | _conversationState = conversationState; |
| 36 | _userState = userState; |
| 37 | _dialog = dialog; |
| 38 | _logger = logger; |
| 39 | } |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Handles an incoming activity. |
| 43 | /// </summary> |
| 44 | /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param> |
| 45 | /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> |
| 46 | /// <returns>A task that represents the work queued to execute.</returns> |
| 47 | /// <remarks> |
| 48 | /// Reference link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.bot.builder.activityhandler.onturnasync?view=botbuilder-dotnet-stable. |
| 49 | /// </remarks> |
| 50 | public override async Task OnTurnAsync( |
| 51 | ITurnContext turnContext, |
| 52 | CancellationToken cancellationToken = default(CancellationToken)) |
| 53 | { |
| 54 | await base.OnTurnAsync(turnContext, cancellationToken); |
| 55 | |
| 56 | // Save any state changes that might have occurred during the turn. |
| 57 | await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken); |
| 58 | await _userState.SaveChangesAsync(turnContext, false, cancellationToken); |
| 59 | } |
| 60 | |
| 61 | /// <summary> |
| 62 | /// Handles when a message is addressed to the bot. |
| 63 | /// </summary> |
| 64 | /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param> |
| 65 | /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> |
| 66 | /// <returns>A Task resolving to either a login card or the adaptive card of the Reddit post.</returns> |
| 67 | /// <remarks> |
| 68 | /// For more information on bot messaging in Teams, see the documentation |
| 69 | /// https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/conversation-basics?tabs=dotnet#receive-a-message. |
| 70 | /// </remarks> |
| 71 | protected override async Task OnMessageActivityAsync( |
| 72 | ITurnContext<IMessageActivity> turnContext, |
| 73 | CancellationToken cancellationToken) |
| 74 | { |
| 75 | _logger.LogInformation("Running dialog with Message Activity."); |
| 76 | |
| 77 | await _dialog.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |