-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
206 lines (179 loc) · 8.93 KB
/
Copy pathProgram.cs
File metadata and controls
206 lines (179 loc) · 8.93 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// The client credentials flow requires that you request the
// /.default scope, and preconfigure your permissions on the
// app registration in Azure. An administrator must grant consent
// to those permissions beforehand.
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Kiota.Abstractions;
using System.Configuration;
using System.Collections.Specialized;
using Microsoft.Extensions.Configuration.Json;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
// Define scopes
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Read values from configuration file
var tenantId = configuration.GetSection("Connect:AzureTenantID").Value;
var clientId = configuration.GetSection("Connect:AzureClientID").Value;
var clientSecret = configuration.GetSection("Connect:AzureClientSecret").Value;
var appointmentSubject = configuration.GetSection("UserConf:EventSubject").Value;
var o365UserIDs = configuration.GetSection("UserConf:UserID").Get<string[]>();
var externalMessage = configuration.GetSection("UserConf:ExternalMessage").Value;
var internalMessage = configuration.GetSection("UserConf:InternalMessage").Value;
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
// main
foreach (var o365UserID in o365UserIDs)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("--------------------------------------------");
Console.WriteLine("App starting check appointments for: " + o365UserID.ToString());
Console.ResetColor();
// Get data from GraphAPI
var o365CalRequest = await graphClient.Users[$"{o365UserID}"].Events.GetAsync((requestConfiguration) =>
{
//requestConfiguration.QueryParameters.Select = new string[] { "start/dateTime", "end/dateTime", "subject"};
requestConfiguration.QueryParameters.Filter = $"startsWith(subject,'{appointmentSubject}')";
requestConfiguration.QueryParameters.Orderby = new string[] { "start/dateTime asc" };
});
try
{ // Store data
var subject = o365CalRequest.Value[0].Subject;
var start = o365CalRequest.Value[0].Start.DateTime;
var end = o365CalRequest.Value[0].End.DateTime;
var timezone = o365CalRequest.Value[0].Start.TimeZone;
// DateTimeTimeZone Werte erstellen
var startDTTZ = new DateTimeTimeZone();
var endDTTZ = new DateTimeTimeZone();
startDTTZ.DateTime = start.ToString();
startDTTZ.TimeZone = timezone;
endDTTZ.DateTime = end.ToString();
endDTTZ.TimeZone = timezone;
//Mailbox Settings sind unterhalb des "Users" kontext, es werden die kompletten MailboxSettings ausgelesen
var o365CalRequest2 = await graphClient.Users[$"{o365UserID}"].GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Select = new string[] { "mailboxSettings" };
});
//Ausgelesene Werte in Variablen speichern
var mailboxSettings = o365CalRequest2?.MailboxSettings;
var outOfOfficeActive = mailboxSettings.AutomaticRepliesSetting.Status;
var outOfOfficeStart = mailboxSettings.AutomaticRepliesSetting.ScheduledStartDateTime.DateTime;
var outOfOfficeEnd = mailboxSettings.AutomaticRepliesSetting.ScheduledEndDateTime.DateTime;
//Die ausgelesenen Werte überprüfen
Console.WriteLine("OoO:" + outOfOfficeActive + " - " + outOfOfficeStart + " - " + outOfOfficeEnd);
Console.WriteLine("Event:" + subject + " - " + start + " - " + end);
//Convertieren des GraphAPI Rückgabewert in DateTime Format
var parsedStartDate = DateTime.Parse(start);
var parsedEndDate = DateTime.Parse(end);
var parsedStartOoODate = DateTime.Parse(outOfOfficeStart);
var parsedEndOoODate = DateTime.Parse(outOfOfficeEnd);
// Replace Placeholder from config file
externalMessage = externalMessage.Replace("[start]", parsedStartDate.ToShortDateString());
externalMessage = externalMessage.Replace("[end]", parsedEndDate.ToShortDateString());
internalMessage = internalMessage.Replace("[start]", parsedStartDate.ToShortDateString());
internalMessage = internalMessage.Replace("[end]", parsedEndDate.ToShortDateString());
// Set OoO when there is no active earlier message
if (parsedEndOoODate <= parsedEndDate && (outOfOfficeActive.ToString() == "scheduled" || outOfOfficeActive.ToString() == "alwaysEnabled"))
{
Console.WriteLine("There is already an earlier OoO Message active.");
}
else
{
mailboxSettings = new MailboxSettings
{
AutomaticRepliesSetting = new AutomaticRepliesSetting
{
// ScheduledStartDateTime = new DateTimeTimeZone(),
// ScheduledEndDateTime = new DateTimeTimeZone()
Status = AutomaticRepliesStatus.Scheduled,
ScheduledStartDateTime = startDTTZ,
ScheduledEndDateTime = endDTTZ,
//Set external OoO Message active (OPTIONS: none, ContactsOnly, All)
ExternalAudience = ExternalAudienceScope.ContactsOnly,
ExternalReplyMessage = $"{externalMessage}",
InternalReplyMessage = $"{internalMessage}"
}
};
var requestInformation = graphClient.Users[$"{o365UserID}"].ToGetRequestInformation();
requestInformation.HttpMethod = Method.PATCH;
requestInformation.UrlTemplate = "{+baseurl}/users/{user%2Did}/mailboxSettings"; //update the template to include /mailBoxSettings
requestInformation.SetContentFromParsable<MailboxSettings>(graphClient.RequestAdapter, "application/json", mailboxSettings);
await graphClient.RequestAdapter.SendNoContentAsync(requestInformation);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Found an appointment with subject: {appointmentSubject}");
Console.WriteLine($"For user: {o365UserID}");
Console.WriteLine($"Set auto response from: {parsedStartDate.ToShortDateString()} until {parsedEndDate.ToShortDateString()}");
Console.WriteLine();
Console.WriteLine("With this internal Message:");
Console.WriteLine($"{internalMessage}");
Console.WriteLine();
Console.WriteLine("And this external Message:");
Console.WriteLine($"{externalMessage}");
Console.ResetColor();
}
//Send Mail
var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = new Message
{
Subject = "Out of Office Auto Response has been configured",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = $"I configured the Auto-Reply from {parsedStartDate.ToShortDateString()} to {parsedEndDate.ToShortDateString()}",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = $"{o365UserID}",
},
},
},
},
SaveToSentItems = true,
};
await graphClient.Users[$"{o365UserID}"].SendMail.PostAsync(requestBody);
}
catch (ArgumentOutOfRangeException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Did not find any apointment with the subject: {appointmentSubject}");
Console.WriteLine($"For user: {o365UserID}");
Console.ResetColor();
}
}
/* var MailboxSettingsDiv =>
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('username')/mailboxSettings",
"timeZone": "W. Europe Standard Time",
"delegateMeetingMessageDeliveryOptions": "sendToDelegateOnly",
"dateFormat": "dd.MM.yyyy",
"timeFormat": "HH:mm",
"userPurpose": "user",
"automaticRepliesSetting": {
"status": "disabled",
"externalAudience": "none",
"internalReplyMessage": "",
"externalReplyMessage": "",
"scheduledStartDateTime": {
"dateTime": "2022-08-27T08:00:00.0000000",
"timeZone": "UTC"
},
"scheduledEndDateTime": {
"dateTime": "2022-08-28T08:00:00.0000000",
"timeZone": "UTC"
}
}, */