microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.8

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Meetings/Program.cs

130lines · modecode

1using Microsoft.Teams.Apps;
2using Microsoft.Teams.Apps.Activities;
3using Microsoft.Teams.Apps.Activities.Events;
4using Microsoft.Teams.Cards;
5using Microsoft.Teams.Common.Logging;
6using Microsoft.Teams.Plugins.AspNetCore.Extensions;
7
8var builder = WebApplication.CreateBuilder(args);
9
10var appBuilder = App.Builder()
11 .AddLogger(new ConsoleLogger(level: Microsoft.Teams.Common.Logging.LogLevel.Debug));
12
13builder.AddTeams(appBuilder);
14
15var app = builder.Build();
16var teams = app.UseTeams();
17
18teams.Use(async context =>
19{
20 var start = DateTime.UtcNow;
21 try
22 {
23 await context.Next();
24 }
25 catch (Exception e)
26 {
27 context.Log.Error(e);
28 context.Log.Error("error occurred during activity processing");
29 }
30 context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms");
31});
32
33teams.OnMeetingStart(async (context, cancellationToken) =>
34{
35 var activity = context.Activity.Value;
36 var startTime = activity.StartTime.ToLocalTime();
37 AdaptiveCard card = new AdaptiveCard
38 {
39 Schema = "http://adaptivecards.io/schemas/adaptive-card.json",
40 Body = new List<CardElement>
41 {
42 new TextBlock($"'{activity.Title}' has started at {startTime}.")
43 {
44 Wrap = true,
45 Weight = TextWeight.Bolder
46 }
47 },
48 Actions = new List<Microsoft.Teams.Cards.Action>
49 {
50 new OpenUrlAction(activity.JoinUrl)
51 {
52 Title = "Join the meeting",
53 }
54 }
55 };
56 await context.Send(card, cancellationToken);
57});
58
59teams.OnMeetingEnd(async (context, cancellationToken) =>
60{
61 var activity = context.Activity.Value;
62 var endTime = activity.EndTime.ToLocalTime();
63
64 AdaptiveCard card = new AdaptiveCard
65 {
66 Schema = "http://adaptivecards.io/schemas/adaptive-card.json",
67 Body = new List<CardElement>
68 {
69 new TextBlock($"'{activity.Title}' has ended at {endTime}.")
70 {
71 Wrap = true,
72 Weight = TextWeight.Bolder
73 }
74 }
75 };
76
77 await context.Send(card, cancellationToken);
78});
79
80teams.OnMeetingJoin(async (context, cancellationToken) =>
81{
82 var activity = context.Activity.Value;
83 var member = activity.Members[0].User.Name;
84 var role = activity.Members[0].Meeting.Role;
85
86 AdaptiveCard card = new AdaptiveCard
87 {
88 Schema = "http://adaptivecards.io/schemas/adaptive-card.json",
89 Body = new List<CardElement>
90 {
91 new TextBlock($"{member} has joined the meeting as {role}.")
92 {
93 Wrap = true,
94 Weight = TextWeight.Bolder
95 }
96 }
97 };
98
99 await context.Send(card, cancellationToken);
100
101});
102
103teams.OnMeetingLeave(async (context, cancellationToken) =>
104{
105 var activity = context.Activity.Value;
106 var member = activity.Members[0].User.Name;
107
108 AdaptiveCard card = new AdaptiveCard
109 {
110 Schema = "http://adaptivecards.io/schemas/adaptive-card.json",
111 Body = new List<CardElement>
112 {
113 new TextBlock($"{member} has left the meeting.")
114 {
115 Wrap = true,
116 Weight = TextWeight.Bolder
117 }
118 }
119 };
120
121 await context.Send(card, cancellationToken);
122});
123
124teams.OnMessage(async (context, cancellationToken) =>
125{
126 await context.Typing(cancellationToken: cancellationToken);
127 await context.Send($"you said '{context.Activity.Text}'", cancellationToken);
128});
129
130app.Run();