microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a1e394606a8568e774b6fbe8930c12ce95ca9ccc

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

61lines · modecode

1using Microsoft.Teams.Apps;
2using Microsoft.Teams.Apps.Activities;
3using Microsoft.Teams.Apps.Events;
4using Microsoft.Teams.Apps.Extensions;
5using Microsoft.Teams.Plugins.AspNetCore.Extensions;
6using Microsoft.Teams.Extensions.Graph;
7
8var builder = WebApplication.CreateBuilder(args);
9
10var appBuilder = App.Builder()
11 .AddLogger(level: Microsoft.Teams.Common.Logging.LogLevel.Debug)
12 // The name of the auth connection to use.
13 // It should be the same as the OAuth connection name defined in the Azure Bot configuration.
14 .AddOAuth("graph");
15
16builder.AddTeams(appBuilder);
17
18var app = builder.Build();
19var teams = app.UseTeams();
20
21teams.OnMessage("/signout", async context =>
22{
23 if (!context.IsSignedIn)
24 {
25 await context.Send("you are not signed in!");
26 return;
27 }
28
29 await context.SignOut(); // call `SignOut()` for your auth connection...
30 await context.Send("you have been signed out!");
31});
32
33teams.OnMessage(async context =>
34{
35 if (!context.IsSignedIn)
36 {
37 await context.SignIn(new SignInOptions()
38 {
39 // Customize the OAuth card text (only applies to OAuth flow, not SSO)
40 OAuthCardText = "Sign in to your account",
41 SignInButtonText = "Sign In"
42 }); // call `SignIn() for your auth connection...
43
44 return;
45 }
46
47 // If user is not signed in then `GetUserGraphClient` will throw an exception
48 var me = await context.GetUserGraphClient().Me.GetAsync();
49 await context.Send($"user '{me!.DisplayName}' is already signed in!");
50});
51
52teams.OnSignIn(async (_, @event) =>
53{
54 var token = @event.Token;
55 var context = @event.Context;
56
57 var me = await context.GetUserGraphClient().Me.GetAsync();
58 await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}");
59});
60
61app.Run();