public bool MessageHandler(Message message) { if (message == null) { return(false); } // log income message _logRepository.Add(message); if (message.Chat == null) { return(false); } var chatId = message.Chat.Id; if (chatId <= 0) { return(false); } var type = Enum.GetName(typeof(MessageType), message.Type); _logger.LogInformation($"[{chatId}] [{type}] [{message.Text ?? ""}] Received message"); Action action; // Fix other highPriorityMessage // ReSharper disable once SwitchStatementMissingSomeCases switch (message.Type) { case MessageType.Contact: case MessageType.Text: { action = CreateCommand(message); break; } default: { var queueMessageFactory = new QueueMessageFactory(message); action = _generalCommandsFactory.Command(queueMessageFactory.WrongMessage); break; } } try { action.Invoke(); return(true); } catch (Exception e) { _logger.LogError($"[{chatId}] [{type}] [{message.Text ?? ""}] MessageHandlerAsync Error.", e); return(false); } }
public Action AddIsStudentCommand(Message message, QueueMessage nextTrueMessage, QueueMessage nextFalseMessage) { return(() => { var answer = message.Text; var chatId = message.Chat.Id; bool?isStudent = null; bool?isAnsweredAllQuestions = null; if (Compare(answer, "да")) { isStudent = true; } if (Compare(answer, "нет")) { isStudent = false; isAnsweredAllQuestions = true; } _userRepository.SaveStudentOrNot(chatId, isStudent, isAnsweredAllQuestions); if (!isStudent.HasValue) { var factory = new QueueMessageFactory(message); _queueService.Add(factory.AskIsStudentMessage); } if (isStudent.HasValue && isStudent.Value) { _queueService.Add(nextTrueMessage); } else { if (!_resultRepository.IsQuizMember(chatId)) { _userRepository.SetQuizMember(chatId, null); } _queueService.Add(nextFalseMessage); } }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ConfigureLogging(services); services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "AsyncProcessor.Web.API", Version = "v1" }); }); var encoder = new JsonEncoder(); services.AddSingleton <IEncoder>(encoder); services.AddSingleton <IDecoder>(encoder); services.AddSingleton <IQueueMessageFactory>(ctx => { var factory = new QueueMessageFactory(ctx.GetRequiredService <IEncoder>()); factory.RegisterMessageIdGenerator <ProcessIncoming>(p => p.Id); return(factory); }); services.AddSingleton <IConnectionFactory>(ctx => { var rabbitConfig = this.Configuration.GetSection("RabbitMQ"); var connectionFactory = new ConnectionFactory() { HostName = rabbitConfig["HostName"], UserName = rabbitConfig["UserName"], Password = rabbitConfig["Password"], Port = AmqpTcpEndpoint.UseDefaultPort }; return(connectionFactory); }); services.AddSingleton <IBusConnection, RabbitPersistentConnection>(); services.AddScoped <IPublisher>(ctx => { var connection = ctx.GetRequiredService <IBusConnection>(); var encoder = ctx.GetRequiredService <IEncoder>(); var logger = ctx.GetRequiredService <ILogger <RabbitPublisher> >(); var rabbitConfig = this.Configuration.GetSection("RabbitMQ"); var exchangeName = rabbitConfig["Exchange"]; return(new RabbitPublisher(connection, exchangeName, encoder, logger)); }); services.AddSingleton(ctx => { var mongoCfg = Configuration.GetSection("Mongo"); var connStr = mongoCfg["ConnectionString"]; return(new MongoClient(connectionString: connStr)); }) .AddSingleton(ctx => { var mongoCfg = Configuration.GetSection("Mongo"); var dbName = mongoCfg["DbName"]; var client = ctx.GetRequiredService <MongoClient>(); var database = client.GetDatabase(dbName); return(database); }).AddSingleton <IDbContext, DbContext>(); services.AddMediatR(typeof(Queries.ProcessedRequestById)); services.AddHealthChecks() //TODO: ensure connections are not spawned more than necessary .AddRabbitMQ(connectionFactory: (ctx) => ctx.GetRequiredService <IConnectionFactory>().CreateConnection(), name: "rabbit", tags: new[] { "infrastructure" }); }
private Action CreateCommand(Message message) { var chatId = message.Chat.Id; var user = _users.Get(chatId); var factory = new QueueMessageFactory(message); if (user == null) { return(_generalCommandsFactory.StartCommand(factory.WelcomeMessage)); } // handle contact message // ReSharper disable once SwitchStatementMissingSomeCases switch (message.Type) { case MessageType.Contact when user.Phone is null: return(_generalCommandsFactory.AddContactCommand(message, factory.AskNameMessage(message.Contact.FirstName))); case MessageType.Contact: return(_generalCommandsFactory.Command(factory.WrongMessage)); } // handle text messages if (user.Phone == null) { return(_generalCommandsFactory.Command(factory.AskContactMessage)); } if (user.Name == null) { return(_generalCommandsFactory.AddNameCommand(message, factory.AskStartQuizMessage)); } // ReSharper disable once ConvertIfStatementToSwitchStatement if (user.IsQuizMember == null) { return(_questionCommandFactory.AddQuizCommand(user, message, user.IsAnsweredAll ? factory.EndMessage : factory.AskEmail, factory.AskStartQuizMessage)); } if (user.IsQuizMember is true) { // add handler to quiz return(_questionCommandFactory.QuestionCommand(message, user.IsAnsweredAll ? factory.EndMessage : factory.AskEmail, user.IsAnsweredAll)); } if (user.EMail == null) { return(_generalCommandsFactory.AddEMailCommand(message, factory.AskProfessionMessage)); } if (user.Profession == null) { return(_generalCommandsFactory.AddProfessionCommand(message, factory.AskIsStudentMessage)); } // ReSharper disable once ConvertIfStatementToSwitchStatement if (user.IsStudent == null) { return(_generalCommandsFactory.AddIsStudentCommand( message, factory.AskUniversityMessage, _resultRepository.IsQuizMember(chatId) ? factory.EndMessage : factory.AskStartQuizMessageAgain)); } if (user.IsStudent is true && user.University == null) { return(_generalCommandsFactory.AddUniversityCommand(message, factory.AskCourse)); } // ReSharper disable once ConvertIfStatementToReturnStatement if (user.IsStudent is true && user.Course == null) { return(_generalCommandsFactory.AddCourseCommand( message, _resultRepository.IsQuizMember(chatId) ? factory.EndMessage : factory.AskStartQuizMessageAgain, factory.AskCourse)); } // ReSharper disable once ConvertIfStatementToReturnStatement if (user.IsAnsweredAll) { return(_generalCommandsFactory.Command(factory.EndMessage)); } return(_generalCommandsFactory.Command(factory.WrongMessage)); }