microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.0-preview.5

Branches

Tags

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

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps/Events/Event.cs

60lines · modecode

1using System.Text.Json;
2using System.Text.Json.Serialization;
3
4using Microsoft.Teams.Apps.Plugins;
5
6namespace Microsoft.Teams.Apps.Events;
7
8/// <summary>
9/// the base Event payload type
10/// </summary>
11public class Event : Dictionary<string, object>
12{
13 public object? GetOrDefault(string key) => ContainsKey(key) ? this[key] : null;
14 public T? GetOrDefault<T>(string key) => (T?)GetOrDefault(key);
15
16 public object Get(string key) => this[key];
17 public T Get<T>(string key) => (T)this[key];
18
19 public override string ToString()
20 {
21 return JsonSerializer.Serialize(this, new JsonSerializerOptions()
22 {
23 WriteIndented = true,
24 DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
25 });
26 }
27}
28
29[AttributeUsage(AttributeTargets.Method, Inherited = true)]
30public class EventAttribute(string name) : Attribute
31{
32 public readonly string Name = name;
33}
34
35public static partial class AppEventExtensions
36{
37 public static App OnEvent(this App app, string name, Action<IPlugin, Event> handler)
38 {
39 app.Events.On(name, handler);
40 return app;
41 }
42
43 public static App OnEvent<TEvent>(this App app, string name, Action<IPlugin, TEvent> handler) where TEvent : Event
44 {
45 app.Events.On(name, (plugin, payload) => handler(plugin, (TEvent)payload));
46 return app;
47 }
48
49 public static App OnEvent(this App app, string name, Func<IPlugin, Event, CancellationToken, Task> handler)
50 {
51 app.Events.On(name, handler);
52 return app;
53 }
54
55 public static App OnEvent<TEvent>(this App app, string name, Func<IPlugin, TEvent, CancellationToken, Task> handler) where TEvent : Event
56 {
57 app.Events.On(name, (plugin, payload, token) => handler(plugin, (TEvent)payload, token));
58 return app;
59 }
60}