From 409ae39297602840c2793d0bfc4aceb1e821c3cb Mon Sep 17 00:00:00 2001 From: serov <1@dmserov.ru> Date: Sun, 23 Feb 2025 09:59:42 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?/=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.config | 7 ++++ OutlookTelegramNotifier.cs | 76 ++++++++++++++++++++++++++++++++++++++ ThisAddIn.cs | 34 +++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 App.config create mode 100644 OutlookTelegramNotifier.cs create mode 100644 ThisAddIn.cs diff --git a/App.config b/App.config new file mode 100644 index 0000000..ce5349b --- /dev/null +++ b/App.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/OutlookTelegramNotifier.cs b/OutlookTelegramNotifier.cs new file mode 100644 index 0000000..67e16ee --- /dev/null +++ b/OutlookTelegramNotifier.cs @@ -0,0 +1,76 @@ +using Microsoft.Office.Interop.Outlook; +using System; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using Telegram.Bot; + +namespace OutlookTelegramNotifier +{ + [ComVisible(true)] + public class OutlookTelegramNotifier + { + private Application outlookApp; + private TelegramBotClient botClient; + private const string TELEGRAM_BOT_TOKEN = "YOUR_BOT_TOKEN"; + private const string TELEGRAM_CHANNEL_ID = "YOUR_CHANNEL_ID"; + private Timer checkTimer; + + public OutlookTelegramNotifier() + { + outlookApp = Globals.ThisAddIn.Application; + botClient = new TelegramBotClient(TELEGRAM_BOT_TOKEN); + + // Запускаем таймер для проверки событий каждую минуту + checkTimer = new Timer(CheckUpcomingMeetings, null, 0, 60000); + } + + private async void CheckUpcomingMeetings(object state) + { + try + { + var calendar = outlookApp.Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar); + var items = calendar.Items; + items.Sort("[Start]"); + + var now = DateTime.Now; + var fifteenMinutesFromNow = now.AddMinutes(15); + + foreach (AppointmentItem appointment in items) + { + if (appointment.Start > now && appointment.Start <= fifteenMinutesFromNow) + { + // Проверяем, не отправляли ли мы уже уведомление для этой встречи + if (!appointment.UserProperties["NotificationSent"]?.Value) + { + await SendTelegramNotification(appointment); + + // Отмечаем, что уведомление отправлено + var prop = appointment.UserProperties.Add("NotificationSent", OlUserPropertyType.olYesNo); + prop.Value = true; + appointment.Save(); + } + } + } + } + catch (Exception ex) + { + // Обработка ошибок + System.Diagnostics.Debug.WriteLine($"Error: {ex.Message}"); + } + } + + private async Task SendTelegramNotification(AppointmentItem appointment) + { + var message = $"🔔 Напоминание о встрече\n\n" + + $"📅 {appointment.Subject}\n" + + $"⏰ Начало: {appointment.Start:HH:mm}\n" + + $"📍 {appointment.Location}\n\n" + + $"Описание: {appointment.Body.Substring(0, Math.Min(appointment.Body.Length, 100))}..."; + + await botClient.SendTextMessageAsync( + chatId: TELEGRAM_CHANNEL_ID, + text: message + ); + } + } +} \ No newline at end of file diff --git a/ThisAddIn.cs b/ThisAddIn.cs new file mode 100644 index 0000000..6b61e0a --- /dev/null +++ b/ThisAddIn.cs @@ -0,0 +1,34 @@ +using System; +using Microsoft.Office.Tools.Outlook; + +namespace OutlookTelegramNotifier +{ + public partial class ThisAddIn + { + private OutlookTelegramNotifier notifier; + + private void ThisAddIn_Startup(object sender, System.EventArgs e) + { + notifier = new OutlookTelegramNotifier(); + } + + private void ThisAddIn_Shutdown(object sender, System.EventArgs e) + { + // Cleanup code here + } + + #region VSTO generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InternalStartup() + { + this.Startup += new System.EventHandler(ThisAddIn_Startup); + this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); + } + + #endregion + } +} \ No newline at end of file