-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphHelper.cs
206 lines (177 loc) · 6.78 KB
/
GraphHelper.cs
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
206
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
using CsvHelper;
using System.Globalization;
class GraphHelper
{
// Settings object
private static Settings? _settings;
// User auth token credential
private static DeviceCodeCredential? _deviceCodeCredential;
// Client configured with user authentication
private static GraphServiceClient? _userClient;
public static async Task<string> GetUserTokenAsync()
{
// Ensure credential isn't null
_ = _deviceCodeCredential ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
// Ensure scopes isn't null
_ = _settings?.GraphUserScopes ?? throw new System.ArgumentNullException("Argument 'scopes' cannot be null");
// Request token with given scopes
var context = new TokenRequestContext(_settings.GraphUserScopes);
var response = await _deviceCodeCredential.GetTokenAsync(context);
return response.Token;
}
public static async Task SendMailAsync(string subject, ItemBody body, string recipient)
{
// Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
// Create a new message
var message = new Message
{
Subject = subject,
Body = body,
ToRecipients = new Recipient[]
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = recipient
}
}
}
};
// Send the message
await _userClient.Me
.SendMail(message)
.Request()
.PostAsync();
}
public static void InitializeGraphForUserAuth(Settings settings,
Func<DeviceCodeInfo, CancellationToken, Task> deviceCodePrompt)
{
_settings = settings;
_deviceCodeCredential = new DeviceCodeCredential(deviceCodePrompt,
settings.TenantId, settings.ClientId);
_userClient = new GraphServiceClient(_deviceCodeCredential, settings.GraphUserScopes);
}
public static Task<User?> GetUserAsync()
{
// Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
return _userClient.Me
.Request()
.Select(u => new
{
// Only request specific properties
u.DisplayName,
u.Mail,
u.UserPrincipalName
})
.GetAsync();
}
public async static Task<string?> CreateTeamsMeet(DateTime startDateTime)
{
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var sub = "Interview Meeting";
var srtTime = startDateTime;
var hrs = "1";
var requestBody = new OnlineMeeting
{
StartDateTime = srtTime,
EndDateTime = srtTime.AddHours(Int32.Parse(hrs)),
Subject = sub
};
var meetLink = await _userClient.Me.OnlineMeetings.Request().AddAsync(requestBody);
return meetLink?.JoinWebUrl;
}
public async static Task SendInterviewInvitationsAsync()
{
try
{
var settings = Settings.LoadSettings();
// Read data from CSV file
Console.WriteLine("Please enter the path to the CSV file:");
var csvFilePath = Console.ReadLine();
var recipients = ReadRecipientsFromCsv(csvFilePath);
foreach (var recipient in recipients)
{
Console.WriteLine($"Sending email to {recipient.Name} ({recipient.Email})...");
// Create Teams meeting
var meetingURL = await GraphHelper.CreateTeamsMeet(recipient.InterviewTime);
// Send email to interviewee
var message = new Message
{
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = recipient.Email,
Name = recipient.Name
}
}
},
Subject = "Interview invitation",
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = $"Dear {recipient.Name},<br><br>You have been invited for an interview on {recipient.InterviewTime}. Please click <a href='{meetingURL}'>here</a> to join the Teams meeting.<br><br>Best regards,<br>The Interview Team"
}
};
await SendMailAsync(message.Subject, message.Body, recipient.Email);
Console.WriteLine("Email sent to interviewee!\n");
// Send email to interviewer
Console.WriteLine($"Sending email to {recipient.Interviewer} ({recipient.InterviewerMail})...");
message = new Message
{
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = recipient.InterviewerMail,
Name = recipient.Interviewer
}
}
},
Subject = "Interview Scheduled",
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = $"Dear {recipient.Interviewer},<br><br>You have been assigned to take an interview on {recipient.InterviewTime}. <br> Candidate Name is {recipient.Name}. Please click <a href='{meetingURL}'>here</a> to join the Teams meeting.<br><br>Best regards,<br>The Interview Team"
}
};
await SendMailAsync(message.Subject, message.Body, recipient.Email);
Console.WriteLine("Email sent to interviewer!\n");
Console.WriteLine("Interview scheduled!\n\n");
}
Console.WriteLine("All emails have been sent. Thank you for using this tool!\n\n\n");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
static List<RecipientData> ReadRecipientsFromCsv(string filePath)
{
using var reader = new StreamReader(filePath);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
return csv.GetRecords<RecipientData>().ToList();
}
class RecipientData
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime InterviewTime { get; set; }
public string Interviewer { get; set; }
public string InterviewerMail { get; set; }
}
}