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