plagin_outlook/OutlookTelegramNotifier.cs

76 lines
3.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
);
}
}
}