microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Core/BotApplication.cs
146lines · 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.AspNetCore.Mvc.Formatters; |
| 7 | using Microsoft.Extensions.Configuration; |
| 8 | using Microsoft.Extensions.Logging; |
| 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 | private readonly string _serviceKey; |
| 22 | internal TurnMiddleware MiddleWare { get; } |
| 23 | |
| 24 | /// <summary> |
| 25 | /// Initializes a new instance of the BotApplication class with the specified conversation client, configuration, |
| 26 | /// logger, and optional service key. |
| 27 | /// </summary> |
| 28 | /// <remarks>This constructor sets up the bot application and starts the bot listener using the provided |
| 29 | /// configuration and service key. The service key is used to locate authentication credentials in the |
| 30 | /// configuration.</remarks> |
| 31 | /// <param name="conversationClient">The client used to manage and interact with conversations for the bot.</param> |
| 32 | /// <param name="userTokenClient">The client used to manage user tokens for authentication.</param> |
| 33 | /// <param name="config">The application configuration settings used to retrieve environment variables and service credentials.</param> |
| 34 | /// <param name="logger">The logger used to record operational and diagnostic information for the bot application.</param> |
| 35 | /// <param name="sectionName">The configuration key identifying the authentication service. Defaults to "AzureAd" if not specified.</param> |
| 36 | public BotApplication(ConversationClient conversationClient, UserTokenClient userTokenClient, IConfiguration config, ILogger<BotApplication> logger, string sectionName = "AzureAd") |
| 37 | { |
| 38 | ArgumentNullException.ThrowIfNull(config); |
| 39 | _logger = logger; |
| 40 | _serviceKey = sectionName; |
| 41 | MiddleWare = new TurnMiddleware(); |
| 42 | _conversationClient = conversationClient; |
| 43 | _userTokenClient = userTokenClient; |
| 44 | string appId = config["MicrosoftAppId"] ?? config["CLIENT_ID"] ?? config[$"{sectionName}:ClientId"] ?? "Unknown AppID"; |
| 45 | logger.LogInformation(" Started {ThisType} listener \n on {Port} \n for AppID:{AppId} \n with SDK version {SdkVersion}", this.GetType().Name, config?["ASPNETCORE_URLS"], appId, Version); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /// <summary> |
| 51 | /// Gets the client used to manage and interact with conversations. |
| 52 | /// </summary> |
| 53 | /// <remarks>Accessing this property before the client is initialized will result in an exception. Ensure |
| 54 | /// that the client is properly configured before use.</remarks> |
| 55 | public ConversationClient ConversationClient => _conversationClient ?? throw new InvalidOperationException("ConversationClient not initialized"); |
| 56 | |
| 57 | /// <summary> |
| 58 | /// Gets the client used to manage user tokens for authentication. |
| 59 | /// </summary> |
| 60 | /// <remarks>Accessing this property before the client is initialized will result in an exception. Ensure |
| 61 | /// that the client is properly configured before use.</remarks> |
| 62 | public UserTokenClient UserTokenClient => _userTokenClient ?? throw new InvalidOperationException("UserTokenClient not registered"); |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Gets or sets the delegate that is invoked to handle an incoming activity asynchronously. |
| 66 | /// </summary> |
| 67 | /// <remarks>Assign a delegate to process activities as they are received. The delegate should accept an |
| 68 | /// <see cref="CoreActivity"/> and a <see cref="CancellationToken"/>, and return a <see cref="Task"/> representing the |
| 69 | /// asynchronous operation. If <see langword="null"/>, incoming activities will not be handled.</remarks> |
| 70 | public Func<CoreActivity, CancellationToken, Task>? OnActivity { get; set; } |
| 71 | |
| 72 | /// <summary> |
| 73 | /// Processes an incoming HTTP request containing a bot activity. |
| 74 | /// </summary> |
| 75 | /// <param name="httpContext"></param> |
| 76 | /// <param name="cancellationToken"></param> |
| 77 | /// <returns></returns> |
| 78 | /// <exception cref="InvalidOperationException"></exception> |
| 79 | /// <exception cref="BotHandlerException"></exception> |
| 80 | public async Task ProcessAsync(HttpContext httpContext, CancellationToken cancellationToken = default) |
| 81 | { |
| 82 | ArgumentNullException.ThrowIfNull(httpContext); |
| 83 | ArgumentNullException.ThrowIfNull(_conversationClient); |
| 84 | |
| 85 | _logger.LogDebug("Start processing HTTP request for activity"); |
| 86 | |
| 87 | CoreActivity activity = await CoreActivity.FromJsonStreamAsync(httpContext.Request.Body, cancellationToken).ConfigureAwait(false) ?? throw new InvalidOperationException("Invalid Activity"); |
| 88 | |
| 89 | _logger.LogInformation("Processing activity {Type} {Id}", activity.Type, activity.Id); |
| 90 | |
| 91 | if (_logger.IsEnabled(LogLevel.Trace)) |
| 92 | { |
| 93 | _logger.LogTrace("Received activity: {Activity}", activity.ToJson()); |
| 94 | } |
| 95 | |
| 96 | using (_logger.BeginScope("Processing activity {Type} {Id}", activity.Type, activity.Id)) |
| 97 | { |
| 98 | try |
| 99 | { |
| 100 | var token = Debugger.IsAttached ? CancellationToken.None : cancellationToken; |
| 101 | await MiddleWare.RunPipelineAsync(this, activity, this.OnActivity, 0, token).ConfigureAwait(false); |
| 102 | |
| 103 | } |
| 104 | catch (Exception ex) |
| 105 | { |
| 106 | _logger.LogError(ex, "Error processing activity {Type} {Id}", activity.Type, activity.Id); |
| 107 | throw new BotHandlerException("Error processing activity", ex, activity); |
| 108 | } |
| 109 | finally |
| 110 | { |
| 111 | _logger.LogInformation("Finished processing activity {Type} {Id}", activity.Type, activity.Id); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// <summary> |
| 117 | /// Adds the specified turn middleware to the middleware pipeline. |
| 118 | /// </summary> |
| 119 | /// <param name="middleware">The middleware component to add to the pipeline. Cannot be null.</param> |
| 120 | /// <returns>An ITurnMiddleWare instance representing the updated middleware pipeline.</returns> |
| 121 | public ITurnMiddleWare Use(ITurnMiddleWare middleware) |
| 122 | { |
| 123 | MiddleWare.Use(middleware); |
| 124 | return MiddleWare; |
| 125 | } |
| 126 | |
| 127 | /// <summary> |
| 128 | /// Sends the specified activity to the conversation asynchronously. |
| 129 | /// </summary> |
| 130 | /// <param name="activity">The activity to send to the conversation. Cannot be null.</param> |
| 131 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param> |
| 132 | /// <returns>A task that represents the asynchronous operation. The task result contains the identifier of the sent activity.</returns> |
| 133 | /// <exception cref="Exception">Thrown if the conversation client has not been initialized.</exception> |
| 134 | public async Task<SendActivityResponse?> SendActivityAsync(CoreActivity activity, CancellationToken cancellationToken = default) |
| 135 | { |
| 136 | ArgumentNullException.ThrowIfNull(activity); |
| 137 | ArgumentNullException.ThrowIfNull(_conversationClient, "ConversationClient not initialized"); |
| 138 | |
| 139 | return await _conversationClient.SendActivityAsync(activity, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 140 | } |
| 141 | |
| 142 | /// <summary> |
| 143 | /// Gets the version of the SDK. |
| 144 | /// </summary> |
| 145 | public static string Version => ThisAssembly.NuGetPackageVersion; |
| 146 | } |
| 147 | |