public async Task SendRateUpdate(TinkoffExchangeRate parsedRate, TinkoffExchangeRate lastRate) { var chats = await _repository.GetDetailedChatsAsync(parsedRate, lastRate); foreach (var chat in chats) { await SendMessageAsync(chat.Id, $"**UPDATE**\n{parsedRate.From}: {parsedRate.GetDiffMessage(chat.MinThresholdRate + chat.DetailedThreshold)}"); await _repository.UpdateChatInfo(chat.Id, chat.DetailedThreshold, parsedRate.Sell); } }
public async Task SendRateAsync(long chatId, TinkoffExchangeRate currentRate, ITelegramBotClient client = null) { var message = $"{currentRate.From}: {currentRate.Sell}"; try { await SendMessageAsync(chatId, message, client); } catch (Exception ex) { _logger.LogError(ex, $"Error while sending message to chat {chatId}"); } }
/// <inheritdoc/> public async Task <IEnumerable <TelegramChatInfo> > GetDetailedChatsAsync(TinkoffExchangeRate actual, TinkoffExchangeRate previous) { var condition = actual.Sell - previous.Sell > 0 ? new ScanCondition(nameof(TelegramChatInfo.MaxThresholdRate), ScanOperator.LessThanOrEqual, actual.Sell) : new ScanCondition(nameof(TelegramChatInfo.MinThresholdRate), ScanOperator.GreaterThanOrEqual, actual.Sell); var conditions = new[] { new ScanCondition(nameof(TelegramChatInfo.IsEnabled), ScanOperator.Equal, true), new ScanCondition(nameof(TelegramChatInfo.Type), ScanOperator.Equal, ChatInfoType.Chat.ToString()), condition }; var scan = _dynamoDBContext.ScanAsync <TelegramChatInfo>(conditions); var result = new List <TelegramChatInfo>(); while (!scan.IsDone) { result.AddRange(await scan.GetNextSetAsync()); } return(result); }
/// <inheritdoc/> protected override async Task TickAsync() { if (LastRate == null) { var repository = _serviceProvider.GetRequiredService <IRepository>(); LastRate = await repository.GetLastRateAsync(); } var client = new HttpClient { BaseAddress = _configuration.BaseUrl }; var response = await client.GetAsync("v1/currency_rates?from=USD&to=RUB"); var content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { _logger.LogCritical($"Can't get rate from API. Status: {response.StatusCode}; Content: {content}"); return; } var obj = JObject.Parse(content); var rate = obj.SelectToken($"$.payload.rates[?(@.category == '{_configuration.CategoryName}')]"); if (rate == null) { _logger.LogCritical($"Can't get rate width category SavingAccountTransfers. Content: {content}."); return; } var parsedRate = rate.ToObject <TinkoffExchangeRate>(); parsedRate.From = rate.SelectToken("fromCurrency.name").Value <string>(); parsedRate.Updated = DateTime.UnixEpoch.AddMilliseconds(obj.SelectToken("$.payload.lastUpdate.milliseconds").Value <double>()); var diff = Math.Abs(parsedRate.Sell - (LastRate?.Sell ?? parsedRate.Sell)); if (diff == 0) { if (LastRate == null) { await _serviceProvider.GetRequiredService <IRepository>().SaveEntityAsync(parsedRate); LastRate = parsedRate; } else { _logger.LogInformation($"Difference between parsed and last rate is 0."); } return; } await _serviceProvider.GetRequiredService <IRepository>().SaveEntityAsync(parsedRate); _logger.LogInformation($"Rate added to DB: {parsedRate.GetDiffMessage(LastRate)}"); var sender = _serviceProvider.GetRequiredService <TelegramMessageSender>(); await sender.SendRateUpdate(parsedRate, LastRate); LastRate = parsedRate; }