private void UpdateLatest(TeleBot bot, TelegramSubscription subscription) { try { foreach (var coinId in subscription.LastSignificantPrice.Keys) { var latest = bot.Ctb.Exchanges[subscription.ExchangeId][coinId]?.Clone( ); while (latest?.Average == 0) { latest = bot.Ctb.Exchanges[subscription.ExchangeId][coinId]?.Clone( ); } if (latest != null) { PriceChanges[coinId.ToString( )] = latest - subscription.LastSignificantPrice[coinId]; } else { PriceChanges[coinId.ToString( )] = null; } CurrentPrice[coinId.ToString( )] = latest; } } catch (Exception e) { Logger.Error(e); } }
private void SendSubscriptionReply( TelegramSubscription subscription, CryptoCoin oldValue, CryptoCoin newValue ) { if (oldValue is null) { return; } var change = newValue - oldValue; var builder = new StringBuilder( ); builder .AppendLine($"{subscription.Exchange.Name,-14} {newValue.Symbol}") .AppendLine($"Current Price: {subscription.Exchange[newValue.Id].Average:C}") .AppendLine($"Change: {change.Value.ToCurrency ( )}") .AppendLine($"Change %: {change.Percentage:P}") .AppendLine($"in {change.TimeDiff:dd\\:hh\\:mm\\:ss}"); bot.SendTextMessageAsync( subscription.ChatId, $"```\n{builder}\n```", ParseMode.Markdown ); }
private void StartSubscription(CryptoExchangeBase exchange, TeleSubscription sub) { var subscription = new TelegramSubscription(exchange, sub); subscription.Changed += SendSubscriptionReply; subscription.Changed += async(s, o, n) => await UpdateSubscriptionInDb(s, n).ConfigureAwait(false); exchange.Subscribe(subscription); lock (subscriptionLock) Subscriptions.Add(subscription); }
private static Task UpdateSubscriptionInDb( TelegramSubscription subscription, CryptoCoin coin ) { UnitOfWork.Do(u => { var ccv = coin.ToCryptoCoinValue(subscription.ExchangeId); u.Subscriptions.UpdateCoin(subscription.Id, ccv); } ); return(Task.CompletedTask); }
public void SubOnUpdated(TelegramSubscription sub, CryptoCoin _, CryptoCoin __) => Send("TeleSubscriptionUpdates", new TeleBotSubscriptionSummary(Bot, sub.Id));