예제 #1
0
        public void ProcessUpdate()
        {
            var update = new Update()
            {
                UpdateId = 123,
                Message  = new Message()
                {
                    Chat = new Chat()
                    {
                        Id = 456
                    },
                    Text = "Hello"
                }
            };
            var response = (SendMessageModel)_updateManager.ProcessUpdate(update);

            Assert.Equal(response.ChatId, update.Message.Chat.Id);
            Assert.Equal(response.Text, "Hello, I'm WeatherBot! Have a nice day!");
        }
예제 #2
0
        /// <summary>
        /// Update processing
        /// </summary>
        public void UpdateProcessing()
        {
            _logger.Debug($"Start update processing...");
            while (_updates.Count > 0)
            {
                var update   = _updates.Dequeue();
                var response = _updateManager.ProcessUpdate(update);
                if (response.GetType() == typeof(BotResponse))
                {
                    var botResponse = (BotResponse)response;
                    var message     = new SendMessageModel();
                    switch (botResponse.Command)
                    {
                    case BotCommands.Weather:
                        var weatherResponse = _weatherService.GetWeather(botResponse.City);
                        message = new SendMessageModel()
                        {
                            ChatId = botResponse.ReceiverId,
                            Text   = GetWeatherMessage(weatherResponse)
                        };
                        if (message.Text != string.Empty)
                        {
                            _messageService.SendMessage(message);
                        }
                        break;

                    case BotCommands.Forecast:
                        var forecast = _weatherService.GetForecast(botResponse.City);
                        message = new SendMessageModel()
                        {
                            ChatId = botResponse.ReceiverId,
                            Text   = GetForecastMessage(forecast)
                        };
                        if (message.Text != string.Empty)
                        {
                            _messageService.SendMessage(message);
                        }
                        break;

                    default:
                        _logger.Error($"Unknown bot command: {botResponse}");
                        break;
                    }
                }
                else
                {
                    _messageService.SendMessage((SendMessageModel)response);
                }
            }
            if (_updates.Count == 0)
            {
                _logger.Debug($"Finish update processing...");
                UpdatesProcessed();
            }
        }