microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.0-preview.6

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

163lines · modecode

1
2using Microsoft.Teams.Api;
3using Microsoft.Teams.Api.Activities;
4using Microsoft.Teams.Api.Auth;
5using Microsoft.Teams.Apps.Events;
6using Microsoft.Teams.Apps.Plugins;
7using Microsoft.Teams.Apps.Testing.Events;
8
9namespace Microsoft.Teams.Apps.Testing.Plugins;
10
11/// <summary>
12/// a plugin used to test any App implementation
13/// </summary>
14[Plugin(Name = "test")]
15public partial class TestPlugin : ISenderPlugin
16{
17 public event EventFunction Events;
18
19 protected Action<App>? OnInitHandler { get; set; }
20 protected Action<App>? OnStartHandler { get; set; }
21 protected Action<App, IPlugin, ErrorEvent>? OnErrorHandler { get; set; }
22 protected Action<App, ISenderPlugin, ActivityEvent>? OnActivityHandler { get; set; }
23 protected Action<App, ISenderPlugin, ActivityResponseEvent>? OnActivityResponseHandler { get; set; }
24 protected Action<App, ISenderPlugin, ActivitySentEvent>? OnActivitySentHandler { get; set; }
25
26 public TestPlugin WithInit(Action<App> handler)
27 {
28 OnInitHandler = handler;
29 return this;
30 }
31
32 public TestPlugin WithError(Action<App, IPlugin, ErrorEvent> handler)
33 {
34 OnErrorHandler = handler;
35 return this;
36 }
37
38 public TestPlugin WithActivity(Action<App, ISenderPlugin, ActivityEvent> handler)
39 {
40 OnActivityHandler = handler;
41 return this;
42 }
43
44 public TestPlugin WithActivityResponse(Action<App, ISenderPlugin, ActivityResponseEvent> handler)
45 {
46 OnActivityResponseHandler = handler;
47 return this;
48 }
49
50 public TestPlugin WithActivitySent(Action<App, ISenderPlugin, ActivitySentEvent> handler)
51 {
52 OnActivitySentHandler = handler;
53 return this;
54 }
55
56 public Task OnInit(App app, CancellationToken cancellationToken = default)
57 {
58 if (OnInitHandler is not null)
59 {
60 OnInitHandler(app);
61 }
62
63 return Task.CompletedTask;
64 }
65
66 public Task OnStart(App app, CancellationToken cancellationToken = default)
67 {
68 if (OnStartHandler is not null)
69 {
70 OnStartHandler(app);
71 }
72
73 return Task.CompletedTask;
74 }
75
76 public Task OnError(App app, IPlugin plugin, ErrorEvent @event, CancellationToken cancellationToken = default)
77 {
78 if (OnErrorHandler is not null)
79 {
80 OnErrorHandler(app, plugin, @event);
81 }
82
83 return Task.CompletedTask;
84 }
85
86 public Task OnActivity(App app, ISenderPlugin sender, ActivityEvent @event, CancellationToken cancellationToken = default)
87 {
88 if (OnActivityHandler is not null)
89 {
90 OnActivityHandler(app, sender, @event);
91 }
92
93 return Task.CompletedTask;
94 }
95
96 public Task OnActivityResponse(App app, ISenderPlugin sender, ActivityResponseEvent @event, CancellationToken cancellationToken = default)
97 {
98 if (OnActivityResponseHandler is not null)
99 {
100 OnActivityResponseHandler(app, sender, @event);
101 }
102
103 return Task.CompletedTask;
104 }
105
106 public Task OnActivitySent(App app, ISenderPlugin sender, ActivitySentEvent @event, CancellationToken cancellationToken = default)
107 {
108 if (OnActivitySentHandler is not null)
109 {
110 OnActivitySentHandler(app, sender, @event);
111 }
112
113 return Task.CompletedTask;
114 }
115
116 public Task<IActivity> Send(IActivity activity, ConversationReference reference, CancellationToken cancellationToken = default)
117 {
118 return Task.FromResult(activity);
119 }
120
121 public Task<TActivity> Send<TActivity>(TActivity activity, ConversationReference reference, CancellationToken cancellationToken = default) where TActivity : IActivity
122 {
123 return Task.FromResult(activity);
124 }
125
126 public IStreamer CreateStream(ConversationReference reference, CancellationToken cancellationToken = default)
127 {
128 return new Stream();
129 }
130
131 public async Task<Response> Do(IToken token, IActivity activity, CancellationToken cancellationToken = default)
132 {
133 if (activity is MessageActivity message)
134 {
135 await Events(
136 this,
137 "message",
138 new TestMessageEvent() { Message = message.Text },
139 cancellationToken
140 );
141 }
142
143 var @out = await Events(
144 this,
145 "activity",
146 new ActivityEvent()
147 {
148 Token = token,
149 Activity = activity
150 },
151 cancellationToken
152 );
153
154 var res = (Response?)@out;
155
156 if (res is null)
157 {
158 throw new Exception("expected activity response");
159 }
160
161 return res;
162 }
163}