-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathProgram.cs
More file actions
128 lines (108 loc) · 4.05 KB
/
Copy pathProgram.cs
File metadata and controls
128 lines (108 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Teams.Apps;
using GraphOrganization = Microsoft.Graph.Models.Organization;
using GraphUser = Microsoft.Graph.Models.User;
WebApplicationBuilder webAppBuilder = WebApplication.CreateSlimBuilder(args);
webAppBuilder.Services.AddTeamsBotApplication();
webAppBuilder.Services.AddSingleton<GraphServiceClient>(sp =>
{
IConfiguration config = sp.GetRequiredService<IConfiguration>();
string tenantId = config["AzureAd:TenantId"]
?? throw new InvalidOperationException("AzureAd:TenantId is not configured.");
string clientId = config["AzureAd:ClientId"]
?? throw new InvalidOperationException("AzureAd:ClientId is not configured.");
string? clientSecret = config["AzureAd:ClientCredentials:0:ClientSecret"];
TokenCredential credential = string.IsNullOrWhiteSpace(clientSecret)
? new ManagedIdentityCredential(clientId)
: new ClientSecretCredential(tenantId, clientId, clientSecret);
return new GraphServiceClient(credential, ["https://graph.microsoft.com/.default"]);
});
WebApplication webApp = webAppBuilder.Build();
TeamsBotApplication bot = webApp.UseTeamsBotApplication();
GraphServiceClient graph = webApp.Services.GetRequiredService<GraphServiceClient>();
bot.OnMessage("(?i)^help$", async (context, ct) =>
{
string helpText = """
**Graph Bot** - App-only Microsoft Graph SDK sample
Commands:
- `users` - List top 5 users (app token)
- `org` - Show tenant organization display name(s)
- `help` - Show this message
""";
await context.SendAsync(
new MessageActivityInput()
.WithText(helpText, TextFormats.Markdown)
, ct);
});
bot.OnMessage("(?i)^users$", async (context, ct) =>
{
try
{
var users = await graph.Users.GetAsync(request =>
{
request.QueryParameters.Top = 5;
request.QueryParameters.Select = ["displayName", "mail", "userPrincipalName", "id"];
}, ct);
if (users?.Value is null || users.Value.Count == 0)
{
await context.SendAsync("No users returned.", ct);
return;
}
List<string> lines = ["**Top users (app token)**"];
foreach (GraphUser user in users.Value)
{
string name = user.DisplayName ?? "(unknown)";
string email = user.Mail ?? user.UserPrincipalName ?? "(unknown)";
string id = user.Id ?? "(unknown)";
lines.Add($"- {name} — {email} (`{id}`)");
}
await context.SendAsync(
new MessageActivityInput()
.WithText(string.Join("\n", lines), TextFormats.Markdown)
, ct);
}
catch (Exception ex)
{
await context.SendAsync($"Graph users call failed: {ex.Message}", ct);
}
});
bot.OnMessage("(?i)^org$", async (context, ct) =>
{
try
{
var orgs = await graph.Organization.GetAsync(request =>
{
request.QueryParameters.Select = ["displayName", "id"];
}, ct);
if (orgs?.Value is null || orgs.Value.Count == 0)
{
await context.SendAsync("No organization records returned.", ct);
return;
}
List<string> lines = ["**Organization (app token)**"];
foreach (GraphOrganization org in orgs.Value)
{
lines.Add($"- {org.DisplayName ?? "(unknown)"} (`{org.Id ?? "(unknown)"}`)");
}
await context.SendAsync(
new MessageActivityInput()
.WithText(string.Join("\n", lines), TextFormats.Markdown)
, ct);
}
catch (Exception ex)
{
await context.SendAsync($"Graph org call failed: {ex.Message}", ct);
}
});
bot.OnInstall(async (context, ct) =>
{
await context.SendAsync(
new MessageActivityInput()
.WithText("Welcome to **Graph Bot** (app-only)! Type `help` to see available commands.", TextFormats.Markdown)
, ct);
});
webApp.Run();