Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }