Exemplo n.º 1
0
        public async Task StopAllActivePollsAsync(UpdateMessage message)
        {
            var chatId = message.Message.Chat.Id;
            var fromId = message.Message.From.Id.ToString();

            var isAdmin = await _telegramClient.IsAdminAsync(chatId, fromId);

            if (!isAdmin)
            {
                await _telegramClient.SendMessageAsync(new SendMessage
                {
                    ChatId = chatId,
                    Text   = Messages.OnlyAdminCanDoThis,
                });

                return;
            }

            var activePolls = _pollRepository.Find(x => x.ChatId == chatId && !x.IsClosed).ToList();

            if (!activePolls.Any())
            {
                await _telegramClient.SendMessageAsync(new SendMessage
                {
                    ChatId = chatId,
                    Text   = Messages.NoActivePollRunning
                });

                return;
            }

            var occasions = _occasionRepository.GetByChatId(chatId).ToList();

            foreach (var activePoll in activePolls)
            {
                var stopPollMessage =
                    TelegramMessageFactory.CreateStopPollMessage(activePoll.ChatId, activePoll.MessageId);
                await _telegramClient.StopPollAsync(stopPollMessage);

                activePoll.IsClosed = true;
                await _pollRepository.UpdateAsync(activePoll);

                var pollOccasion = occasions.FirstOrDefault(x => x.RowKey == activePoll.OccasionId);
                if (pollOccasion == null)
                {
                    continue;
                }

                await _telegramClient.SendMessageAsync(new SendMessage
                {
                    ChatId = chatId,
                    Text   = string.Format(Messages.ActivePollStopped, pollOccasion.Name)
                });
            }
        }