/// <inheritdoc /> protected override async Task Handle(BotActionRequest <UnsubscribeAction> request, CancellationToken cancellationToken) { var chat = request.Update.Message.Chat; var channel = await _channelStore.GetAsync(request.Action.Channel, cancellationToken); if (channel == null) { var message = Localizer.GetString(BotResources.UnsubscribeFailedChannelNotFound); await Reply.SendAsync(new SendTelegramReply(chat.Id, message), cancellationToken); return; } var existingSubscription = await _subscriptionsStore.GetAsync(chat.Id, request.Action.Channel, cancellationToken); if (existingSubscription == null) { var message = Localizer.GetString(BotResources.UnsubscribeFailedNotSubscribed); await Reply.SendAsync(new SendTelegramReply(chat.Id, message), cancellationToken); return; } await _subscriptionsStore.RemoveAsync(existingSubscription, cancellationToken); var replyMessage = Localizer.GetString(BotResources.UnsubscribeSucceed); await Reply.SendAsync(new SendTelegramReply(chat.Id, replyMessage), cancellationToken); }
/// <inheritdoc /> protected override async Task Handle(BotActionRequest <SubscriptionsAction> request, CancellationToken cancellationToken) { var chatId = request.Message.Chat.Id; var subscriptions = await _subscriptionsStore.GetAsync(chatId, cancellationToken); if (subscriptions.Count < 1) { var notSubscribedText = Localizer.GetString(BotResources.ChatSubscriptionsNotFound); await Reply.SendAsync(new SendTelegramReply(chatId, notSubscribedText), cancellationToken); return; } var subscribedText = Localizer.GetString(BotResources.ChatSubscriptions); var messageBuilder = new StringBuilder(subscribedText) .AppendLines(count: 2); foreach (var(index, subscription) in subscriptions.Index()) { var command = $"/unsubscribe_{subscription.Channel.Replace('-', '_')}"; messageBuilder.AppendLine($"{index + 1}. {subscription.Channel} ({command})"); } await Reply.SendAsync(new SendTelegramReply(chatId, messageBuilder.ToString()), cancellationToken); }
/// <inheritdoc /> protected override async Task Handle(BotActionRequest <StartAction> request, CancellationToken cancellationToken) { var text = Localizer.GetString(BotResources.StartText); var messageRequest = new SendTelegramReply(request.Chat.Id, text.EscapeMarkdown()) { ParseMode = ParseMode.MarkdownV2, DisableWebPagePreview = true }; await Reply.SendAsync(messageRequest, cancellationToken); }
/// <inheritdoc /> protected override async Task Handle(BotActionRequest <SubscribeAction> request, CancellationToken cancellationToken) { var chat = request.Update.Message.Chat; var channel = await _channelStore.GetAsync(request.Action.Channel, cancellationToken); if (channel == null) { var message = Localizer.GetString(BotResources.SubscribeFailedChannelNotFound); await Reply.SendAsync(new SendTelegramReply(chat.Id, message), cancellationToken); return; } var subscriptionExists = await _subscriptionsStore.ExistsAsync(request.Action.Channel, chat.Id, cancellationToken); if (subscriptionExists) { var message = Localizer.GetString(BotResources.SubscribeFailedAlreadySubscribed); await Reply.SendAsync(new SendTelegramReply(chat.Id, message), cancellationToken); return; } var subscription = new AlertsSubscription { Channel = request.Action.Channel, ChatId = chat.Id, ChatName = chat.Username != null ? $"@{chat.Username}" : chat.Title }; await _subscriptionsStore.StoreAsync(subscription, cancellationToken); var replyMessage = Localizer.GetString(BotResources.SubscribeSucceed); await Reply.SendAsync(new SendTelegramReply(chat.Id, replyMessage), cancellationToken); }
/// <inheritdoc /> protected override async Task Handle(BotActionRequest <ChannelsAction> request, CancellationToken cancellationToken) { var chatId = request.Message.Chat.Id; var channels = await _channelStore.GetAllAsync(cancellationToken); if (channels.Count < 1) { var notSubscribedText = Localizer.GetString(BotResources.ChannelsNotFound); var replyRequest = new SendTelegramReply(chatId, notSubscribedText); await Reply.SendAsync(replyRequest, cancellationToken); return; } var subscribedText = Localizer.GetString(BotResources.Channels); var messageBuilder = new StringBuilder(subscribedText) .AppendLines(count: 2); foreach (var(index, channel) in channels.Index()) { var userSubscribed = await _subscriptionsStore.ExistsAsync(channel.Name, chatId, cancellationToken); var descriptionSuffix = string.IsNullOrWhiteSpace(channel.Description) ? string.Empty : $" — {channel.Description}"; var subscribeSuffix = userSubscribed ? string.Empty : $" (/subscribe_{channel.Name.Replace('-', '_')})"; messageBuilder.AppendLine($"{index + 1}. {channel.Name}{descriptionSuffix}{subscribeSuffix}"); } await Reply.SendAsync(new SendTelegramReply(chatId, messageBuilder.ToString()), cancellationToken); }