public async Task StartAsync(UpdateMessage message) { var callbackQueryData = message.CallbackQuery.Data.Split(';'); var occasionId = callbackQueryData[1]; var occasion = _occasionRepository.FindById(occasionId); if (occasion == null) { return; } var activePolls = _pollRepository.Find(x => x.ChatId == occasion.ChatId && !x.IsClosed).ToList(); if (activePolls.Any()) { await _telegramClient.SendMessageAsync(new SendMessage { ChatId = occasion.ChatId, Text = Messages.ActivePollRunning }); return; } var sendPollMessage = TelegramMessageFactory.CreateSendPollMessage(occasion.ChatId, occasion.Name); var pollMessage = await _telegramClient.SendPollAsync(sendPollMessage); var entity = new PollEntity { PartitionKey = occasion.ChatId, RowKey = pollMessage.Poll.Id, Timestamp = _dateTimeService.TableEntityTimeStamp, ChatId = occasion.ChatId, OccasionId = occasionId, MessageId = pollMessage.MessageId.ToString(), IsClosed = false }; await _pollRepository.CreateAsync(entity); await _telegramClient.AnswerCallbackQuery(new AnswerCallbackQuery { CallBackQueryId = message.CallbackQuery.Id, Text = Messages.PollStarted }); }
// FRONTEND public ActionResult Question(int questionId) { //check if user is answered the poll var ip = Request.UserHostAddress; var userAnswered = _pollRepository.CheckUserAnswered(questionId, ip); if (userAnswered) { return(Result(questionId)); } else { var now = DateTime.Now; var question = _pollRepository.Find(questionId); if (!question.Done && question.FromDate <= now && (question.EndDate == null || question.EndDate >= now)) { return(PartialView("_Question", question)); } else { return(Result(questionId)); } } }
private async Task <Tuple <int, IEnumerable <ChatMemberStat> > > GetStatAsync(string chatId) { var occasions = _occasionRepository.GetByChatId(chatId); var polls = new List <PollEntity>(); foreach (var occasionEntity in occasions) { var occasionPolls = _pollRepository.Find(x => x.OccasionId == occasionEntity.RowKey && x.IsClosed); polls.AddRange(occasionPolls); } if (!polls.Any()) { await _telegramClient.SendMessageAsync(new SendMessage { ChatId = chatId, Text = Messages.NoOcassionsForStats, }); return(new Tuple <int, IEnumerable <ChatMemberStat> >(0, Enumerable.Empty <ChatMemberStat>())); } var answers = new List <PollAnswerEntity>(); foreach (var pollEntity in polls) { var yesAnswers = _pollAnswerRepository.Find(x => x.PollId == pollEntity.RowKey && x.IsYesAnswer).ToList(); answers.AddRange(yesAnswers); } var answersByUser = answers.GroupBy(x => x.UserId) .ToDictionary(x => x.Key, y => y.Count()) .OrderByDescending(x => x.Value); var result = new List <ChatMemberStat>(); foreach (var kvp in answersByUser) { var firstName = string.Empty; var lastName = string.Empty; var userInfo = await _telegramClient.GetChatMemberAsync(chatId, kvp.Key); if (userInfo == null) { firstName = kvp.Key; } else { firstName = userInfo.User.FirstName; lastName = userInfo.User.LastName; } var percentage = (int)(((double)kvp.Value / (double)polls.Count) * 100); result.Add(new ChatMemberStat { FirstName = firstName, LastName = lastName, TotalVisited = kvp.Value, Percentage = percentage }); } return(new Tuple <int, IEnumerable <ChatMemberStat> >(polls.Count, result)); }