microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix/msal-cache

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs

161lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Diagnostics.CodeAnalysis;
5using System.Text.Json;
6
7using Microsoft.Teams.Api.Activities;
8using Microsoft.Teams.Common.Http;
9
10namespace Microsoft.Teams.Api.Clients;
11
12public 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, CancellationToken cancellationToken = default)
37 {
38 var token = cancellationToken != default ? cancellationToken : _cancellationToken;
39 var req = HttpRequest.Post(
40 $"{ServiceUrl}v3/conversations/{conversationId}/activities",
41 body: activity
42 );
43
44 var res = await _http.SendAsync(req, token).ConfigureAwait(false);
45
46 if (res.Body == string.Empty) return null;
47
48 var body = JsonSerializer.Deserialize<Resource>(res.Body);
49 return body;
50 }
51
52 public async Task<Resource?> UpdateAsync(string conversationId, string id, IActivity activity, CancellationToken cancellationToken = default)
53 {
54 var token = cancellationToken != default ? cancellationToken : _cancellationToken;
55 var req = HttpRequest.Put(
56 $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}",
57 body: activity
58 );
59
60 var res = await _http.SendAsync(req, token).ConfigureAwait(false);
61
62 if (res.Body == string.Empty) return null;
63
64 var body = JsonSerializer.Deserialize<Resource>(res.Body);
65 return body;
66 }
67
68 public async Task<Resource?> ReplyAsync(string conversationId, string id, IActivity activity, CancellationToken cancellationToken = default)
69 {
70 var token = cancellationToken != default ? cancellationToken : _cancellationToken;
71 activity.ReplyToId = id;
72 var req = HttpRequest.Post(
73 $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}",
74 body: activity
75 );
76
77 var res = await _http.SendAsync(req, token).ConfigureAwait(false);
78
79 if (res.Body == string.Empty) return null;
80
81 var body = JsonSerializer.Deserialize<Resource>(res.Body);
82 return body;
83 }
84
85 public async Task DeleteAsync(string conversationId, string id, CancellationToken cancellationToken = default)
86 {
87 var token = cancellationToken != default ? cancellationToken : _cancellationToken;
88 var req = HttpRequest.Delete(
89 $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}"
90 );
91
92 await _http.SendAsync(req, token).ConfigureAwait(false);
93 }
94
95 /// <summary>
96 /// Create a new targeted activity in a conversation.
97 /// Targeted activities are only visible to the specified recipient.
98 /// </summary>
99 /// <param name="conversationId">The ID of the conversation</param>
100 /// <param name="activity">The activity to create</param>
101 /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
102 /// <returns>The created activity resource</returns>
103 [Experimental("ExperimentalTeamsTargeted")]
104 public async Task<Resource?> CreateTargetedAsync(string conversationId, IActivity activity, CancellationToken cancellationToken = default)
105 {
106 var token = cancellationToken != default ? cancellationToken : _cancellationToken;
107 var req = HttpRequest.Post(
108 $"{ServiceUrl}v3/conversations/{conversationId}/activities?isTargetedActivity=true",
109 body: activity
110 );
111
112 var res = await _http.SendAsync(req, token).ConfigureAwait(false);
113
114 if (res.Body == string.Empty) return null;
115
116 var body = JsonSerializer.Deserialize<Resource>(res.Body);
117 return body;
118 }
119
120 /// <summary>
121 /// Update an existing targeted activity in a conversation.
122 /// </summary>
123 /// <param name="conversationId">The ID of the conversation</param>
124 /// <param name="id">The ID of the activity to update</param>
125 /// <param name="activity">The updated activity data</param>
126 /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
127 /// <returns>The updated activity resource</returns>
128 [Experimental("ExperimentalTeamsTargeted")]
129 public async Task<Resource?> UpdateTargetedAsync(string conversationId, string id, IActivity activity, CancellationToken cancellationToken = default)
130 {
131 var token = cancellationToken != default ? cancellationToken : _cancellationToken;
132 var req = HttpRequest.Put(
133 $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}?isTargetedActivity=true",
134 body: activity
135 );
136
137 var res = await _http.SendAsync(req, token).ConfigureAwait(false);
138
139 if (res.Body == string.Empty) return null;
140
141 var body = JsonSerializer.Deserialize<Resource>(res.Body);
142 return body;
143 }
144
145 /// <summary>
146 /// Delete a targeted activity from a conversation.
147 /// </summary>
148 /// <param name="conversationId">The ID of the conversation</param>
149 /// <param name="id">The ID of the activity to delete</param>
150 /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
151 [Experimental("ExperimentalTeamsTargeted")]
152 public async Task DeleteTargetedAsync(string conversationId, string id, CancellationToken cancellationToken = default)
153 {
154 var token = cancellationToken != default ? cancellationToken : _cancellationToken;
155 var req = HttpRequest.Delete(
156 $"{ServiceUrl}v3/conversations/{conversationId}/activities/{id}?isTargetedActivity=true"
157 );
158
159 await _http.SendAsync(req, token).ConfigureAwait(false);
160 }
161}