322 lines
13 KiB
C#
322 lines
13 KiB
C#
using Newtonsoft.Json;
|
||
using Telegram.Bot;
|
||
using Telegram.Bot.Polling;
|
||
using Telegram.Bot.Types;
|
||
using Telegram.Bot.Types.Enums;
|
||
using Telegram.Bot.Types.ReplyMarkups;
|
||
|
||
namespace nsszcontactbot;
|
||
|
||
public class Program
|
||
{
|
||
private static ITelegramBotClient Bot;
|
||
private static List<Employee> Employees = new();
|
||
private static List<string> Departments = new();
|
||
private static int CurrentPage = 0; // Текущая страница для пагинации
|
||
private static int PageSize = 5; // Количество подразделений на странице
|
||
|
||
public static async Task Main(string[] args)
|
||
{
|
||
Bot = new TelegramBotClient("6237449447:AAEGMmpx-1hUEApFZZS_ySAMZErseKJ8dHo"); // Укажите ваш токен
|
||
List<BotCommand> cmds = new();
|
||
await Bot.SetMyCommandsAsync(cmds);
|
||
LoadEmployees();
|
||
var me = await Bot.GetMeAsync();
|
||
Console.WriteLine($"Бот запущен: @{me.Username}");
|
||
|
||
// Запуск обработки обновлений
|
||
var cancellationToken = new CancellationTokenSource().Token;
|
||
ReceiverOptions receiverOptions =
|
||
new()
|
||
{
|
||
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types except ChatMember related updates
|
||
};
|
||
Bot.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions: receiverOptions, cancellationToken: cancellationToken);
|
||
|
||
Console.WriteLine("Нажмите Enter для выхода...");
|
||
Console.ReadLine();
|
||
}
|
||
|
||
private static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||
{
|
||
if (update.Type == UpdateType.Message && update.Message?.Text != null)
|
||
{
|
||
var message = update.Message;
|
||
|
||
if (message.Text.StartsWith("/start"))
|
||
{
|
||
await StartCommand(message);
|
||
}
|
||
else if (message.Text.StartsWith("/choose_department"))
|
||
{
|
||
await ChooseDepartment(message);
|
||
}
|
||
else if (message.ReplyToMessage != null && message.ReplyToMessage.Text!.StartsWith("Выберите подразделение"))
|
||
{
|
||
await HandleDepartmentSelection(message);
|
||
}
|
||
else
|
||
{
|
||
await HandleSearchInput(message);
|
||
}
|
||
}
|
||
else if (update.Type == UpdateType.CallbackQuery)
|
||
{
|
||
var message = update.CallbackQuery.Message;
|
||
string data = update.CallbackQuery.Data ?? string.Empty;
|
||
string[] dataParts = data.Split('/');
|
||
if (dataParts.Length > 0)
|
||
{
|
||
switch (dataParts[0])
|
||
{
|
||
case "addcontact":
|
||
{
|
||
await HandleAddContact(update.CallbackQuery, dataParts[1]);
|
||
break;
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
private static async Task StartCommand(Message message)
|
||
{
|
||
string text = "Привет! Я бот для поиска контактов сотрудников. Введите запрос.\n";
|
||
//text += "/choose_department - Выбрать подразделение\n";
|
||
await Bot.SendTextMessageAsync(message.Chat.Id, text);
|
||
}
|
||
|
||
private static async Task ChooseDepartment(Message message)
|
||
{
|
||
Departments = Employees.Select(e => e.Department).Distinct().ToList();
|
||
CurrentPage = 0; // Сброс страницы
|
||
await ShowDepartments(message.Chat.Id);
|
||
}
|
||
|
||
private static async Task ShowDepartments(long chatId)
|
||
{
|
||
var totalDepartments = Departments.Count;
|
||
var totalPages = (int)Math.Ceiling((double)totalDepartments / PageSize);
|
||
var departmentsToShow = Departments.Skip(CurrentPage * PageSize).Take(PageSize).ToList();
|
||
|
||
string text = "Выберите подразделение:\n";
|
||
text += string.Join("\n", departmentsToShow.Select((d, i) => $"{i + 1 + CurrentPage * PageSize}. {d}"));
|
||
|
||
// Создание кнопок
|
||
var inlineKeyboard = new List<List<InlineKeyboardButton>>();
|
||
if (CurrentPage > 0)
|
||
{
|
||
inlineKeyboard.Add(new List<InlineKeyboardButton>
|
||
{
|
||
InlineKeyboardButton.WithCallbackData("◀️ Назад", "prev")
|
||
});
|
||
}
|
||
if (CurrentPage < totalPages - 1)
|
||
{
|
||
inlineKeyboard.Add(new List<InlineKeyboardButton>
|
||
{
|
||
InlineKeyboardButton.WithCallbackData("Вперед ▶️", "next")
|
||
});
|
||
}
|
||
|
||
var keyboardMarkup = new InlineKeyboardMarkup(inlineKeyboard);
|
||
|
||
await Bot.SendTextMessageAsync(chatId, text, replyMarkup: keyboardMarkup);
|
||
}
|
||
|
||
private static async Task HandleSearchInput(Message message)
|
||
{
|
||
if (!string.IsNullOrEmpty(message.Text))
|
||
{
|
||
string query = message.Text.Trim().ToLower();
|
||
if (query.Length < 3)
|
||
{
|
||
await Bot.SendTextMessageAsync(message.Chat.Id, "Минимальная длина строки поиска - 3 символа.");
|
||
return;
|
||
}
|
||
|
||
// Поиск сотрудников по полям
|
||
var results = Employees.Where(e =>
|
||
e.FullName.ToLower().Contains(query) ||
|
||
(e.Mail != null && e.Mail.ToLower().Contains(query)) ||
|
||
(e.Phone != null && e.Phone.Contains(query)) ||
|
||
(e.Mobile != null && e.Mobile.Contains(query)) ||
|
||
(e.IP != null && e.IP.Contains(query))
|
||
).OrderBy(e => e.FullName.ToLower().IndexOf(query)).ToList();
|
||
|
||
// Отправка результатов поиска
|
||
await ShowEmployeeResults(message.Chat.Id, results);
|
||
}
|
||
}
|
||
|
||
private static async Task HandleAddContact(CallbackQuery callbackQuery, string id)
|
||
{
|
||
if (callbackQuery.Message != null)
|
||
{
|
||
var employee = Employees.Where(e => e.Id == id).First();
|
||
if (employee != null)
|
||
{
|
||
EmployeeContact contact = new(employee);
|
||
string phoneNumber = !string.IsNullOrEmpty(contact.Mobile) ? contact.Mobile : contact.Phone;
|
||
var replyParameters = new ReplyParameters() { MessageId = callbackQuery.Message.MessageId, QuoteParseMode = ParseMode.Html, Quote = $"<b>{contact.FullName}</b>" };
|
||
await Bot.SendContactAsync(callbackQuery.Message.Chat.Id, phoneNumber, contact.FirstName, lastName: contact.LastName, vcard: contact.VCard, replyParameters: replyParameters);
|
||
await Task.Delay(999);
|
||
}
|
||
}
|
||
await Bot.AnswerCallbackQueryAsync(callbackQuery.Id);
|
||
}
|
||
|
||
private static async Task HandleDepartmentSelection(Message message)
|
||
{
|
||
if (message.Text.StartsWith("◀️ Назад"))
|
||
{
|
||
if (CurrentPage > 0)
|
||
{
|
||
CurrentPage--;
|
||
await ShowDepartments(message.Chat.Id);
|
||
}
|
||
}
|
||
else if (message.Text.StartsWith("Вперед ▶️"))
|
||
{
|
||
CurrentPage++;
|
||
await ShowDepartments(message.Chat.Id);
|
||
}
|
||
else
|
||
{
|
||
int selectedIndex = int.Parse(message.Text) - 1;
|
||
if (selectedIndex >= 0 && selectedIndex < Departments.Count)
|
||
{
|
||
string selectedDepartment = Departments[selectedIndex];
|
||
var results = Employees.Where(e => e.Department == selectedDepartment).ToList();
|
||
await ShowEmployeeResults(message.Chat.Id, results);
|
||
}
|
||
else
|
||
{
|
||
await Bot.SendTextMessageAsync(message.Chat.Id, "Некорректный выбор.");
|
||
}
|
||
}
|
||
}
|
||
|
||
private static async Task ShowEmployeeResults(long chatId, List<Employee> results)
|
||
{
|
||
if (results.Any())
|
||
{
|
||
foreach (var employee in results)
|
||
{
|
||
var keyboardMarkup = new InlineKeyboardMarkup()
|
||
//.AddNewRow("1.1", "1.2", "1.3")
|
||
.AddNewRow()
|
||
.AddButton("👤 Контакт", $"addcontact/{employee.Id}");
|
||
//.AddButton("В избранные", $"addfavorite/{employee.Id}");
|
||
//.AddButton(InlineKeyboardButton.WithUrl("Написать письмо", $"mailto:{employee.Mail}"));
|
||
|
||
var card = CreateEmployeeCard(employee);
|
||
string photoPath = $"data/photos/{employee.FullName}.jpg";
|
||
try
|
||
{
|
||
await using var fileStream = new FileStream(photoPath, FileMode.Open, FileAccess.Read);
|
||
await Bot.SendPhotoAsync(chatId, fileStream, caption: card, parseMode: ParseMode.Html, replyMarkup: keyboardMarkup);
|
||
}
|
||
catch (FileNotFoundException)
|
||
{
|
||
await Bot.SendTextMessageAsync(chatId, card, null, ParseMode.Html, replyMarkup: keyboardMarkup);
|
||
}
|
||
finally
|
||
{
|
||
await Task.Delay(999);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
await Bot.SendTextMessageAsync(chatId, "Сотрудники не найдены.");
|
||
}
|
||
}
|
||
|
||
static string GetEventStatus(Employee employee)
|
||
{
|
||
if (employee.StartDate == null || employee.EndDate == null)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
DateTime today = DateTime.Now;
|
||
TimeSpan oneWeekBeforeEvent = (DateTime)employee.StartDate - today;
|
||
|
||
if (employee.StartDate <= today && today <= employee.EndDate)
|
||
{
|
||
// Сотрудник участвует в событии
|
||
return $"🟥 {employee.State} с {employee.StartDate:dd.MM.yyyy} по {employee.EndDate:dd.MM.yyyy}";
|
||
}
|
||
else if (oneWeekBeforeEvent.TotalDays <= 7 && oneWeekBeforeEvent.TotalDays > 0)
|
||
{
|
||
// Событие начнется через неделю
|
||
return $"🟩 {employee.State} с {employee.StartDate:dd.MM.yyyy} по {employee.EndDate:dd.MM.yyyy}";
|
||
}
|
||
|
||
// Если событие не указано или неактуально
|
||
return string.Empty;
|
||
}
|
||
|
||
private static string CreateEmployeeCard(Employee employee)
|
||
{
|
||
EmployeeContact contact = new(employee);
|
||
string card = $"👤 <b>{contact.FullName}</b>\n" +
|
||
$"🏢 {contact.Department}\n" +
|
||
$"🎓 {contact.Position}\n";
|
||
|
||
if (!string.IsNullOrEmpty(contact.Mail))
|
||
card += $"✉️ {contact.Mail}\n";
|
||
|
||
if (!string.IsNullOrEmpty(contact.Mobile))
|
||
card += $"📱 {contact.Mobile}\n";
|
||
if (!string.IsNullOrEmpty(contact.ExtensionNumber))
|
||
card += $"📞 {contact.ExtensionNumber}\n";
|
||
|
||
//if (!string.IsNullOrEmpty(employee.IP))
|
||
// card += $"🌐 {employee.IP}\n";
|
||
|
||
card += GetEventStatus(employee);
|
||
|
||
return card;
|
||
}
|
||
|
||
private static string CreateEmployeeCard2(Employee employee)
|
||
{
|
||
string photoUrl = $"https://contacts.int.nssz.ru/data/photos/{employee.FullName.Replace(" ", "%20")}.jpg"; // Формирование пути к фотографии
|
||
|
||
string card = $"👤 <b>{employee.FullName}</b>\n" +
|
||
$"🏢 Отдел: {employee.Department}\n" +
|
||
$"🎓 Должность: {employee.Position}\n" +
|
||
$"📸 Фото: <a href='{photoUrl}'>Посмотреть фото</a>\n"; // Добавление ссылки на фото
|
||
|
||
if (!string.IsNullOrEmpty(employee.Mail))
|
||
card += $"✉️ Почта: {employee.Mail}\n";
|
||
if (!string.IsNullOrEmpty(employee.Phone))
|
||
card += $"📞 Телефон: {employee.Phone}\n";
|
||
if (!string.IsNullOrEmpty(employee.Mobile))
|
||
card += $"📱 Мобильный: +{employee.Mobile}\n";
|
||
if (!string.IsNullOrEmpty(employee.IP))
|
||
card += $"🌐 IP: {employee.IP}\n";
|
||
|
||
card += GetEventStatus(employee);
|
||
|
||
return card;
|
||
}
|
||
|
||
private static void LoadEmployees()
|
||
{
|
||
// Загрузка данных сотрудников из JSON файла
|
||
var json = System.IO.File.ReadAllText("data/contacts.json");
|
||
Employees = JsonConvert.DeserializeObject<List<Employee>>(json)!;
|
||
}
|
||
|
||
private static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
|
||
{
|
||
Console.WriteLine($"Произошла ошибка: {exception.Message}");
|
||
return Task.CompletedTask;
|
||
}
|
||
}
|