Example #1
0
        /// <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);
        }
Example #2
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);
        }