microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
next/oauth-card-null-ref-bug

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

85lines · modecode

1using Microsoft.Teams.Apps;
2using Microsoft.Teams.Apps.Activities;
3using Microsoft.Teams.Apps.Activities.Invokes;
4using Microsoft.Teams.Apps.Events;
5using Microsoft.Teams.Apps.Extensions;
6using Microsoft.Teams.Common.Logging;
7using Microsoft.Teams.Extensions.Graph;
8using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions;
9using Microsoft.Teams.Plugins.AspNetCore.Extensions;
10
11var builder = WebApplication.CreateBuilder(args);
12
13var 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
19builder.AddTeams(appBuilder).AddTeamsDevTools();
20
21var app = builder.Build();
22var teams = app.UseTeams();
23
24teams.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
38teams.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
50teams.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
69teams.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
78teams.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
85app.Run();