microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2d96e60ca204cc76be8786a1bb66e95109cb9b41

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

77lines · modecode

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