microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c5e63a262c0da168a0339cc65cd714b09609e8cf

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

76lines · 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 } catch
30 {
31 context.Log.Error("error occurred during activity processing");
32 }
33 context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms");
34});
35
36teams.OnMessage("/signout", async context =>
37{
38 if (!context.IsSignedIn)
39 {
40 await context.Send("you are not signed in!");
41 return;
42 }
43
44 await context.SignOut(); // call `SignOut()` for your auth connection...
45 await context.Send("you have been signed out!");
46});
47
48teams.OnMessage(async context =>
49{
50 if (!context.IsSignedIn)
51 {
52 await context.SignIn(new OAuthOptions()
53 {
54 // Customize the OAuth card text (only applies to OAuth flow, not SSO)
55 OAuthCardText = "Sign in to your account",
56 SignInButtonText = "Sign In"
57 }); // call `SignIn() for your auth connection...
58
59 return;
60 }
61
62 // If user is not signed in then `GetUserGraphClient` will throw an exception
63 var me = await context.GetUserGraphClient().Me.GetAsync();
64 await context.Send($"user '{me!.DisplayName}' is already signed in!");
65});
66
67teams.OnSignIn(async (_, @event) =>
68{
69 var token = @event.Token;
70 var context = @event.Context;
71
72 var me = await context.GetUserGraphClient().Me.GetAsync();
73 await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}");
74});
75
76app.Run();