Exemplo n.º 1
0
        private async Task GetDroplets(Message message)
        {
            _sessionRepo.Update(message.From.Id, session =>
            {
                session.State = SessionState.WaitChooseDropletsForFirewall;
            });

            var digitalOceanApi = _digitalOceanClientFactory.GetInstance(message.From.Id);
            var droplets        = await digitalOceanApi.Droplets.GetAll();

            if (droplets.Count == 0)
            {
                await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, "You dont have a droplets \U0001F914", ParseMode.Markdown);

                return;
            }

            var checkListPage = _checkListPageFactory.GetInstance <Droplet, DropletCheckList>();
            var pageModel     = checkListPage.GetCheckListPage(droplets, false);

            var sendMessage = await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, pageModel.Message, replyMarkup : pageModel.Keyboard);

            _handlerCallbackRepo.Update(message.From.Id, calllback =>
            {
                calllback.MessageId   = sendMessage.MessageId;
                calllback.UserId      = message.From.Id;
                calllback.HandlerType = GetType().FullName;
            });
        }
        private async Task GetDroplets(Message message)
        {
            _sessionRepo.Update(message.From.Id, session =>
            {
                session.State = SessionState.WaitChooseDropletsForFirewall;
            });

            var fireWallId      = (string)_sessionRepo.Get(message.From.Id).Data;
            var digitalOceanApi = _digitalOceanClientFactory.GetInstance(message.From.Id);
            var fireWall        = await digitalOceanApi.Firewalls.Get(fireWallId);

            if (fireWall.DropletIds == null || fireWall.DropletIds.Count == 0)
            {
                await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, "Firewall don't have related droplets  \U0001F914");

                return;
            }

            var droplets = await digitalOceanApi.Droplets.GetAll();

            var checkListPage = _checkListPageFactory.GetInstance <Responses.Droplet, DropletCheckList>();
            var pageModel     = checkListPage.GetCheckListPage(droplets.Where(d => fireWall.DropletIds.Contains(d.Id)), false);

            var sendMessage = await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, pageModel.Message, ParseMode.Markdown, replyMarkup : pageModel.Keyboard);

            _handlerCallbackRepo.Update(message.From.Id, callback =>
            {
                callback.MessageId   = sendMessage.MessageId;
                callback.UserId      = message.From.Id;
                callback.HandlerType = GetType().FullName;
            });
        }
        private async Task AddDropletsToFirewall(Message message)
        {
            var outboundRules = new List <Requests.OutboundRule>();
            var userRules     = message.Text.Split(';');

            foreach (var rule in userRules)
            {
                var ruleData = rule.Split(':');
                if (ruleData.Length == 3)
                {
                    var outboundRule = new Requests.OutboundRule
                    {
                        Protocol     = ruleData[0],
                        Ports        = ruleData[1],
                        Destinations = new Requests.SourceLocation
                        {
                            Addresses = ruleData[2].Split(',').ToList()
                        }
                    };

                    outboundRules.Add(outboundRule);
                }
                else
                {
                    await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, $"Invalid rule: {rule}\nPlease, try again");

                    return;
                }
            }

            var digitalOceanApi = _digitalOceanClientFactory.GetInstance(message.From.Id);
            var droplets        = await digitalOceanApi.Droplets.GetAll();

            var checkList = _checkListFactory.GetInstance <Responses.Droplet, DropletCheckList>();
            var pageModel = checkList.GetCheckListPage(droplets, true);

            _sessionRepo.Update(message.From.Id, session =>
            {
                var firewall           = session.Data.CastObject <Requests.Firewall>();
                firewall.OutboundRules = outboundRules;
                session.Data           = firewall;
                session.State          = SessionState.WaitChooseDropletsForFirewall;
            });

            var sendMessage = await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, pageModel.Message, ParseMode.Markdown, replyMarkup : pageModel.Keyboard);

            _handlerCallbackRepo.Update(message.From.Id, calllback =>
            {
                calllback.MessageId   = sendMessage.MessageId;
                calllback.UserId      = message.From.Id;
                calllback.HandlerType = GetType().FullName;
            });
        }