microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
26672917c280e3fc166a8e8fb59f7019fd3f4cd6

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.Graph/Program.cs

84lines · modeblame

e37420d2Kavin1 years ago1using Microsoft.Teams.Apps;
2using Microsoft.Teams.Apps.Activities;
24841698Corina5 months ago3using Microsoft.Teams.Apps.Activities.Invokes;
77ea35b6Alex Acebo1 years ago4using Microsoft.Teams.Apps.Events;
e37420d2Kavin1 years ago5using Microsoft.Teams.Apps.Extensions;
cf24054eKavin10 months ago6using Microsoft.Teams.Common.Logging;
928f1d86Kavin1 years ago7using Microsoft.Teams.Extensions.Graph;
fb2fb20cAlex Acebo11 months ago8using Microsoft.Teams.Plugins.AspNetCore.Extensions;
e37420d2Kavin1 years ago9
10var builder = WebApplication.CreateBuilder(args);
11
12var appBuilder = App.Builder()
cf24054eKavin10 months ago13.AddLogger(new ConsoleLogger(level: Microsoft.Teams.Common.Logging.LogLevel.Debug))
e37420d2Kavin1 years ago14// 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
733a37f2Corina2 months ago18builder.AddTeams(appBuilder);
e37420d2Kavin1 years ago19
20var app = builder.Build();
21var teams = app.UseTeams();
22
cf24054eKavin10 months ago23teams.Use(async context =>
24{
25var start = DateTime.UtcNow;
26try
27{
28await context.Next();
2846e4f6Alex Acebo10 months ago29}
30catch
cf24054eKavin10 months ago31{
32context.Log.Error("error occurred during activity processing");
33}
34context.Log.Debug($"request took {(DateTime.UtcNow - start).TotalMilliseconds}ms");
35});
36
2a3ae203Rido6 months ago37teams.OnMessage("/signout", async (context, cancellationToken) =>
e37420d2Kavin1 years ago38{
39if (!context.IsSignedIn)
40{
2a3ae203Rido6 months ago41await context.Send("you are not signed in!", cancellationToken);
e37420d2Kavin1 years ago42return;
43}
44
2a3ae203Rido6 months ago45await context.SignOut(cancellationToken: cancellationToken); // call `SignOut()` for your auth connection...
46await context.Send("you have been signed out!", cancellationToken);
e37420d2Kavin1 years ago47});
48
2a3ae203Rido6 months ago49teams.OnMessage(async (context, cancellationToken) =>
e37420d2Kavin1 years ago50{
51if (!context.IsSignedIn)
52{
901704ddAlex Acebo11 months ago53await context.SignIn(new OAuthOptions()
e37420d2Kavin1 years ago54{
55// Customize the OAuth card text (only applies to OAuth flow, not SSO)
56OAuthCardText = "Sign in to your account",
57SignInButtonText = "Sign In"
2a3ae203Rido6 months ago58}, cancellationToken); // call `SignIn() for your auth connection...
e37420d2Kavin1 years ago59
60return;
61}
62
928f1d86Kavin1 years ago63// If user is not signed in then `GetUserGraphClient` will throw an exception
2a3ae203Rido6 months ago64var me = await context.GetUserGraphClient().Me.GetAsync(cancellationToken: cancellationToken);
65await context.Send($"user '{me!.DisplayName}' is already signed in!", cancellationToken);
e37420d2Kavin1 years ago66});
67
2a3ae203Rido6 months ago68teams.OnSignIn(async (_, @event, cancellationToken) =>
5a1c7318Alex Acebo1 years ago69{
e37420d2Kavin1 years ago70var token = @event.Token;
71var context = @event.Context;
72
2a3ae203Rido6 months ago73var me = await context.GetUserGraphClient().Me.GetAsync(cancellationToken: cancellationToken);
74await context.Send($"user \"{me!.DisplayName}\" signed in. Here's the token: {token.Token}", cancellationToken);
e37420d2Kavin1 years ago75});
76
24841698Corina5 months ago77teams.OnSignInFailure(async (context, cancellationToken) =>
78{
79var failure = context.Activity.Value;
80context.Log.Error($"sign-in failed: {failure?.Code} - {failure?.Message}");
b704ab7fCorina5 months ago81await context.Send("Sign-in failed.", cancellationToken);
24841698Corina5 months ago82});
83
e37420d2Kavin1 years ago84app.Run();