microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Samples/Samples.Graph/Program.cs
77lines · modecode
| 1 | using Microsoft.Teams.Apps; |
| 2 | using Microsoft.Teams.Apps.Activities; |
| 3 | using Microsoft.Teams.Apps.Events; |
| 4 | using Microsoft.Teams.Apps.Extensions; |
| 5 | using Microsoft.Teams.Common.Logging; |
| 6 | using Microsoft.Teams.Extensions.Graph; |
| 7 | using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions; |
| 8 | using Microsoft.Teams.Plugins.AspNetCore.Extensions; |
| 9 | |
| 10 | var builder = WebApplication.CreateBuilder(args); |
| 11 | |
| 12 | var appBuilder = App.Builder() |
| 13 | .AddLogger(new ConsoleLogger(level: Microsoft.Teams.Common.Logging.LogLevel.Debug)) |
| 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 | |
| 18 | builder.AddTeams(appBuilder).AddTeamsDevTools(); |
| 19 | |
| 20 | var app = builder.Build(); |
| 21 | var teams = app.UseTeams(); |
| 22 | |
| 23 | teams.Use(async context => |
| 24 | { |
| 25 | var start = DateTime.UtcNow; |
| 26 | try |
| 27 | { |
| 28 | await context.Next(); |
| 29 | } |
| 30 | catch |
| 31 | { |
| 32 | context.Log.Error("error occurred during activity processing"); |
| 33 | } |
| 34 | context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms"); |
| 35 | }); |
| 36 | |
| 37 | teams.OnMessage("/signout", async context => |
| 38 | { |
| 39 | if (!context.IsSignedIn) |
| 40 | { |
| 41 | await context.Send("you are not signed in!"); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | await context.SignOut(); // call `SignOut()` for your auth connection... |
| 46 | await context.Send("you have been signed out!"); |
| 47 | }); |
| 48 | |
| 49 | teams.OnMessage(async context => |
| 50 | { |
| 51 | if (!context.IsSignedIn) |
| 52 | { |
| 53 | await context.SignIn(new OAuthOptions() |
| 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" |
| 58 | }); // call `SignIn() for your auth connection... |
| 59 | |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // If user is not signed in then `GetUserGraphClient` will throw an exception |
| 64 | var me = await context.GetUserGraphClient().Me.GetAsync(); |
| 65 | await context.Send($"user '{me!.DisplayName}' is already signed in!"); |
| 66 | }); |
| 67 | |
| 68 | teams.OnSignIn(async (_, @event) => |
| 69 | { |
| 70 | var token = @event.Token; |
| 71 | var context = @event.Context; |
| 72 | |
| 73 | var me = await context.GetUserGraphClient().Me.GetAsync(); |
| 74 | await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}"); |
| 75 | }); |
| 76 | |
| 77 | app.Run(); |