microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs
89lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | using Microsoft.Teams.Api.Activities; |
| 7 | using Microsoft.Teams.Common.Http; |
| 8 | |
| 9 | namespace Microsoft.Teams.Api.Clients; |
| 10 | |
| 11 | public class ActivityClient : Client |
| 12 | { |
| 13 | public readonly string ServiceUrl; |
| 14 | |
| 15 | public ActivityClient(string serviceUrl, CancellationToken cancellationToken = default) : base(cancellationToken) |
| 16 | { |
| 17 | ServiceUrl = serviceUrl; |
| 18 | } |
| 19 | |
| 20 | public ActivityClient(string serviceUrl, IHttpClient client, CancellationToken cancellationToken = default) : base(client, cancellationToken) |
| 21 | { |
| 22 | ServiceUrl = serviceUrl; |
| 23 | } |
| 24 | |
| 25 | public ActivityClient(string serviceUrl, IHttpClientOptions options, CancellationToken cancellationToken = default) : base(options, cancellationToken) |
| 26 | { |
| 27 | ServiceUrl = serviceUrl; |
| 28 | } |
| 29 | |
| 30 | public ActivityClient(string serviceUrl, IHttpClientFactory factory, CancellationToken cancellationToken = default) : base(factory, cancellationToken) |
| 31 | { |
| 32 | ServiceUrl = serviceUrl; |
| 33 | } |
| 34 | |
| 35 | public async Task<Resource?> CreateAsync(string conversationId, IActivity activity) |
| 36 | { |
| 37 | var req = HttpRequest.Post( |
| 38 | $"{ServiceUrl}v3/conversations/{conversationId}/activities", |
| 39 | body: activity |
| 40 | ); |
| 41 | |
| 42 | var res = await _http.SendAsync(req, _cancellationToken); |
| 43 | |
| 44 | if (res.Body == string.Empty) return null; |
| 45 | |
| 46 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 47 | return body; |
| 48 | } |
| 49 | |
| 50 | public async Task<Resource?> UpdateAsync(string conversationId, string id, IActivity activity) |
| 51 | { |
| 52 | var req = HttpRequest.Put( |
| 53 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}", |
| 54 | body: activity |
| 55 | ); |
| 56 | |
| 57 | var res = await _http.SendAsync(req, _cancellationToken); |
| 58 | |
| 59 | if (res.Body == string.Empty) return null; |
| 60 | |
| 61 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 62 | return body; |
| 63 | } |
| 64 | |
| 65 | public async Task<Resource?> ReplyAsync(string conversationId, string id, IActivity activity) |
| 66 | { |
| 67 | activity.ReplyToId = id; |
| 68 | var req = HttpRequest.Post( |
| 69 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}", |
| 70 | body: activity |
| 71 | ); |
| 72 | |
| 73 | var res = await _http.SendAsync(req, _cancellationToken); |
| 74 | |
| 75 | if (res.Body == string.Empty) return null; |
| 76 | |
| 77 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 78 | return body; |
| 79 | } |
| 80 | |
| 81 | public async Task DeleteAsync(string conversationId, string id) |
| 82 | { |
| 83 | var req = HttpRequest.Delete( |
| 84 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}" |
| 85 | ); |
| 86 | |
| 87 | await _http.SendAsync(req, _cancellationToken); |
| 88 | } |
| 89 | } |