diff --git a/Cu.Yemekhane.Bot.Telegram/Cu.Yemekhane.Bot.Telegram.csproj b/Cu.Yemekhane.Bot.Telegram/Cu.Yemekhane.Bot.Telegram.csproj index 9e09d72..ca34c14 100644 --- a/Cu.Yemekhane.Bot.Telegram/Cu.Yemekhane.Bot.Telegram.csproj +++ b/Cu.Yemekhane.Bot.Telegram/Cu.Yemekhane.Bot.Telegram.csproj @@ -8,7 +8,6 @@ - diff --git a/Cu.Yemekhane.Bot.Telegram/Program.cs b/Cu.Yemekhane.Bot.Telegram/Program.cs index 1cf687a..838833c 100644 --- a/Cu.Yemekhane.Bot.Telegram/Program.cs +++ b/Cu.Yemekhane.Bot.Telegram/Program.cs @@ -1,23 +1,21 @@ -using Telegram.Bot; -using Telegram.Bot.Exceptions; -using Telegram.Bot.Extensions.Polling; +using Cu.Yemekhane.Common.Services; +using Cu.Yemekhane.Bot.Telegram.Services; +using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; -using Cu.Yemekhane.Common.Services; -using Cu.Yemekhane.Common; -using Microsoft.Extensions.Caching.Memory; -using Cu.Yemekhane.Common.Models.Data; +using Telegram.Bot.Exceptions; +using Telegram.Bot.Extensions.Polling; var builder = WebApplication.CreateBuilder(args); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddMemoryCache(); var app = builder.Build(); app.MapGet("/ping", () => "pong"); var serviceProvider = builder.Services.BuildServiceProvider(); -var _webApiService = (IWebApiService)serviceProvider.GetService(typeof(IWebApiService)); -var _memoryCache = (IMemoryCache)serviceProvider.GetService(typeof(IMemoryCache)); +var _replyService = serviceProvider.GetService(); string telegramApiToken = Environment.GetEnvironmentVariable("TELEGRAM_API_TOKEN"); var botClient = new TelegramBotClient(telegramApiToken); @@ -35,87 +33,10 @@ async Task handleUpdateAsync(ITelegramBotClient botClient, Update update, Cancel var messageText = update.Message.Text; Console.WriteLine($"Received a '{messageText}' message in chat {chatId}."); - var replyMessage = await generateReply(messageText); + var replyMessage = await _replyService.GenareteReplyMessage(messageText); await botClient.SendTextMessageAsync(chatId, replyMessage); } -const string helpCommand = "Sana Çukurova Üniversitesinin yemekhane menülerine ulaşman konusunda yardımcı olabilirim.\n" + - "/today komutu ile bugünün menüsüne ulaşabilirsin.\n" + - "/tomorrow komutu ile yarının menüsüne ulaşabilirsin.\n" + - "/menu 12.03.2022 ile herhangi bir günün menüsüne ulaşabilirsin.\n" + - "/menu komutunu kullanırken tarih biçimi gün.ay.yıl şeklinde olmalıdır.\n" + - "/source komutu ile projelerin kaynağına ulaşabilirsin.\n"; - -async Task generateReply(string messageText) -{ - var splittedMessage = messageText.Split(' '); - var dateNowForTurkey = DateOnly.FromDateTime(DateTime.UtcNow.AddHours(3)); - string todayAsString = dateNowForTurkey.ToString("dd.MM.yyyy"); - string reply = string.Empty; - switch (splittedMessage[0].ToLower()) - { - case "/start": - reply = "Heyyo! Ben Çukurova Üniversitesi Yemekhane botu,\n" + helpCommand + "\nhttps://github.com/halilkocaoz/cu-yemekhane"; - break; - case "/today": - reply = await getMenuDetailReplyMessage(todayAsString); - break; - case "/tomorrow": - string tomorrowAsString = dateNowForTurkey.AddDays(1).ToString("dd.MM.yyyy"); - reply = await getMenuDetailReplyMessage(tomorrowAsString); - break; - case "/menu": - if (splittedMessage.Length > 1) - { - string selectedDay = splittedMessage[1]; - reply = await getMenuDetailReplyMessage(selectedDay); - } - else - reply = $"Tarih formatında veri girmelisiniz. Örnek:\n/menu {todayAsString}"; - break; - case "/source": - reply = "https://github.com/halilkocaoz/cu-yemekhane"; - break; - case "/help": - reply = helpCommand; - break; - default: - reply = "Seni anlamadım. Komutlarımı görmek için /help yazabilirsin."; - break; - }; - return reply; -} - -async Task>> getMenusResponseFromCache() -{ - const string cacheKey = "menus_response_cache"; - if (!_memoryCache.TryGetValue(cacheKey, out Cu.Yemekhane.Common.ApiResponse> response)) - { - response = await _webApiService.GetMenus(); - _memoryCache.Set(cacheKey, response, new MemoryCacheEntryOptions - { - AbsoluteExpiration = DateTime.Now.AddHours(6) - }); - } - return response; -} - -async Task getMenuDetailReplyMessage(string date) -{ - string menuDetailReply = string.Empty; - if (date.IsParseableAsDate()) - { - var menusResponse = await getMenusResponseFromCache(); - var selectedMenu = menusResponse.Data?.FirstOrDefault(x => x.Date == date); - menuDetailReply = selectedMenu is not null - ? selectedMenu.Detail - : $"{date} tarihi için menü bulunamadı."; - } - else - menuDetailReply = ErrorMessages.InvalidDateFormat; - - return menuDetailReply; -} Task handleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) { var ErrorMessage = exception switch