diff --git a/.gitignore b/.gitignore index 0917f87..940c6ef 100644 --- a/.gitignore +++ b/.gitignore @@ -453,3 +453,4 @@ !.vscode/launch.json !.vscode/extensions.json /.vscode +/VQCalendarAttention/.env diff --git a/VQCalendarAttention/DiscordWebhook.cs b/VQCalendarAttention/DiscordWebhook.cs new file mode 100644 index 0000000..394cde2 --- /dev/null +++ b/VQCalendarAttention/DiscordWebhook.cs @@ -0,0 +1,34 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace VestalisQuintet.VQCalendarAttention +{ + public class DiscordWebhook + { + /// + /// webhookのURLを保持する + /// + private readonly string _webhookUrl; + + public DiscordWebhook(string webhookUrl) + { + _webhookUrl = webhookUrl; + } + + public async Task SendMessageAsync(string message) + { + using (var httpClient = new HttpClient()) + { + var content = new StringContent(JsonConvert.SerializeObject(new { content = message }), Encoding.UTF8, "application/json"); + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + var response = await httpClient.PostAsync(_webhookUrl, content); + response.EnsureSuccessStatusCode(); + } + } + } +} diff --git a/VQCalendarAttention/Program.cs b/VQCalendarAttention/Program.cs index 5222b68..0a5cac5 100644 --- a/VQCalendarAttention/Program.cs +++ b/VQCalendarAttention/Program.cs @@ -7,15 +7,14 @@ { public class Program { - private const string CredentialsFilePath = "../calendartowebhook-fa30e21f081f.json"; - private const string CalendarId = "3e39821e10e795b311f93c79490960c3196632a465a794ca32e8a44c50115dde@group.calendar.google.com"; - public static void Main(string[] args) { + DotNetEnv.Env.Load(".env"); + CreateHostBuilder(args).Build().Run(); // テストとしてCalendarApiTestを実行する - // await CalendarApiTest.GetLatestEventAsync("3e39821e10e795b311f93c79490960c3196632a465a794ca32e8a44c50115dde@group.calendar.google.com", "../calendartowebhook-fa30e21f081f.json"); + // await CalendarApiTest.GetLatestEventAsync(DotNetEnv.Env.GetString("CALENDAR_ID"), DotNetEnv.Env.GetString("CREDENTIALS_FILE_PATH")); } public static IHostBuilder CreateHostBuilder(string[] args) => @@ -24,7 +23,7 @@ { // Cronosで定義したCronジョブサービスを追加 // 翌日の予定を通知する - services.AddHostedService(serviceProvider => + services.AddHostedService(serviceProvider => { // Cron式を設定 (例: 毎日午前9時に実行) //var cronExpression = CronExpression.Parse("0 9 * * *"); @@ -35,7 +34,9 @@ // タイムゾーンを東京に設定 var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time"); - return new NextDayCalendarAttentionService(cronExpression, timeZoneInfo, serviceProvider, CredentialsFilePath, CalendarId); + string calendarId = DotNetEnv.Env.GetString("CALENDAR_ID"); + string CredentialsFilePath = DotNetEnv.Env.GetString("CREDENTIALS_FILE_PATH"); + return new NextDayCalendarAttentionService(cronExpression, timeZoneInfo, serviceProvider, CredentialsFilePath, calendarId); }); }); } diff --git a/VQCalendarAttention/Services/NextDayCalendarAttentionService.cs b/VQCalendarAttention/Services/NextDayCalendarAttentionService.cs index 09f87dc..c893df0 100644 --- a/VQCalendarAttention/Services/NextDayCalendarAttentionService.cs +++ b/VQCalendarAttention/Services/NextDayCalendarAttentionService.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using Cronos; using Google.Apis.Calendar.v3; using Microsoft.Extensions.DependencyInjection; @@ -63,11 +64,30 @@ // 明日の予定を表示 if (tomorrowEvents.Count > 0) { - Console.WriteLine("明日の予定:"); + // DiscordWebhook インスタンスを作成 + string webhookUrl = DotNetEnv.Env.GetString("DISCORD_WEBHOOK_URL"); + var discordWebhook = new DiscordWebhook(webhookUrl); + + // 通知先ロールIDを指定 + string roleId = DotNetEnv.Env.GetString("DISCORD_ROLE_ID"); + + // 明日の予定をメッセージとして組み立て + var sb = new StringBuilder("明日の予定:\n"); foreach (var tomorrowEvent in tomorrowEvents) { - Console.WriteLine($"{tomorrowEvent.Summary}: {tomorrowEvent.Start.DateTime}"); + var importanceMention = ""; + // カレンダーの予定の説明欄に[重要]と入ってたら、オーネグスメンバー宛にメンションを入れるようにする + if (tomorrowEvent.Description != null && tomorrowEvent.Description.Contains("[重要]")) + { + importanceMention = $" <@&{roleId}>"; + } + sb.AppendLine($"{tomorrowEvent.Summary}: {tomorrowEvent.Start.DateTime}{importanceMention}"); } + + // メッセージをDiscordに送信 + await discordWebhook.SendMessageAsync(sb.ToString()); + + Console.WriteLine(sb.ToString()); } else { diff --git a/VQCalendarAttention/VQCalendarAttention.csproj b/VQCalendarAttention/VQCalendarAttention.csproj index 08d087f..1a0f4d0 100644 --- a/VQCalendarAttention/VQCalendarAttention.csproj +++ b/VQCalendarAttention/VQCalendarAttention.csproj @@ -9,6 +9,7 @@ +