microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Samples/Samples.Graph/Program.cs
84lines · modeblame
e37420d2Kavin1 years ago | 1 | using Microsoft.Teams.Apps; |
| 2 | using Microsoft.Teams.Apps.Activities; | |
24841698Corina5 months ago | 3 | using Microsoft.Teams.Apps.Activities.Invokes; |
77ea35b6Alex Acebo1 years ago | 4 | using Microsoft.Teams.Apps.Events; |
e37420d2Kavin1 years ago | 5 | using Microsoft.Teams.Apps.Extensions; |
cf24054eKavin10 months ago | 6 | using Microsoft.Teams.Common.Logging; |
928f1d86Kavin1 years ago | 7 | using Microsoft.Teams.Extensions.Graph; |
fb2fb20cAlex Acebo11 months ago | 8 | using Microsoft.Teams.Plugins.AspNetCore.Extensions; |
e37420d2Kavin1 years ago | 9 | |
| 10 | var builder = WebApplication.CreateBuilder(args); | |
| 11 | | |
| 12 | var appBuilder = App.Builder() | |
cf24054eKavin10 months ago | 13 | .AddLogger(new ConsoleLogger(level: Microsoft.Teams.Common.Logging.LogLevel.Debug)) |
e37420d2Kavin1 years ago | 14 | // The name of the auth connection to use. |
| 15 | // It should be the same as the OAuth connection name defined in the Azure Bot configuration. | |
| 16 | .AddOAuth("graph"); | |
| 17 | | |
733a37f2Corina2 months ago | 18 | builder.AddTeams(appBuilder); |
e37420d2Kavin1 years ago | 19 | |
| 20 | var app = builder.Build(); | |
| 21 | var teams = app.UseTeams(); | |
| 22 | | |
cf24054eKavin10 months ago | 23 | teams.Use(async context => |
| 24 | { | |
| 25 | var start = DateTime.UtcNow; | |
| 26 | try | |
| 27 | { | |
| 28 | await context.Next(); | |
2846e4f6Alex Acebo10 months ago | 29 | } |
| 30 | catch | |
cf24054eKavin10 months ago | 31 | { |
| 32 | context.Log.Error("error occurred during activity processing"); | |
| 33 | } | |
| 34 | context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms"); | |
| 35 | }); | |
| 36 | | |
2a3ae203Rido6 months ago | 37 | teams.OnMessage("/signout", async (context, cancellationToken) => |
e37420d2Kavin1 years ago | 38 | { |
| 39 | if (!context.IsSignedIn) | |
| 40 | { | |
2a3ae203Rido6 months ago | 41 | await context.Send("you are not signed in!", cancellationToken); |
e37420d2Kavin1 years ago | 42 | return; |
| 43 | } | |
| 44 | | |
2a3ae203Rido6 months ago | 45 | await context.SignOut(cancellationToken: cancellationToken); // call `SignOut()` for your auth connection... |
| 46 | await context.Send("you have been signed out!", cancellationToken); | |
e37420d2Kavin1 years ago | 47 | }); |
| 48 | | |
2a3ae203Rido6 months ago | 49 | teams.OnMessage(async (context, cancellationToken) => |
e37420d2Kavin1 years ago | 50 | { |
| 51 | if (!context.IsSignedIn) | |
| 52 | { | |
901704ddAlex Acebo11 months ago | 53 | await context.SignIn(new OAuthOptions() |
e37420d2Kavin1 years ago | 54 | { |
| 55 | // Customize the OAuth card text (only applies to OAuth flow, not SSO) | |
| 56 | OAuthCardText = "Sign in to your account", | |
| 57 | SignInButtonText = "Sign In" | |
2a3ae203Rido6 months ago | 58 | }, cancellationToken); // call `SignIn() for your auth connection... |
e37420d2Kavin1 years ago | 59 | |
| 60 | return; | |
| 61 | } | |
| 62 | | |
928f1d86Kavin1 years ago | 63 | // If user is not signed in then `GetUserGraphClient` will throw an exception |
2a3ae203Rido6 months ago | 64 | var me = await context.GetUserGraphClient().Me.GetAsync(cancellationToken: cancellationToken); |
| 65 | await context.Send($"user '{me!.DisplayName}' is already signed in!", cancellationToken); | |
e37420d2Kavin1 years ago | 66 | }); |
| 67 | | |
2a3ae203Rido6 months ago | 68 | teams.OnSignIn(async (_, @event, cancellationToken) => |
5a1c7318Alex Acebo1 years ago | 69 | { |
e37420d2Kavin1 years ago | 70 | var token = @event.Token; |
| 71 | var context = @event.Context; | |
| 72 | | |
2a3ae203Rido6 months ago | 73 | var me = await context.GetUserGraphClient().Me.GetAsync(cancellationToken: cancellationToken); |
| 74 | await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}", cancellationToken); | |
e37420d2Kavin1 years ago | 75 | }); |
| 76 | | |
24841698Corina5 months ago | 77 | teams.OnSignInFailure(async (context, cancellationToken) => |
| 78 | { | |
| 79 | var failure = context.Activity.Value; | |
| 80 | context.Log.Error($"sign-in failed: {failure?.Code} - {failure?.Message}"); | |
b704ab7fCorina5 months ago | 81 | await context.Send("Sign-in failed.", cancellationToken); |
24841698Corina5 months ago | 82 | }); |
| 83 | | |
e37420d2Kavin1 years ago | 84 | app.Run(); |