microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/move-activity-classes-to-core-again

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/src/Microsoft.Bot.Core.Compat/CompatAdapter.cs

123lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using Microsoft.AspNetCore.Http;
5using Microsoft.Bot.Builder;
6using Microsoft.Bot.Builder.Integration.AspNet.Core;
7using Microsoft.Bot.Core.Schema;
8using Microsoft.Bot.Schema;
9
10
11namespace Microsoft.Bot.Core.Compat;
12
13/// <summary>
14/// Provides a compatibility adapter for processing bot activities and HTTP requests using legacy middleware and bot
15/// framework interfaces.
16/// </summary>
17/// <remarks>Use this adapter to bridge between legacy bot framework middleware and newer bot application models.
18/// The adapter allows registration of middleware and error handling delegates, and supports processing HTTP requests
19/// and continuing conversations. Thread safety is not guaranteed; instances should not be shared across concurrent
20/// requests.</remarks>
21/// <param name="botApplication">The bot application instance that handles activity processing and manages user token operations.</param>
22/// <param name="compatBotAdapter">The underlying bot adapter used to interact with the bot framework and create turn contexts.</param>
23public class CompatAdapter(BotApplication botApplication, CompatBotAdapter compatBotAdapter) : IBotFrameworkHttpAdapter
24{
25 /// <summary>
26 /// Gets the collection of middleware components configured for the application.
27 /// </summary>
28 /// <remarks>Use this property to access or inspect the set of middleware that will be invoked during
29 /// request processing. The returned collection is read-only and reflects the current middleware pipeline.</remarks>
30 public MiddlewareSet MiddlewareSet { get; } = new MiddlewareSet();
31
32 /// <summary>
33 /// Gets or sets the error handling callback to be invoked when an exception occurs during a turn.
34 /// </summary>
35 /// <remarks>Assign a delegate to customize how errors are handled within the bot's turn processing. The
36 /// callback receives the current turn context and the exception that was thrown. If not set, unhandled exceptions
37 /// may propagate and result in default error behavior. This property is typically used to log errors, send
38 /// user-friendly messages, or perform cleanup actions.</remarks>
39 public Func<ITurnContext, Exception, Task>? OnTurnError { get; set; }
40
41 /// <summary>
42 /// Adds the specified middleware to the adapter's processing pipeline.
43 /// </summary>
44 /// <param name="middleware">The middleware component to be invoked during request processing. Cannot be null.</param>
45 /// <returns>The current <see cref="CompatAdapter"/> instance, enabling method chaining.</returns>
46 public CompatAdapter Use(Builder.IMiddleware middleware)
47 {
48 MiddlewareSet.Use(middleware);
49 return this;
50 }
51
52 /// <summary>
53 /// Processes an incoming HTTP request and generates an appropriate HTTP response using the provided bot instance.
54 /// </summary>
55 /// <param name="httpRequest"></param>
56 /// <param name="httpResponse"></param>
57 /// <param name="bot"></param>
58 /// <param name="cancellationToken"></param>
59 /// <returns></returns>
60 public async Task ProcessAsync(HttpRequest httpRequest, HttpResponse httpResponse, IBot bot, CancellationToken cancellationToken = default)
61 {
62 ArgumentNullException.ThrowIfNull(httpRequest);
63 ArgumentNullException.ThrowIfNull(bot);
64
65 CoreActivity? activity = null;
66 botApplication.OnActivity = (activity, cancellationToken1) =>
67 {
68 TurnContext turnContext = new(compatBotAdapter, activity.ToCompatActivity());
69 //turnContext.TurnState.Add<Connector.Authentication.UserTokenClient>(new CompatUserTokenClient(botApplication.UserTokenClient));
70 return bot.OnTurnAsync(turnContext, cancellationToken1);
71 };
72 try
73 {
74 foreach (Builder.IMiddleware? middleware in MiddlewareSet)
75 {
76 botApplication.Use(new CompatMiddlewareAdapter(middleware));
77 }
78
79 activity = await botApplication.ProcessAsync(httpRequest.HttpContext, cancellationToken).ConfigureAwait(false);
80 }
81 catch (Exception ex)
82 {
83 if (OnTurnError != null)
84 {
85 if (ex is BotHandlerException aex)
86 {
87 activity = aex.Activity;
88 using TurnContext turnContext = new(compatBotAdapter, activity!.ToCompatActivity());
89 await OnTurnError(turnContext, ex).ConfigureAwait(false);
90 }
91 else
92 {
93 throw;
94 }
95 }
96 else
97 {
98 throw;
99 }
100 }
101 }
102
103 /// <summary>
104 /// Continues an existing bot conversation by invoking the specified callback with the provided conversation
105 /// reference.
106 /// </summary>
107 /// <remarks>Use this method to resume a conversation at a specific point, such as in response to an event
108 /// or proactive message. The callback is executed within the context of the continued conversation.</remarks>
109 /// <param name="botId">The unique identifier of the bot participating in the conversation.</param>
110 /// <param name="reference">A reference to the conversation to continue. Must not be null.</param>
111 /// <param name="callback">A delegate that handles the bot logic for the continued conversation. The callback receives a turn context and
112 /// cancellation token.</param>
113 /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
114 /// <returns>A task that represents the asynchronous operation.</returns>
115 public async Task ContinueConversationAsync(string botId, ConversationReference reference, BotCallbackHandler callback, CancellationToken cancellationToken)
116 {
117 ArgumentNullException.ThrowIfNull(reference);
118 ArgumentNullException.ThrowIfNull(callback);
119
120 using TurnContext turnContext = new(compatBotAdapter, reference.GetContinuationActivity());
121 await callback(turnContext, cancellationToken).ConfigureAwait(false);
122 }
123}
124