microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Core/BotApplication.cs
151lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Diagnostics; |
| 5 | using Microsoft.AspNetCore.Http; |
| 6 | using Microsoft.Extensions.Logging; |
| 7 | using Microsoft.Extensions.Logging.Abstractions; |
| 8 | using Microsoft.Teams.Bot.Core.Hosting; |
| 9 | using Microsoft.Teams.Bot.Core.Schema; |
| 10 | |
| 11 | namespace Microsoft.Teams.Bot.Core; |
| 12 | |
| 13 | /// <summary> |
| 14 | /// Represents a bot application. |
| 15 | /// </summary> |
| 16 | public class BotApplication |
| 17 | { |
| 18 | private readonly ILogger<BotApplication> _logger; |
| 19 | private readonly ConversationClient? _conversationClient; |
| 20 | private readonly UserTokenClient? _userTokenClient; |
| 21 | internal TurnMiddleware MiddleWare { get; } |
| 22 | |
| 23 | /// <summary> |
| 24 | /// Creates a default instance, primarily for testing purposes. The ConversationClient and UserTokenClient properties will not be initialized |
| 25 | /// </summary> |
| 26 | protected BotApplication() |
| 27 | { |
| 28 | _logger = NullLogger<BotApplication>.Instance; |
| 29 | MiddleWare = new TurnMiddleware(); |
| 30 | } |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Initializes a new instance of the BotApplication class with the specified conversation client, app ID, |
| 34 | /// and logger. |
| 35 | /// Initializes a new instance of the BotApplication class with the specified conversation client, app ID, |
| 36 | /// and logger. |
| 37 | /// </summary> |
| 38 | /// <param name="conversationClient">The client used to manage and interact with conversations for the bot.</param> |
| 39 | /// <param name="userTokenClient">The client used to manage user tokens for authentication.</param> |
| 40 | /// <param name="logger">The logger used to record operational and diagnostic information for the bot application.</param> |
| 41 | /// <param name="options">Options containing the application (client) ID, used for logging and diagnostics. Defaults to an empty instance if not provided.</param> |
| 42 | public BotApplication(ConversationClient conversationClient, UserTokenClient userTokenClient, ILogger<BotApplication> logger, BotApplicationOptions? options = null) |
| 43 | { |
| 44 | options ??= new(); |
| 45 | _logger = logger; |
| 46 | MiddleWare = new TurnMiddleware(); |
| 47 | _conversationClient = conversationClient; |
| 48 | _userTokenClient = userTokenClient; |
| 49 | logger.LogInformationGuarded("Started {ThisType} listener for AppID:{AppId} with SDK version {SdkVersion}", GetType().Name, options.AppId, Version); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Gets the client used to manage and interact with conversations. |
| 55 | /// </summary> |
| 56 | /// <remarks>Accessing this property before the client is initialized will result in an exception. Ensure |
| 57 | /// that the client is properly configured before use.</remarks> |
| 58 | public ConversationClient ConversationClient => _conversationClient ?? throw new InvalidOperationException("ConversationClient not initialized"); |
| 59 | |
| 60 | /// <summary> |
| 61 | /// Gets the client used to manage user tokens for authentication. |
| 62 | /// </summary> |
| 63 | /// <remarks>Accessing this property before the client is initialized will result in an exception. Ensure |
| 64 | /// that the client is properly configured before use.</remarks> |
| 65 | public UserTokenClient UserTokenClient => _userTokenClient ?? throw new InvalidOperationException("UserTokenClient not registered"); |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Gets or sets the delegate that is invoked to handle an incoming activity asynchronously. |
| 69 | /// </summary> |
| 70 | /// <remarks>Assign a delegate to process activities as they are received. The delegate should accept an |
| 71 | /// <see cref="CoreActivity"/> and a <see cref="CancellationToken"/>, and return a <see cref="Task"/> representing the |
| 72 | /// asynchronous operation. If <see langword="null"/>, incoming activities will not be handled.</remarks> |
| 73 | public virtual Func<CoreActivity, CancellationToken, Task>? OnActivity { get; set; } |
| 74 | |
| 75 | /// <summary> |
| 76 | /// Processes an incoming HTTP request containing a bot activity. |
| 77 | /// </summary> |
| 78 | /// <param name="httpContext"></param> |
| 79 | /// <param name="cancellationToken"></param> |
| 80 | /// <returns></returns> |
| 81 | /// <exception cref="InvalidOperationException"></exception> |
| 82 | /// <exception cref="BotHandlerException"></exception> |
| 83 | public virtual async Task ProcessAsync(HttpContext httpContext, CancellationToken cancellationToken = default) |
| 84 | { |
| 85 | ArgumentNullException.ThrowIfNull(httpContext); |
| 86 | ArgumentNullException.ThrowIfNull(_conversationClient); |
| 87 | |
| 88 | _logger.LogDebug("Start processing HTTP request for activity"); |
| 89 | |
| 90 | CoreActivity activity = await CoreActivity.FromJsonStreamAsync(httpContext.Request.Body, cancellationToken).ConfigureAwait(false) ?? throw new InvalidOperationException("Invalid Activity"); |
| 91 | |
| 92 | _logger.LogInformationGuarded("Activity received: Type={Type} Id={Id} ServiceUrl={ServiceUrl} MSCV={MSCV}", |
| 93 | activity.Type, |
| 94 | activity.Id, |
| 95 | activity.ServiceUrl, |
| 96 | httpContext.Request.GetCorrelationVector()); |
| 97 | |
| 98 | _logger.LogTraceGuarded("Received activity: {Activity}", activity.ToJson()); |
| 99 | |
| 100 | // TODO: Replace with structured scope data, ensure it works with OpenTelemetry and other logging providers |
| 101 | using (_logger.BeginScope("ActivityType={ActivityType} ActivityId={ActivityId} ServiceUrl={ServiceUrl} MSCV={MSCV}", |
| 102 | activity.Type, activity.Id, activity.ServiceUrl, httpContext.Request.GetCorrelationVector())) |
| 103 | { |
| 104 | try |
| 105 | { |
| 106 | CancellationToken token = Debugger.IsAttached ? CancellationToken.None : cancellationToken; |
| 107 | await MiddleWare.RunPipelineAsync(this, activity, this.OnActivity, 0, token).ConfigureAwait(false); |
| 108 | } |
| 109 | catch (Exception ex) |
| 110 | { |
| 111 | _logger.LogError(ex, "Error processing activity: Id={Id}", activity.Id); |
| 112 | throw new BotHandlerException("Error processing activity", ex, activity); |
| 113 | } |
| 114 | finally |
| 115 | { |
| 116 | _logger.LogInformationGuarded("Finished processing activity: Id={Id}", activity.Id); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// <summary> |
| 122 | /// Adds the specified turn middleware to the middleware pipeline. |
| 123 | /// </summary> |
| 124 | /// <param name="middleware">The middleware component to add to the pipeline. Cannot be null.</param> |
| 125 | /// <returns>An ITurnMiddleWare instance representing the updated middleware pipeline.</returns> |
| 126 | public ITurnMiddleware UseMiddleware(ITurnMiddleware middleware) |
| 127 | { |
| 128 | MiddleWare.Use(middleware); |
| 129 | return MiddleWare; |
| 130 | } |
| 131 | |
| 132 | /// <summary> |
| 133 | /// Sends the specified activity to the conversation asynchronously. |
| 134 | /// </summary> |
| 135 | /// <param name="activity">The activity to send to the conversation. Cannot be null.</param> |
| 136 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param> |
| 137 | /// <returns>A task that represents the asynchronous operation. The task result contains the identifier of the sent activity.</returns> |
| 138 | /// <exception cref="Exception">Thrown if the conversation client has not been initialized.</exception> |
| 139 | public async Task<SendActivityResponse?> SendActivityAsync(CoreActivity activity, CancellationToken cancellationToken = default) |
| 140 | { |
| 141 | ArgumentNullException.ThrowIfNull(activity); |
| 142 | ArgumentNullException.ThrowIfNull(_conversationClient, "ConversationClient not initialized"); |
| 143 | |
| 144 | return await _conversationClient.SendActivityAsync(activity, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 145 | } |
| 146 | |
| 147 | /// <summary> |
| 148 | /// Gets the version of the SDK. |
| 149 | /// </summary> |
| 150 | public static string Version => ThisAssembly.NuGetPackageVersion; |
| 151 | } |
| 152 | |