コード例 #1
0
        private async void DoWork(object sender)
        {
            try
            {
                var client = await bot.Get();

                var updates = await client.GetUpdatesAsync(offset); // получаем массив обновлений

                foreach (var update in updates)                     // Перебираем все обновления
                {
                    offset = update.Id + 1;
                    var commands = bot.GetCommandList();
                    var message  = update.Message;
                    var callback = update.CallbackQuery;

                    if (message != null)
                    {
                        var command = commands.FirstOrDefault(x => x.Contains(message.Text));

                        if (command != null)
                        {
                            command.Execute(message, client);
                            logger.LogInformation("Execute command {0}", update.Message.Text);
                        }
                    }
                    else if (callback != null)
                    {
                        var answerEvent = new EventUserAnsweredQuestion()
                        {
                            UserChatId     = callback.From.Id,
                            AnswerOptionId = new Guid(callback.Data)
                        };
                        publisher.Publish(answerEvent);

                        await client.SendTextMessageAsync(callback.From.Id, String.Format("Ответ сохранен", callback.Message.Text));
                    }
                }
            }
            catch (Telegram.Bot.Exceptions.ApiRequestException ex)
            {
                logger.LogError(ex, "Error {0}", ex.Message);
            }
        }
コード例 #2
0
        public void ReceiveUserAnswer(EventUserAnsweredQuestion @event)
        {
            var userId = GetOrCreateUserId(@event.UserChatId, @event.UserName);

            // проверка на существование ответа
            var userAnswer = _userAnswerRepository.GetList <DMUserAnswer>(x => x.AnswerOptionId == @event.AnswerOptionId);

            if (userAnswer.Count() != 0)
            {
                return;
            }

            // одна комната для пользователя
            var rooms = _roomRepository.GetList <DMRoomUserIds>(x => x.UserIds.Contains(userId));
            var room  = rooms.FirstOrDefault();

            if (room == null)
            {
                return;
            }

            var questions = _questionRepository.GetList <DMMultipleChoiceQuestionOptionIds>(x => x.OptionIds.Contains(@event.AnswerOptionId));
            var question  = questions.FirstOrDefault();

            if (question == null)
            {
                return;
            }

            _userAnswerRepository.Create(new DMUserAnswer()
            {
                Id             = Guid.NewGuid(),
                UserId         = userId,
                RoomId         = room.Id,
                QuestionId     = question.Id,
                AnswerOptionId = @event.AnswerOptionId
            });
        }