microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs
103lines · 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, bool isTargeted = false) |
| 36 | { |
| 37 | var url = $"{ServiceUrl}v3/conversations/{conversationId}/activities"; |
| 38 | if (isTargeted) |
| 39 | { |
| 40 | url += "?isTargetedActivity=true"; |
| 41 | } |
| 42 | |
| 43 | var req = HttpRequest.Post(url, body: activity); |
| 44 | |
| 45 | var res = await _http.SendAsync(req, _cancellationToken); |
| 46 | |
| 47 | if (res.Body == string.Empty) return null; |
| 48 | |
| 49 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 50 | return body; |
| 51 | } |
| 52 | |
| 53 | public async Task<Resource?> UpdateAsync(string conversationId, string id, IActivity activity, bool isTargeted = false) |
| 54 | { |
| 55 | var url = $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}"; |
| 56 | if (isTargeted) |
| 57 | { |
| 58 | url += "?isTargetedActivity=true"; |
| 59 | } |
| 60 | |
| 61 | var req = HttpRequest.Put(url, body: activity); |
| 62 | |
| 63 | var res = await _http.SendAsync(req, _cancellationToken); |
| 64 | |
| 65 | if (res.Body == string.Empty) return null; |
| 66 | |
| 67 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 68 | return body; |
| 69 | } |
| 70 | |
| 71 | public async Task<Resource?> ReplyAsync(string conversationId, string id, IActivity activity, bool isTargeted = false) |
| 72 | { |
| 73 | activity.ReplyToId = id; |
| 74 | |
| 75 | var url = $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}"; |
| 76 | if (isTargeted) |
| 77 | { |
| 78 | url += "?isTargetedActivity=true"; |
| 79 | } |
| 80 | |
| 81 | var req = HttpRequest.Post(url, body: activity); |
| 82 | |
| 83 | var res = await _http.SendAsync(req, _cancellationToken); |
| 84 | |
| 85 | if (res.Body == string.Empty) return null; |
| 86 | |
| 87 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 88 | return body; |
| 89 | } |
| 90 | |
| 91 | public async Task DeleteAsync(string conversationId, string id, bool isTargeted = false) |
| 92 | { |
| 93 | var url = $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}"; |
| 94 | if (isTargeted) |
| 95 | { |
| 96 | url += "?isTargetedActivity=true"; |
| 97 | } |
| 98 | |
| 99 | var req = HttpRequest.Delete(url); |
| 100 | |
| 101 | await _http.SendAsync(req, _cancellationToken); |
| 102 | } |
| 103 | } |