microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3ddf9fa76ec1801a0e3ca312c6d9855879571ac1

Branches

Tags

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

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps.Testing/Plugins/TestPlugin.cs

195lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Net;
5
6using Microsoft.Teams.Api;
7using Microsoft.Teams.Api.Activities;
8using Microsoft.Teams.Api.Auth;
9using Microsoft.Teams.Apps.Events;
10using Microsoft.Teams.Apps.Plugins;
11using Microsoft.Teams.Apps.Testing.Events;
12
13namespace Microsoft.Teams.Apps.Testing.Plugins;
14
15/// <summary>
16/// a plugin used to test any App implementation
17/// </summary>
18[Plugin(Name = "test")]
19public partial class TestPlugin : ISenderPlugin
20{
21 public event EventFunction Events;
22
23 protected Action<App>? OnInitHandler { get; set; }
24 protected Action<App>? OnStartHandler { get; set; }
25 protected Action<App, IPlugin, ErrorEvent>? OnErrorHandler { get; set; }
26 protected Action<App, ISenderPlugin, ActivityEvent>? OnActivityHandler { get; set; }
27 protected Action<App, ISenderPlugin, ActivityResponseEvent>? OnActivityResponseHandler { get; set; }
28 protected Action<App, ISenderPlugin, ActivitySentEvent>? OnActivitySentHandler { get; set; }
29
30 public TestPlugin WithInit(Action<App> handler)
31 {
32 OnInitHandler = handler;
33 return this;
34 }
35
36 public TestPlugin WithError(Action<App, IPlugin, ErrorEvent> handler)
37 {
38 OnErrorHandler = handler;
39 return this;
40 }
41
42 public TestPlugin WithActivity(Action<App, ISenderPlugin, ActivityEvent> handler)
43 {
44 OnActivityHandler = handler;
45 return this;
46 }
47
48 public TestPlugin WithActivityResponse(Action<App, ISenderPlugin, ActivityResponseEvent> handler)
49 {
50 OnActivityResponseHandler = handler;
51 return this;
52 }
53
54 public TestPlugin WithActivitySent(Action<App, ISenderPlugin, ActivitySentEvent> handler)
55 {
56 OnActivitySentHandler = handler;
57 return this;
58 }
59
60 public Task OnInit(App app, CancellationToken cancellationToken = default)
61 {
62 if (OnInitHandler is not null)
63 {
64 OnInitHandler(app);
65 }
66
67 return Task.CompletedTask;
68 }
69
70 public Task OnStart(App app, CancellationToken cancellationToken = default)
71 {
72 if (OnStartHandler is not null)
73 {
74 OnStartHandler(app);
75 }
76
77 return Task.CompletedTask;
78 }
79
80 public Task OnError(App app, IPlugin plugin, ErrorEvent @event, CancellationToken cancellationToken = default)
81 {
82 if (OnErrorHandler is not null)
83 {
84 OnErrorHandler(app, plugin, @event);
85 }
86
87 return Task.CompletedTask;
88 }
89
90 public Task OnActivity(App app, ISenderPlugin sender, ActivityEvent @event, CancellationToken cancellationToken = default)
91 {
92 if (OnActivityHandler is not null)
93 {
94 OnActivityHandler(app, sender, @event);
95 }
96
97 return Task.CompletedTask;
98 }
99
100 public Task OnActivityResponse(App app, ISenderPlugin sender, ActivityResponseEvent @event, CancellationToken cancellationToken = default)
101 {
102 if (OnActivityResponseHandler is not null)
103 {
104 OnActivityResponseHandler(app, sender, @event);
105 }
106
107 return Task.CompletedTask;
108 }
109
110 public Task OnActivitySent(App app, ISenderPlugin sender, ActivitySentEvent @event, CancellationToken cancellationToken = default)
111 {
112 if (OnActivitySentHandler is not null)
113 {
114 OnActivitySentHandler(app, sender, @event);
115 }
116
117 return Task.CompletedTask;
118 }
119
120 public Task<IActivity> Send(IActivity activity, ConversationReference reference, CancellationToken cancellationToken = default)
121 {
122 return Send<IActivity>(activity, reference, cancellationToken);
123 }
124
125 public Task<TActivity> Send<TActivity>(TActivity activity, ConversationReference reference, CancellationToken cancellationToken = default) where TActivity : IActivity
126 {
127 #pragma warning disable ExperimentalTeamsTargeted
128 if (activity.Recipient?.IsTargeted == true && reference.Conversation.Type?.IsPersonal == true)
129 {
130 throw new InvalidOperationException(
131 "Targeted messages are not supported in personal (1:1) chats.");
132 }
133 #pragma warning restore ExperimentalTeamsTargeted
134
135 return Task.FromResult(activity);
136 }
137
138 public IStreamer CreateStream(ConversationReference reference, CancellationToken cancellationToken = default)
139 {
140 return new Stream();
141 }
142
143 public async Task<Response> Do(IToken token, IActivity activity, IDictionary<string, object?>? extra = null, CancellationToken cancellationToken = default)
144 {
145 return await Do(new()
146 {
147 Token = token,
148 Activity = activity,
149 Extra = extra
150 }, cancellationToken);
151 }
152
153 public async Task<Response> Do(ActivityEvent @event, CancellationToken cancellationToken = default)
154 {
155 try
156 {
157 if (@event.Activity is MessageActivity message)
158 {
159 await Events(
160 this,
161 "message",
162 new TestMessageEvent() { Message = message.Text },
163 cancellationToken
164 );
165 }
166
167 var @out = await Events(
168 this,
169 EventType.Activity,
170 @event,
171 cancellationToken
172 );
173
174 var res = (Response?)@out;
175
176 if (res is null)
177 {
178 throw new Exception("expected activity response");
179 }
180
181 return res;
182 }
183 catch (Exception ex)
184 {
185 await Events(
186 this,
187 EventType.Error,
188 new ErrorEvent() { Exception = ex },
189 cancellationToken
190 );
191
192 return new(HttpStatusCode.InternalServerError, ex.ToString());
193 }
194 }
195}