private async Task HandleSubscribeCommandAsync(Message message) { var from = message.From; var(command, parameters) = message.ExtractCommand(Self); if (parameters.Count < 3) { await Client.SendTextBlockAsync(message.Chat, $"Usage:\n{CommandHandlers[command].usage}", cancellationToken : CancellationToken).ConfigureAwait(false); return; } if (!Enums.TryParse <CryptoExchangeId> (parameters[0], true, out var exchangeId)) { await Client.SendTextBlockAsync(message.Chat, $"{parameters[0]} is not a valid Exchange name", cancellationToken : CancellationToken).ConfigureAwait(false); return; } if (!decimal.TryParse(parameters[1].Trim('%'), out var threshold)) { await Client.SendTextBlockAsync(message.Chat, $"{parameters[1]} is not a valid percentage value", cancellationToken : CancellationToken).ConfigureAwait(false); return; } var index = bool.TryParse(parameters[2].ToLower( ), out var isSilent) ? 3 : 2; var subscription = new TelegramPercentChangeSubscription( message.Chat, from, exchangeId, threshold / 100m, isSilent, parameters .Skip(index) .Select(x => x.Trim(' ', ',')) ); await AddOrUpdateSubscriptionAsync(subscription).ConfigureAwait(false); }
public EditSubscriptionPage(IMenu menu, TelegramPercentChangeSubscription subscription, IPage previousPage) : base("Edit Subscription", menu, previousPage: previousPage) { Subscription = subscription; Title = $"Edit Subscription:\n{subscription.Summary ( )}"; Labels = new[] { "change silence mode", "change threshold", "add symbols", "remove symbols" } .Batch(2) .ToList( ); AddWideLabel("delete"); AddWideLabel("back"); BuildKeyboard( ); AddHandlers( ); }
private async Task AddSubscriptionHandlerAsync(CallbackQuery query) { var exchangeId = await RunExchangeSelectionPageAsync( ).ConfigureAwait(false); if (!exchangeId) { return; } await Menu.RequestReplyAsync("Enter the threshold%").ConfigureAwait(false); var threshold = await ReadPercentageAsync( ).ConfigureAwait(false); if (threshold is null) { return; } var isSilent = await RunSelectionPageAsync(new[] { "yes", "no" }.Batch ( 2 ), "Keep Silent?") .ConfigureAwait(false); if (!isSilent) { return; } var symbols = await ReadSymbolsAsync( ).ConfigureAwait(false); var subscription = new TelegramPercentChangeSubscription( Chat, User, exchangeId.Result, threshold.Value, isSilent.Result == "yes", symbols ); await TelegramBot.AddOrUpdateSubscriptionAsync(subscription).ConfigureAwait(false); await RedrawAsync( ).ConfigureAwait(false); }
public async Task AddOrUpdateSubscriptionAsync(TelegramPercentChangeSubscription subscription) { var list = Data.GetPercentChangeSubscriptions(x => x.IsSimilarTo(subscription)); var existing = list.SingleOrDefault(x => x.Threshold == subscription.Threshold); if (existing is null) { Data.AddOrUpdate(subscription); subscription.Trigger += async(sub, old, current) => Data.AddOrUpdate(sub as TelegramPercentChangeSubscription); await subscription.StartAsync(this, true).ConfigureAwait(false); return; } await existing.MergeWithAsync(subscription).ConfigureAwait(false); Data.AddOrUpdate(existing); }
public bool AddOrUpdate(TelegramPercentChangeSubscription subscription) => PercentChangeSubscriptions.AddOrUpdate(subscription);