public override async Task <IHandlerResult <StartViewModel> > Handle( StartCommand request, CancellationToken cancellationToken) { _logger.Information("StartCommand invoked."); if (request.IsBot) { _logger.Fatal("Bot attempted to subscribe!"); return(ValidationFailed("Sorry, no bots here!")); } if (request.ChatId <= 0) { _logger.Warning($"Bad parameter: {nameof(ChatId)}!"); return(ValidationFailed($"Bad parameter: {nameof(ChatId)}!")); } var dbUser = await _userRepository.GetFirstAsync(u => u.ChatId == request.ChatId); if (!(dbUser is null)) { var message = dbUser.IsSubscribed ? "were" : "were not"; return(ValidationFailed( $"You already registered to the bot previously and {message} subscribed to updates.")); } var newUser = _mapper.Map <User>(request); newUser.IsSubscribed = true; newUser.RegisteredAt = DateTime.Now; await _userRepository.CreateAsync(newUser); var tours = await _toursFetcher.FetchToursAsync(true, 10); foreach (var tour in tours) { if (tour.ImgUrl != null) { var uri = new Uri(tour.ImgUrl); await _telegram.SendPhotoAsync(request.ChatId, new InputOnlineFile(uri), cancellationToken : cancellationToken); } if (tour.Text != null) { await _telegram.SendTextMessageAsync(request.ChatId, tour.Text, cancellationToken : cancellationToken); } } return(Ok()); }
private async void DoWork(object state) { try { var tours = await _toursFetcherService.FetchToursAsync(false, 10); var command = new NotifyUpdatesCommand { Tours = tours }; await _mediator.Send(command); } catch (Exception e) { _logger.Error(e, $"{nameof(ToursNotifierHostedService)} failed."); } }