microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e2efe052792ba394b46f2004d1390a3cdfcd0c1e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Core/BotApplication.cs

154lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Diagnostics;
5using Microsoft.AspNetCore.Http;
6using Microsoft.Extensions.Logging;
7using Microsoft.Extensions.Logging.Abstractions;
8using Microsoft.Teams.Bot.Core.Hosting;
9using Microsoft.Teams.Bot.Core.Schema;
10
11namespace Microsoft.Teams.Bot.Core;
12
13/// <summary>
14/// Represents a bot application.
15/// </summary>
16public 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.LogInformation("Started {ThisType} listener for AppID:{AppId} with SDK version {SdkVersion}", this.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.LogInformation("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 if (_logger.IsEnabled(LogLevel.Trace))
99 {
100 _logger.LogTrace("Received activity: {Activity}", activity.ToJson());
101 }
102
103 // TODO: Replace with structured scope data, ensure it works with OpenTelemetry and other logging providers
104 using (_logger.BeginScope("ActivityType={ActivityType} ActivityId={ActivityId} ServiceUrl={ServiceUrl} MSCV={MSCV}",
105 activity.Type, activity.Id, activity.ServiceUrl, httpContext.Request.GetCorrelationVector()))
106 {
107 try
108 {
109 CancellationToken token = Debugger.IsAttached ? CancellationToken.None : cancellationToken;
110 await MiddleWare.RunPipelineAsync(this, activity, this.OnActivity, 0, token).ConfigureAwait(false);
111 }
112 catch (Exception ex)
113 {
114 _logger.LogError(ex, "Error processing activity: Id={Id}", activity.Id);
115 throw new BotHandlerException("Error processing activity", ex, activity);
116 }
117 finally
118 {
119 _logger.LogInformation("Finished processing activity: Id={Id}", activity.Id);
120 }
121 }
122 }
123
124 /// <summary>
125 /// Adds the specified turn middleware to the middleware pipeline.
126 /// </summary>
127 /// <param name="middleware">The middleware component to add to the pipeline. Cannot be null.</param>
128 /// <returns>An ITurnMiddleWare instance representing the updated middleware pipeline.</returns>
129 public ITurnMiddleware UseMiddleware(ITurnMiddleware middleware)
130 {
131 MiddleWare.Use(middleware);
132 return MiddleWare;
133 }
134
135 /// <summary>
136 /// Sends the specified activity to the conversation asynchronously.
137 /// </summary>
138 /// <param name="activity">The activity to send to the conversation. Cannot be null.</param>
139 /// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param>
140 /// <returns>A task that represents the asynchronous operation. The task result contains the identifier of the sent activity.</returns>
141 /// <exception cref="Exception">Thrown if the conversation client has not been initialized.</exception>
142 public async Task<SendActivityResponse?> SendActivityAsync(CoreActivity activity, CancellationToken cancellationToken = default)
143 {
144 ArgumentNullException.ThrowIfNull(activity);
145 ArgumentNullException.ThrowIfNull(_conversationClient, "ConversationClient not initialized");
146
147 return await _conversationClient.SendActivityAsync(activity, cancellationToken: cancellationToken).ConfigureAwait(false);
148 }
149
150 /// <summary>
151 /// Gets the version of the SDK.
152 /// </summary>
153 public static string Version => ThisAssembly.NuGetPackageVersion;
154}
155