microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Samples/Samples.Graph/Program.cs
68lines · 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.Extensions.Graph; |
| 6 | using Microsoft.Teams.Plugins.AspNetCore.Extensions; |
| 7 | |
| 8 | var builder = WebApplication.CreateBuilder(args); |
| 9 | builder.AddTeams(); |
| 10 | builder.Services.Configure<AppOptions>(options => options.OAuth = new OAuthSettings("graph")); |
| 11 | var app = builder.Build(); |
| 12 | var teams = app.UseTeams(); |
| 13 | |
| 14 | teams.Use(async context => |
| 15 | { |
| 16 | var start = DateTime.UtcNow; |
| 17 | try |
| 18 | { |
| 19 | await context.Next(); |
| 20 | } |
| 21 | catch |
| 22 | { |
| 23 | context.Log.Error("error occurred during activity processing"); |
| 24 | } |
| 25 | context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms"); |
| 26 | }); |
| 27 | |
| 28 | teams.OnMessage("/signout", async context => |
| 29 | { |
| 30 | if (!context.IsSignedIn) |
| 31 | { |
| 32 | await context.Send("you are not signed in!"); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | await context.SignOut(); // call `SignOut()` for your auth connection... |
| 37 | await context.Send("you have been signed out!"); |
| 38 | }); |
| 39 | |
| 40 | teams.OnMessage(async context => |
| 41 | { |
| 42 | if (!context.IsSignedIn) |
| 43 | { |
| 44 | await context.SignIn(new OAuthOptions() |
| 45 | { |
| 46 | // Customize the OAuth card text (only applies to OAuth flow, not SSO) |
| 47 | OAuthCardText = "Sign in to your account", |
| 48 | SignInButtonText = "Sign In" |
| 49 | }); // call `SignIn() for your auth connection... |
| 50 | |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // If user is not signed in then `GetUserGraphClient` will throw an exception |
| 55 | var me = await context.GetUserGraphClient().Me.GetAsync(); |
| 56 | await context.Send($"user '{me!.DisplayName}' is already signed in!"); |
| 57 | }); |
| 58 | |
| 59 | teams.OnSignIn(async (_, @event) => |
| 60 | { |
| 61 | var token = @event.Token; |
| 62 | var context = @event.Context; |
| 63 | |
| 64 | var me = await context.GetUserGraphClient().Me.GetAsync(); |
| 65 | await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}"); |
| 66 | }); |
| 67 | |
| 68 | app.Run(); |