microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/msal-agentic

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

68lines · modecode

1using Microsoft.Teams.Apps;
2using Microsoft.Teams.Apps.Activities;
3using Microsoft.Teams.Apps.Events;
4using Microsoft.Teams.Apps.Extensions;
5using Microsoft.Teams.Extensions.Graph;
6using Microsoft.Teams.Plugins.AspNetCore.Extensions;
7
8var builder = WebApplication.CreateBuilder(args);
9builder.AddTeams();
10builder.Services.Configure<AppOptions>(options => options.OAuth = new OAuthSettings("graph"));
11var app = builder.Build();
12var teams = app.UseTeams();
13
14teams.Use(async context =>
15{
16 var start = DateTime.UtcNow;
17 try
18 {
19 await context.Next();
20 }
21 catch
22 {
23 context.Log.Error("error occurred during activity processing");
24 }
25 context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms");
26});
27
28teams.OnMessage("/signout", async context =>
29{
30 if (!context.IsSignedIn)
31 {
32 await context.Send("you are not signed in!");
33 return;
34 }
35
36 await context.SignOut(); // call `SignOut()` for your auth connection...
37 await context.Send("you have been signed out!");
38});
39
40teams.OnMessage(async context =>
41{
42 if (!context.IsSignedIn)
43 {
44 await context.SignIn(new OAuthOptions()
45 {
46 // Customize the OAuth card text (only applies to OAuth flow, not SSO)
47 OAuthCardText = "Sign in to your account",
48 SignInButtonText = "Sign In"
49 }); // call `SignIn() for your auth connection...
50
51 return;
52 }
53
54 // If user is not signed in then `GetUserGraphClient` will throw an exception
55 var me = await context.GetUserGraphClient().Me.GetAsync();
56 await context.Send($"user '{me!.DisplayName}' is already signed in!");
57});
58
59teams.OnSignIn(async (_, @event) =>
60{
61 var token = @event.Token;
62 var context = @event.Context;
63
64 var me = await context.GetUserGraphClient().Me.GetAsync();
65 await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}");
66});
67
68app.Run();