microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.DevTools/DevToolsConversationClient.cs
46lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Extensions.Logging; |
| 5 | using Microsoft.Teams.Bot.Core; |
| 6 | using Microsoft.Teams.Bot.Core.Schema; |
| 7 | |
| 8 | namespace Microsoft.Teams.Bot.DevTools; |
| 9 | |
| 10 | using CustomHeaders = Dictionary<string, string>; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// Decorator around <see cref="ConversationClient"/> that emits "sent" events to DevTools UI clients |
| 14 | /// whenever an activity is sent. |
| 15 | /// </summary> |
| 16 | public class DevToolsConversationClient : ConversationClient |
| 17 | { |
| 18 | private readonly DevToolsService _service; |
| 19 | |
| 20 | /// <summary> |
| 21 | /// Creates a new DevToolsConversationClient. |
| 22 | /// </summary> |
| 23 | /// <param name="httpClient">The HTTP client for sending activities.</param> |
| 24 | /// <param name="logger">The logger.</param> |
| 25 | /// <param name="service">The shared DevTools service for emitting events.</param> |
| 26 | public DevToolsConversationClient(HttpClient httpClient, ILogger<ConversationClient> logger, DevToolsService service) |
| 27 | : base(httpClient, logger) |
| 28 | { |
| 29 | _service = service; |
| 30 | } |
| 31 | |
| 32 | /// <inheritdoc/> |
| 33 | public override async Task<SendActivityResponse> SendActivityAsync(CoreActivity activity, CustomHeaders? customHeaders = null, CancellationToken cancellationToken = default) |
| 34 | { |
| 35 | ArgumentNullException.ThrowIfNull(activity); |
| 36 | var response = await base.SendActivityAsync(activity, customHeaders, cancellationToken).ConfigureAwait(false); |
| 37 | |
| 38 | // Ensure activity has an ID so the DevTools UI can distinguish messages |
| 39 | activity.Id ??= response.Id ?? Guid.NewGuid().ToString(); |
| 40 | |
| 41 | // Emit sent event after successful send |
| 42 | await _service.EmitSent(activity, cancellationToken).ConfigureAwait(false); |
| 43 | |
| 44 | return response; |
| 45 | } |
| 46 | } |
| 47 | |