microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
df2202d1bf4bccd37efa56c709f911109c0d9ec5

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

75lines · modecode

1using Microsoft.Teams.Apps;
2using Microsoft.Teams.Apps.Activities;
3using Microsoft.Teams.Apps.Events;
4using Microsoft.Teams.Apps.Extensions;
5using Microsoft.Teams.Extensions.Graph;
6using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions;
7using Microsoft.Teams.Plugins.AspNetCore.Extensions;
8
9var builder = WebApplication.CreateBuilder(args);
10
11var appBuilder = App.Builder()
12 // The name of the auth connection to use.
13 // It should be the same as the OAuth connection name defined in the Azure Bot configuration.
14 .AddOAuth("graph");
15
16builder.AddTeams(appBuilder).AddTeamsDevTools();
17
18var app = builder.Build();
19var teams = app.UseTeams();
20
21teams.Use(async context =>
22{
23 var start = DateTime.UtcNow;
24 try
25 {
26 await context.Next();
27 }
28 catch
29 {
30 context.Log.LogError("error occurred during activity processing");
31 }
32 context.Log.LogDebug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms");
33});
34
35teams.OnMessage("/signout", async context =>
36{
37 if (!context.IsSignedIn)
38 {
39 await context.Send("you are not signed in!");
40 return;
41 }
42
43 await context.SignOut(); // call `SignOut()` for your auth connection...
44 await context.Send("you have been signed out!");
45});
46
47teams.OnMessage(async context =>
48{
49 if (!context.IsSignedIn)
50 {
51 await context.SignIn(new OAuthOptions()
52 {
53 // Customize the OAuth card text (only applies to OAuth flow, not SSO)
54 OAuthCardText = "Sign in to your account",
55 SignInButtonText = "Sign In"
56 }); // call `SignIn() for your auth connection...
57
58 return;
59 }
60
61 // If user is not signed in then `GetUserGraphClient` will throw an exception
62 var me = await context.GetUserGraphClient().Me.GetAsync();
63 await context.Send($"user '{me!.DisplayName}' is already signed in!");
64});
65
66teams.OnSignIn(async (_, @event) =>
67{
68 var token = @event.Token;
69 var context = @event.Context;
70
71 var me = await context.GetUserGraphClient().Me.GetAsync();
72 await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}");
73});
74
75app.Run();