microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4e77a5cc22b7f3e3bc8a37f8d22d2bd0f75e61ac

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

61lines · modeblame

e37420d2Kavin1 years ago1using Microsoft.Teams.Apps;
2using Microsoft.Teams.Apps.Activities;
77ea35b6Alex Acebo1 years ago3using Microsoft.Teams.Apps.Events;
e37420d2Kavin1 years ago4using Microsoft.Teams.Apps.Extensions;
928f1d86Kavin11 months ago5using Microsoft.Teams.Extensions.Graph;
fb2fb20cAlex Acebo9 months ago6using Microsoft.Teams.Plugins.AspNetCore.Extensions;
e37420d2Kavin1 years ago7
8var builder = WebApplication.CreateBuilder(args);
9
10var appBuilder = App.Builder()
11.AddLogger(level: Microsoft.Teams.Common.Logging.LogLevel.Debug)
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);
17
18var app = builder.Build();
19var teams = app.UseTeams();
20
21teams.OnMessage("/signout", async context =>
22{
23if (!context.IsSignedIn)
24{
25await context.Send("you are not signed in!");
26return;
27}
28
29await context.SignOut(); // call `SignOut()` for your auth connection...
30await context.Send("you have been signed out!");
31});
32
33teams.OnMessage(async context =>
34{
35if (!context.IsSignedIn)
36{
901704ddAlex Acebo9 months ago37await context.SignIn(new OAuthOptions()
e37420d2Kavin1 years ago38{
39// Customize the OAuth card text (only applies to OAuth flow, not SSO)
40OAuthCardText = "Sign in to your account",
41SignInButtonText = "Sign In"
42}); // call `SignIn() for your auth connection...
43
44return;
45}
46
928f1d86Kavin11 months ago47// If user is not signed in then `GetUserGraphClient` will throw an exception
48var me = await context.GetUserGraphClient().Me.GetAsync();
e37420d2Kavin1 years ago49await context.Send($"user '{me!.DisplayName}' is already signed in!");
50});
51
5a1c7318Alex Acebo1 years ago52teams.OnSignIn(async (_, @event) =>
53{
e37420d2Kavin1 years ago54var token = @event.Token;
55var context = @event.Context;
56
928f1d86Kavin11 months ago57var me = await context.GetUserGraphClient().Me.GetAsync();
e37420d2Kavin1 years ago58await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}");
59});
60
61app.Run();