microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs
151lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | using System.Text.Json; |
| 6 | |
| 7 | using Microsoft.Teams.Api.Activities; |
| 8 | using Microsoft.Teams.Common.Http; |
| 9 | |
| 10 | namespace Microsoft.Teams.Api.Clients; |
| 11 | |
| 12 | public class ActivityClient : Client |
| 13 | { |
| 14 | public readonly string ServiceUrl; |
| 15 | |
| 16 | public ActivityClient(string serviceUrl, CancellationToken cancellationToken = default) : base(cancellationToken) |
| 17 | { |
| 18 | ServiceUrl = serviceUrl; |
| 19 | } |
| 20 | |
| 21 | public ActivityClient(string serviceUrl, IHttpClient client, CancellationToken cancellationToken = default) : base(client, cancellationToken) |
| 22 | { |
| 23 | ServiceUrl = serviceUrl; |
| 24 | } |
| 25 | |
| 26 | public ActivityClient(string serviceUrl, IHttpClientOptions options, CancellationToken cancellationToken = default) : base(options, cancellationToken) |
| 27 | { |
| 28 | ServiceUrl = serviceUrl; |
| 29 | } |
| 30 | |
| 31 | public ActivityClient(string serviceUrl, IHttpClientFactory factory, CancellationToken cancellationToken = default) : base(factory, cancellationToken) |
| 32 | { |
| 33 | ServiceUrl = serviceUrl; |
| 34 | } |
| 35 | |
| 36 | public async Task<Resource?> CreateAsync(string conversationId, IActivity activity) |
| 37 | { |
| 38 | var req = HttpRequest.Post( |
| 39 | $"{ServiceUrl}v3/conversations/{conversationId}/activities", |
| 40 | body: activity |
| 41 | ); |
| 42 | |
| 43 | var res = await _http.SendAsync(req, _cancellationToken); |
| 44 | |
| 45 | if (res.Body == string.Empty) return null; |
| 46 | |
| 47 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 48 | return body; |
| 49 | } |
| 50 | |
| 51 | public async Task<Resource?> UpdateAsync(string conversationId, string id, IActivity activity) |
| 52 | { |
| 53 | var req = HttpRequest.Put( |
| 54 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}", |
| 55 | body: activity |
| 56 | ); |
| 57 | |
| 58 | var res = await _http.SendAsync(req, _cancellationToken); |
| 59 | |
| 60 | if (res.Body == string.Empty) return null; |
| 61 | |
| 62 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 63 | return body; |
| 64 | } |
| 65 | |
| 66 | public async Task<Resource?> ReplyAsync(string conversationId, string id, IActivity activity) |
| 67 | { |
| 68 | activity.ReplyToId = id; |
| 69 | var req = HttpRequest.Post( |
| 70 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}", |
| 71 | body: activity |
| 72 | ); |
| 73 | |
| 74 | var res = await _http.SendAsync(req, _cancellationToken); |
| 75 | |
| 76 | if (res.Body == string.Empty) return null; |
| 77 | |
| 78 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 79 | return body; |
| 80 | } |
| 81 | |
| 82 | public async Task DeleteAsync(string conversationId, string id) |
| 83 | { |
| 84 | var req = HttpRequest.Delete( |
| 85 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}" |
| 86 | ); |
| 87 | |
| 88 | await _http.SendAsync(req, _cancellationToken); |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
| 92 | /// Create a new targeted activity in a conversation. |
| 93 | /// Targeted activities are only visible to the specified recipient. |
| 94 | /// </summary> |
| 95 | /// <param name="conversationId">The ID of the conversation</param> |
| 96 | /// <param name="activity">The activity to create</param> |
| 97 | /// <returns>The created activity resource</returns> |
| 98 | [Experimental("ExperimentalTeamsTargeted")] |
| 99 | public async Task<Resource?> CreateTargetedAsync(string conversationId, IActivity activity) |
| 100 | { |
| 101 | var req = HttpRequest.Post( |
| 102 | $"{ServiceUrl}v3/conversations/{conversationId}/activities?isTargetedActivity=true", |
| 103 | body: activity |
| 104 | ); |
| 105 | |
| 106 | var res = await _http.SendAsync(req, _cancellationToken); |
| 107 | |
| 108 | if (res.Body == string.Empty) return null; |
| 109 | |
| 110 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 111 | return body; |
| 112 | } |
| 113 | |
| 114 | /// <summary> |
| 115 | /// Update an existing targeted activity in a conversation. |
| 116 | /// </summary> |
| 117 | /// <param name="conversationId">The ID of the conversation</param> |
| 118 | /// <param name="id">The ID of the activity to update</param> |
| 119 | /// <param name="activity">The updated activity data</param> |
| 120 | /// <returns>The updated activity resource</returns> |
| 121 | [Experimental("ExperimentalTeamsTargeted")] |
| 122 | public async Task<Resource?> UpdateTargetedAsync(string conversationId, string id, IActivity activity) |
| 123 | { |
| 124 | var req = HttpRequest.Put( |
| 125 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}?isTargetedActivity=true", |
| 126 | body: activity |
| 127 | ); |
| 128 | |
| 129 | var res = await _http.SendAsync(req, _cancellationToken); |
| 130 | |
| 131 | if (res.Body == string.Empty) return null; |
| 132 | |
| 133 | var body = JsonSerializer.Deserialize<Resource>(res.Body); |
| 134 | return body; |
| 135 | } |
| 136 | |
| 137 | /// <summary> |
| 138 | /// Delete a targeted activity from a conversation. |
| 139 | /// </summary> |
| 140 | /// <param name="conversationId">The ID of the conversation</param> |
| 141 | /// <param name="id">The ID of the activity to delete</param> |
| 142 | [Experimental("ExperimentalTeamsTargeted")] |
| 143 | public async Task DeleteTargetedAsync(string conversationId, string id) |
| 144 | { |
| 145 | var req = HttpRequest.Delete( |
| 146 | $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}?isTargetedActivity=true" |
| 147 | ); |
| 148 | |
| 149 | await _http.SendAsync(req, _cancellationToken); |
| 150 | } |
| 151 | } |