示例#1
0
        public BotBase SelectBot()
        {
            var builtBots = _bots.ToList();

            if (!int.TryParse(_settingsService["BotCoreIndex"]?.ToString(), out int code))
            {
                _botLogger.LogWarning("Select your bot:\n");
                for (int i = 0; i < builtBots.Count; i++)
                {
                    _botLogger.LogInfo($"\t{i} {builtBots[i].GetType().Name}");
                }
                while (true)
                {
                    _botLogger.LogInfo("Select bot:");
                    var codeString = Console.ReadLine()?.Trim();
                    if (!int.TryParse(codeString, out code) || code >= builtBots.Count)
                    {
                        _botLogger.LogDanger("Invalid item!");
                        continue;
                    }
                    break;
                }
            }
            _settingsService["BotCoreIndex"] = code;
            return(builtBots[code]);
        }
示例#2
0
        public async Task <Task> Start()
        {
            if (_bot == null)
            {
                _botLogger.LogDanger("You can't start bot listener without a bot!");
                return(Task.CompletedTask);
            }
            var server = AskServerAddress();

            _kahlaLocation.UseKahlaServer(server);
            if (!await TestKahlaLive())
            {
                return(Task.CompletedTask);
            }
            await OpenSignIn();

            var code = await AskCode();

            await SignIn(code);
            await DisplayMyProfile();

            var websocketAddress = await GetWSAddress();

            _botLogger.LogInfo($"Your account channel: {websocketAddress}");
            return(MonitorEvents(websocketAddress));
        }
示例#3
0
        public static BotBase SelectBot(
            IEnumerable <BotBase> bots,
            SettingsService settingsService,
            BotLogger botLogger)
        {
            var builtBots = bots.ToList();
            int code      = settingsService.Read().BotCoreIndex;

            if (code < 0)
            {
                botLogger.LogWarning("Select your bot:\n");
                for (int i = 0; i < builtBots.Count; i++)
                {
                    botLogger.LogInfo($"\t{i.ToString()} {builtBots[i].GetType().Name}");
                }
                while (true)
                {
                    botLogger.LogInfo($"Select bot:");
                    var codeString = Console.ReadLine().Trim();
                    if (!int.TryParse(codeString, out code) || code >= builtBots.Count)
                    {
                        botLogger.LogDanger($"Invalid item!");
                        continue;
                    }
                    break;
                }
                settingsService.Save(code);
            }
            return(builtBots[code]);
        }
示例#4
0
        public async Task <bool> Execute(string command)
        {
            var conversations = await _conversationService.AllAsync();

            _botLogger.LogInfo("");
            foreach (var conversation in conversations.Items)
            {
                _botLogger.LogInfo($"ID: {conversation.ConversationId}\tName:\t{conversation.DisplayName}");
            }
            _botLogger.LogInfo("");
            var convId = _botLogger.ReadLine("Enter conversation ID you want to say:");
            var target = conversations.Items.FirstOrDefault(t => t.ConversationId.ToString() == convId);

            if (target == null)
            {
                _botLogger.LogDanger($"Can't find conversation with ID: {convId}");
                return(true);
            }
            var toSay = _botLogger.ReadLine($"Enter the message you want to send to '{target.DisplayName}':");

            if (string.IsNullOrWhiteSpace(toSay))
            {
                _botLogger.LogDanger("Can't send empty content.");
                return(true);
            }
            var encrypted = _aes.OpenSSLEncrypt(toSay, _eventSyncer.Contacts.FirstOrDefault(t => t.ConversationId == target.ConversationId)?.AesKey);
            await _conversationService.SendMessageAsync(encrypted, target.ConversationId);

            _botLogger.LogSuccess("Sent.");
            return(true);
        }
示例#5
0
        public async Task <bool> Execute(string command)
        {
            await Task.Delay(0);

            foreach (var request in _eventSyncer.Requests)
            {
                _botLogger.LogInfo($"Name:\t{request.Creator.NickName}");
                _botLogger.LogInfo($"Time:\t{request.CreateTime}");
                if (request.Completed)
                {
                    _botLogger.LogSuccess("\tCompleted.");
                }
                else
                {
                    _botLogger.LogWarning("\tPending.");
                }
            }
            return(true);
        }
示例#6
0
        protected virtual async Task OnNewMessageEvent(NewMessageEvent typedEvent)
        {
            string decrypted = _aes.OpenSSLDecrypt(typedEvent.Message.Content, typedEvent.AESKey);

            _botLogger.LogInfo($"On message from sender `{typedEvent.Message.Sender.NickName}`: {decrypted}");
            if (decrypted.StartsWith("[group]") && int.TryParse(decrypted.Substring(7), out int groupId))
            {
                await BuildBot.OnGroupInvitation(groupId, typedEvent);
            }
            await BuildBot.OnMessage(decrypted, typedEvent).ConfigureAwait(false);
        }
示例#7
0
        public async Task <bool> Execute(string command)
        {
            await Task.Delay(0);

            if (int.TryParse(command.Substring(4).Trim(), out int convId))
            {
                var conversation = _eventSyncer.Contacts.FirstOrDefault(t => t.ConversationId == convId);
                if (conversation == null)
                {
                    _botLogger.LogDanger($"Conversation with Id '{convId}' was not found!");
                    return(true);
                }
                foreach (var message in conversation.Messages)
                {
                    if (!message.GroupWithPrevious)
                    {
                        _botLogger.LogInfo($"{message.Sender.NickName} says: \t {_aes.OpenSSLDecrypt(message.Content, conversation.AesKey)}");
                    }
                    else
                    {
                        _botLogger.LogInfo($"\t\t\t {_aes.OpenSSLDecrypt(message.Content, conversation.AesKey)}");
                    }
                }
                return(true);
            }
            foreach (var conversation in _eventSyncer.Contacts)
            {
                var online = conversation.Online == true ? "online" :
                             conversation.Online == false ? "offline" : string.Empty;
                _botLogger.LogInfo($"Name:\t{conversation.DisplayName}");
                _botLogger.LogInfo($"ID:\t{conversation.ConversationId}\t{online}\t\t{conversation.Discriminator}");
                if (!string.IsNullOrWhiteSpace(conversation.LatestMessage?.Content))
                {
                    _botLogger.LogInfo($"Last:\t{_aes.OpenSSLDecrypt(conversation.LatestMessage.Content, conversation.AesKey)}");
                    _botLogger.LogInfo($"Time:\t{conversation.LatestMessage.SendTime}");
                }
                if (conversation.UnReadAmount > 0)
                {
                    _botLogger.LogDanger($"Unread:\t**{conversation.UnReadAmount}**");
                }
                if (conversation.SomeoneAtMe)
                {
                    _botLogger.LogWarning("At!");
                }
                if (conversation.Messages.Count > 0)
                {
                    _botLogger.LogVerbose($"Local Messages:\t**{conversation.Messages.Count}**");
                }
                _botLogger.LogInfo("\n");
            }
            return(true);
        }
示例#8
0
文件: BotHost.cs 项目: ploxolq/Kahla
        public string AskServerAddress()
        {
            var cached = _settingsService["ServerAddress"] as string;

            if (!string.IsNullOrWhiteSpace(cached))
            {
                return(cached);
            }
            _botLogger.LogInfo("Welcome! Please enter the server address of Kahla.");
            var result = _botLogger.ReadLine("\r\nEnter 1 for production\r\nEnter 2 for staging\r\nFor other server, enter like: https://server.kahla.app\r\n");

            if (result.Trim() == 1.ToString())
            {
                return("https://server.kahla.app");
            }
            else if (result.Trim() == 2.ToString())
            {
                return("https://staging.server.kahla.app");
            }
            else
            {
                return(result);
            }
        }
示例#9
0
 public virtual Task OnBotStarting()
 {
     BotLogger.LogInfo("Bot Starting...");
     return(Task.CompletedTask);
 }
示例#10
0
        public Task <bool> Execute(string command)
        {
            _botLogger.LogInfo("Kahla bot commands:");

            _botLogger.LogInfo("\r\nConversation");
            _botLogger.LogInfo("\tconv\t\tShow all conversations.");
            _botLogger.LogInfo("\tconv [ID]\tShow messages in one conversation.");
            _botLogger.LogInfo("\treq\t\tShow all requests.");
            _botLogger.LogInfo("\tsay\t\tSay something to someone.");
            _botLogger.LogInfo("\tb\t\tBroadcast to all conversations.");
            _botLogger.LogInfo("\tclear\t\tClear console.");

            _botLogger.LogInfo("\r\nGroup");
            _botLogger.LogInfo("\tm\t\tMute all groups.");
            _botLogger.LogInfo("\tu\t\tUnmute all groups.");

            _botLogger.LogInfo("\r\nNetwork");
            _botLogger.LogInfo("\treboot\t\tReconnect to Stargate.");
            _botLogger.LogInfo("\tlogout\t\tLogout.");

            _botLogger.LogInfo("\r\nProgram");
            _botLogger.LogInfo("\thelp\t\tShow help.");
            _botLogger.LogInfo("\tversion\t\tCheck and show version info.");
            _botLogger.LogInfo("\texit\t\tQuit bot.");
            _botLogger.LogInfo("");
            return(Task.FromResult(true));
        }
示例#11
0
文件: BotBase.cs 项目: LXKing/Kahla
        public async Task <Task> Connect()
        {
            var server = AskServerAddress();

            SettingsService.Save(server);
            KahlaLocation.UseKahlaServer(server);
            if (!await TestKahlaLive())
            {
                return(Task.CompletedTask);
            }
            if (!await SignedIn())
            {
                await OpenSignIn();

                var code = await AskCode();
                await SignIn(code);
            }
            else
            {
                BotLogger.LogSuccess($"You are already signed in! Welcome!");
            }
            await RefreshUserProfile();
            await OnBotInit();

            var websocketAddress = await GetWSAddress();

            BotLogger.LogInfo($"Listening to your account channel: {websocketAddress}");
            var requests = (await FriendshipService.MyRequestsAsync())
                           .Items
                           .Where(t => !t.Completed);

            foreach (var request in requests)
            {
                await OnFriendRequest(new NewFriendRequestEvent
                {
                    RequestId   = request.Id,
                    Requester   = request.Creator,
                    RequesterId = request.CreatorId,
                });
            }
            return(MonitorEvents(websocketAddress));
        }
示例#12
0
        public async Task Command()
        {
            await Task.Delay(0);

            while (true)
            {
                var command = Console.ReadLine();
                if (command.Length < 1)
                {
                    continue;
                }
                switch (command.ToLower().Trim()[0])
                {
                case 'q':
                    Environment.Exit(0);
                    return;

                case 'h':
                    _botLogger.LogInfo($"Kahla bot commands:");

                    _botLogger.LogInfo($"\r\nConversation");
                    _botLogger.LogInfo($"\ta\tShow all conversations.");
                    _botLogger.LogInfo($"\ts\tSay something to someone.");
                    _botLogger.LogInfo($"\tb\tBroadcast to all conversations.");

                    _botLogger.LogInfo($"\r\nGroup");
                    _botLogger.LogInfo($"\tm\tMute all groups.");
                    _botLogger.LogInfo($"\tu\tUnmute all groups.");

                    _botLogger.LogInfo($"\r\nNetwork");
                    _botLogger.LogInfo($"\tr\tReconnect to Stargate.");
                    _botLogger.LogInfo($"\tl\tLogout.");

                    _botLogger.LogInfo($"\r\nProgram");
                    _botLogger.LogInfo($"\th\tShow help.");
                    _botLogger.LogInfo($"\tq\tQuit bot.");
                    break;

                default:
                    _botLogger.LogDanger($"Unknown command: {command}. Please try command: 'h' for help.");
                    break;
                }
            }
        }