microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Bot.Core/ConversationClient.cs
76lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Net.Http.Json; |
| 5 | using System.Net.Mime; |
| 6 | using System.Text; |
| 7 | using System.Text.Json; |
| 8 | using Microsoft.Bot.Core.Hosting; |
| 9 | using Microsoft.Bot.Core.Schema; |
| 10 | |
| 11 | namespace Microsoft.Bot.Core; |
| 12 | |
| 13 | /// <summary> |
| 14 | /// Provides methods for sending activities to a conversation endpoint using HTTP requests. |
| 15 | /// </summary> |
| 16 | /// <param name="httpClient">The HTTP client instance used to send requests to the conversation service. Must not be null.</param> |
| 17 | public class ConversationClient(HttpClient httpClient) |
| 18 | { |
| 19 | internal const string ConversationHttpClientName = "BotConversationClient"; |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Sends the specified activity to the conversation endpoint asynchronously. |
| 23 | /// </summary> |
| 24 | /// <param name="activity">The activity to send. Cannot be null. The activity must contain valid conversation and service URL information.</param> |
| 25 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the send operation.</param> |
| 26 | /// <returns>A task that represents the asynchronous operation. The task result contains the response content as a string if |
| 27 | /// the activity is sent successfully.</returns> |
| 28 | /// <exception cref="Exception">Thrown if the activity could not be sent successfully. The exception message includes the HTTP status code and |
| 29 | /// response content.</exception> |
| 30 | public async Task<ResourceResponse> SendActivityAsync(CoreActivity activity, CancellationToken cancellationToken = default) |
| 31 | { |
| 32 | ArgumentNullException.ThrowIfNull(activity); |
| 33 | ArgumentNullException.ThrowIfNull(activity.Conversation); |
| 34 | ArgumentNullException.ThrowIfNullOrWhiteSpace(activity.Conversation.Id); |
| 35 | ArgumentNullException.ThrowIfNull(activity.ServiceUrl); |
| 36 | |
| 37 | string url = $"{activity.ServiceUrl.ToString().TrimEnd('/')}/v3/conversations/{activity.Conversation.Id}/activities/"; |
| 38 | |
| 39 | using StringContent content = new(activity.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json); |
| 40 | |
| 41 | using HttpRequestMessage request = new(HttpMethod.Post, url) { Content = content }; |
| 42 | |
| 43 | request.Options.Set(BotAuthenticationHandler.AgenticIdentityKey, AgenticIdentity.FromProperties(activity.From.Properties)); |
| 44 | |
| 45 | using HttpResponseMessage resp = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 46 | |
| 47 | if (resp.IsSuccessStatusCode) |
| 48 | { |
| 49 | string responseString = await resp.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); |
| 50 | if (responseString.Length > 2) // to handle empty response |
| 51 | { |
| 52 | ResourceResponse? resourceResponse = JsonSerializer.Deserialize<ResourceResponse>(responseString); |
| 53 | return resourceResponse ?? new ResourceResponse(); |
| 54 | } |
| 55 | return new ResourceResponse(); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | string errResponseString = await resp.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); |
| 60 | throw new HttpRequestException($"Error sending activity {resp.StatusCode}. {errResponseString}"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /// <summary> |
| 67 | /// Resource Response |
| 68 | /// </summary> |
| 69 | public class ResourceResponse |
| 70 | { |
| 71 | /// <summary> |
| 72 | /// Id of the activity |
| 73 | /// </summary> |
| 74 | [JsonPropertyName("id")] |
| 75 | public string? Id { get; set; } |
| 76 | } |
| 77 | |