コード例 #1
0
        private async void OnCallbackQuery(object sender, CallbackQueryEventArgs e)
        {
            if (sender == null)
            {
                return;
            }

            try
            {
                await _telegramBotClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id, "Секунду...");
            }
            catch (InvalidParameterException exception)
            {
                Console.WriteLine($"Шеф, все пропало: {exception.Message}");
                return;
            }

            await using var dbContext = new ApplicationDbContext();

            var    chatContext = dbContext.ChatContexts.FirstOrDefault(context => context.ChatId == e.CallbackQuery.Message.Chat.Id);
            string group       = chatContext?.Group;

            if (group == null)
            {
                return;
            }
            var scheduleEntry = dbContext.Schedules.FirstOrDefault(entry => entry.Group == group);

            var schedule = scheduleEntry?.Schedule;

            if (schedule == null)
            {
                string groupId = await _scheduleProvider.GetGroupId(group);

                schedule = await _scheduleProvider.ParseScheduleByGroup(groupId);

                if (schedule == null)
                {
                    await _telegramBotClient.SendTextMessageAsync(e.CallbackQuery.Message.Chat, "Произошла ошибка при получении расписания, извините.");

                    return;
                }

                var scheduleDbEntry = new ScheduleDbEntry()
                {
                    Group      = group,
                    LastUpdate = DateTime.Now,
                    Schedule   = schedule
                };
                dbContext.Schedules.Add(scheduleDbEntry);
            }

            if (e.CallbackQuery.Data == DayType.Today.ToString())
            {
                await _telegramBotClient.SendTextMessageAsync(e.CallbackQuery.Message.Chat, schedule.OutputDay(DayType.Today));
            }
            else if (e.CallbackQuery.Data == DayType.Tomorrow.ToString())
            {
                await _telegramBotClient.SendTextMessageAsync(e.CallbackQuery.Message.Chat, schedule.OutputDay(DayType.Tomorrow));
            }
            else if (e.CallbackQuery.Data.StartsWith(DayType.Date.ToString()))
            {
                await _telegramBotClient.SendTextMessageAsync(e.CallbackQuery.Message.Chat, schedule.OutputDay(DayType.Date, e.CallbackQuery.Data.Remove(0, 5)));
            }

            await dbContext.SaveChangesAsync();
        }
コード例 #2
0
        private async Task CheckSubscribersAsync()
        {
            TimeSpan delay = TimeSpan.FromMinutes(1);

            while (true)
            {
                await using var dbContext = new ApplicationDbContext();

                var currentTime = DateTime.Now;
                var currentDay  = DateTime.Today;

                var stopwatch = Stopwatch.StartNew();

                var subscriptions = dbContext.Subscriptions
                                    .Include(subscription => subscription.ChatContext);

                stopwatch.Stop();
                Console.WriteLine($"Checking subscribers. Time is {DateTime.Now}. Pending subscriptions count is {subscriptions.Count()}. Elapsed time {stopwatch.ElapsedMilliseconds} ms");

                foreach (var subscription in subscriptions)
                {
                    if (subscription.LastNotificationTime + subscription.NotificationInterval > currentTime)
                    {
                        continue;
                    }

                    var chatContext = subscription.ChatContext;
                    var group       = chatContext.Group;

                    var scheduleEntry = dbContext.Schedules.FirstOrDefault(entry => entry.Group == group);

                    var schedule = scheduleEntry?.Schedule;
                    if (schedule == null)
                    {
                        string groupId = await _scheduleProvider.GetGroupId(group);

                        schedule = await _scheduleProvider.ParseScheduleByGroup(groupId);

                        if (schedule == null)
                        {
                            await _telegramBotClient.SendTextMessageAsync(chatContext.GetChat(), "Произошла ошибка при получении расписания, извините.");

                            return;
                        }

                        var scheduleDbEntry = new ScheduleDbEntry()
                        {
                            Group      = group,
                            LastUpdate = DateTime.Now,
                            Schedule   = schedule
                        };
                        dbContext.Schedules.Add(scheduleDbEntry);
                    }

                    await _telegramBotClient.SendTextMessageAsync(chatContext.GetChat(), schedule.OutputDay(DayType.Today));

                    subscription.LastNotificationTime = currentDay + subscription.NotificationTime;
                }

                await dbContext.SaveChangesAsync();

                await Task.Delay(delay);
            }
        }