microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
samples/migration-bot

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Apps/Handlers/TaskModules/TaskModuleResponse.cs

260lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text.Json.Serialization;
5
6namespace Microsoft.Teams.Bot.Apps.Handlers.TaskModules;
7
8/// <summary>
9/// Task module response types.
10/// </summary>
11public static class TaskModuleResponseType
12{
13 /// <summary>
14 /// Continue type - displays a card or URL in the task module.
15 /// </summary>
16 public const string Continue = "continue";
17
18 /// <summary>
19 /// Message type - displays a plain text message.
20 /// </summary>
21 public const string Message = "message";
22}
23
24/// <summary>
25/// Task module size constants.
26/// </summary>
27public static class TaskModuleSize
28{
29 /// <summary>
30 /// Small size.
31 /// </summary>
32 public const string Small = "small";
33
34 /// <summary>
35 /// Medium size.
36 /// </summary>
37 public const string Medium = "medium";
38
39 /// <summary>
40 /// Large size.
41 /// </summary>
42 public const string Large = "large";
43}
44
45/// <summary>
46/// Task module response wrapper.
47/// </summary>
48public class TaskModuleResponse
49{
50 /// <summary>
51 /// The task module result.
52 /// </summary>
53 [JsonPropertyName("task")]
54 public Response? Task { get; set; }
55
56 /// <summary>
57 /// Creates a new builder for TaskModuleResponse.
58 /// </summary>
59 public static TaskModuleResponseBuilder CreateBuilder()
60 {
61 return new TaskModuleResponseBuilder();
62 }
63}
64
65/// <summary>
66/// Builder for TaskModuleResponse.
67/// </summary>
68public class TaskModuleResponseBuilder
69{
70 private string? _type;
71 private string? _title;
72 private object? _card;
73 private object _height = TaskModuleSize.Small;
74 private object _width = TaskModuleSize.Small;
75 private string? _message;
76 //private string? _url;
77 //private string? _fallbackUrl;
78 //private string? _completionBotId;
79
80 /// <summary>
81 /// Sets the type of the response. Use TaskModuleResponseType constants.
82 /// </summary>
83 public TaskModuleResponseBuilder WithType(string type)
84 {
85 _type = type;
86 return this;
87 }
88
89 /// <summary>
90 /// Sets the title of the task module.
91 /// </summary>
92 public TaskModuleResponseBuilder WithTitle(string title)
93 {
94 _title = title;
95 return this;
96 }
97
98 /// <summary>
99 /// Sets the card content for continue type.
100 /// </summary>
101 public TaskModuleResponseBuilder WithCard(object card)
102 {
103 _card = card;
104 return this;
105 }
106
107 /// <summary>
108 /// Sets the height. Can be a number (pixels) or use TaskModuleSize constants.
109 /// </summary>
110 public TaskModuleResponseBuilder WithHeight(object height)
111 {
112 _height = height;
113 return this;
114 }
115
116 /// <summary>
117 /// Sets the width. Can be a number (pixels) or use TaskModuleSize constants.
118 /// </summary>
119 public TaskModuleResponseBuilder WithWidth(object width)
120 {
121 _width = width;
122 return this;
123 }
124
125 /// <summary>
126 /// Sets the message for message type.
127 /// </summary>
128 public TaskModuleResponseBuilder WithMessage(string message)
129 {
130 _message = message;
131 return this;
132 }
133
134 /*
135 /// <summary>
136 /// Sets the URL for continue type.
137 /// </summary>
138 public TaskModuleResponseBuilder WithUrl(string url)
139 {
140 _url = url;
141 return this;
142 }
143
144 /// <summary>
145 /// Sets the fallback URL if the card cannot be displayed.
146 /// </summary>
147 public TaskModuleResponseBuilder WithFallbackUrl(string fallbackUrl)
148 {
149 _fallbackUrl = fallbackUrl;
150 return this;
151 }
152
153 /// <summary>
154 /// Sets the completion bot ID.
155 /// </summary>
156 public TaskModuleResponseBuilder WithCompletionBotId(string completionBotId)
157 {
158 _completionBotId = completionBotId;
159 return this;
160 }
161 */
162
163 /// <summary>
164 /// Builds the TaskModuleResponse.
165 /// </summary>
166 internal TaskModuleResponse Validate()
167 {
168 if (string.IsNullOrEmpty(_type))
169 {
170 throw new InvalidOperationException("Type must be set. Use WithType() to specify TaskModuleResponseType.Continue or TaskModuleResponseType.Message.");
171 }
172
173 object? value = _type switch
174 {
175 TaskModuleResponseType.Continue => ValidateContinueType(),
176 TaskModuleResponseType.Message => ValidateMessageType(),
177 _ => throw new InvalidOperationException($"Unknown task module response type: {_type}")
178 };
179
180 return new TaskModuleResponse
181 {
182 Task = new Response
183 {
184 Type = _type,
185 Value = value
186 }
187 };
188 }
189
190 private object ValidateContinueType()
191 {
192 if (_card == null)
193 {
194 throw new InvalidOperationException("Card must be set for Continue type. Use WithCard().");
195 }
196
197 if (!string.IsNullOrEmpty(_message))
198 {
199 throw new InvalidOperationException("Message cannot be set for Continue type. Message is only used with Message type.");
200 }
201
202 return new
203 {
204 title = _title,
205 height = _height,
206 width = _width,
207 card = _card,
208 //url = _url,
209 //fallbackUrl = _fallbackUrl,
210 //completionBotId = _completionBotId
211 };
212 }
213
214 private string ValidateMessageType()
215 {
216 if (string.IsNullOrEmpty(_message))
217 {
218 throw new InvalidOperationException("Message must be set for Message type. Use WithMessage().");
219 }
220
221 if (!string.IsNullOrEmpty(_title))
222 {
223 throw new InvalidOperationException("Title cannot be set for Message type. Title is only used with Continue type.");
224 }
225
226 if (_card != null)
227 {
228 throw new InvalidOperationException("Card cannot be set for Message type. Card is only used with Continue type.");
229 }
230
231 return _message;
232 }
233
234 /// <summary>
235 /// Builds the TaskModuleResponse and wraps it in a InvokeResponse.
236 /// </summary>
237 /// <param name="statusCode">The HTTP status code (default: 200).</param>
238 public InvokeResponse<TaskModuleResponse> Build(int statusCode = 200)
239 {
240 return new InvokeResponse<TaskModuleResponse>(statusCode, Validate());
241 }
242}
243
244/// <summary>
245/// Task module result.
246/// </summary>
247public class Response
248{
249 /// <summary>
250 /// Type of result.
251 /// </summary>
252 [JsonPropertyName("type")]
253 public required string Type { get; set; }
254
255 /// <summary>
256 /// Value
257 /// </summary>
258 [JsonPropertyName("value")]
259 public object? Value { get; set; }
260}
261