microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Samples/Samples.Graph/Program.cs
61lines · modeblame
e37420d2Kavin1 years ago | 1 | using Microsoft.Teams.Apps; |
| 2 | using Microsoft.Teams.Apps.Activities; | |
77ea35b6Alex Acebo1 years ago | 3 | using Microsoft.Teams.Apps.Events; |
e37420d2Kavin1 years ago | 4 | using Microsoft.Teams.Apps.Extensions; |
928f1d86Kavin11 months ago | 5 | using Microsoft.Teams.Extensions.Graph; |
fb2fb20cAlex Acebo9 months ago | 6 | using Microsoft.Teams.Plugins.AspNetCore.Extensions; |
e37420d2Kavin1 years ago | 7 | |
| 8 | var builder = WebApplication.CreateBuilder(args); | |
| 9 | | |
| 10 | var 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 | | |
| 16 | builder.AddTeams(appBuilder); | |
| 17 | | |
| 18 | var app = builder.Build(); | |
| 19 | var teams = app.UseTeams(); | |
| 20 | | |
| 21 | teams.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 | | |
| 33 | teams.OnMessage(async context => | |
| 34 | { | |
| 35 | if (!context.IsSignedIn) | |
| 36 | { | |
901704ddAlex Acebo9 months ago | 37 | await context.SignIn(new OAuthOptions() |
e37420d2Kavin1 years ago | 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 | | |
928f1d86Kavin11 months ago | 47 | // If user is not signed in then `GetUserGraphClient` will throw an exception |
| 48 | var me = await context.GetUserGraphClient().Me.GetAsync(); | |
e37420d2Kavin1 years ago | 49 | await context.Send($"user '{me!.DisplayName}' is already signed in!"); |
| 50 | }); | |
| 51 | | |
5a1c7318Alex Acebo1 years ago | 52 | teams.OnSignIn(async (_, @event) => |
| 53 | { | |
e37420d2Kavin1 years ago | 54 | var token = @event.Token; |
| 55 | var context = @event.Context; | |
| 56 | | |
928f1d86Kavin11 months ago | 57 | var me = await context.GetUserGraphClient().Me.GetAsync(); |
e37420d2Kavin1 years ago | 58 | await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}"); |
| 59 | }); | |
| 60 | | |
| 61 | app.Run(); |