Beispiel #1
0
        private static async Task InitMarkovAsync(BotArguments args)
        {
            var chat = await File.ReadAllLinesAsync(args.SourceText);

            _model = new StringMarkov(5);
            _model.Learn(chat);
        }
Beispiel #2
0
 private static async Task InitBrowserAsync(BotArguments args)
 {
     await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
     _browser = await Puppeteer.LaunchAsync(new LaunchOptions
     {
         UserDataDir = Path.Combine(".", "user-data-dir"),
         Headless    = false
     });
 }
Beispiel #3
0
        private static async Task LaunchProcessAsync(BotArguments args)
        {
            await InitBrowserAsync(args);
            await InitWhatsAppAsync(args);
            await InitMarkovAsync(args);

            Console.ReadLine();
            await _browser.CloseAsync();
        }
Beispiel #4
0
        private static async Task InitWhatsAppAsync(BotArguments args)
        {
            _whatsAppPage = await _browser.NewPageAsync();

            await _whatsAppPage.GoToAsync(WhatsAppMetadata.WhatsAppURL);

            await _whatsAppPage.WaitForSelectorAsync(WhatsAppMetadata.MainPanel);

            var input = await _whatsAppPage.QuerySelectorAsync(WhatsAppMetadata.SearchInput);

            await input.TypeAsync(args.ChatName);

            await _whatsAppPage.WaitForTimeoutAsync(500);

            var menuItem = (await _whatsAppPage.QuerySelectorAllAsync(WhatsAppMetadata.PersonItem)).ElementAt(1);
            await menuItem.ClickAsync();

            await _whatsAppPage.ExposeFunctionAsync("newChat", async (string text) =>
            {
                Console.WriteLine(text);

                if (text.ToLower().Contains(args.TriggerWord) && !text.Contains(args.ResponseTemplate))
                {
                    await RespondAsync(args, text);
                }

                text = text.Replace(args.ResponseTemplate, string.Empty);
                await File.AppendAllTextAsync(args.SourceText, text + "\n");
            });

            await _whatsAppPage.EvaluateFunctionAsync($@"() => {{
                var observer = new MutationObserver((mutations) => {{
                    for(var mutation of mutations) {{
                        if(mutation.addedNodes.length && mutation.addedNodes[0].classList.value === '{WhatsAppMetadata.MessageLine}') {{
                            newChat(mutation.addedNodes[0].querySelector('.copyable-text span').innerText);
                        }}
                    }}
                }});
                observer.observe(document.querySelector('{WhatsAppMetadata.ChatContainer}'), {{ attributes: false, childList: true, subtree: true }});
            }}");
        }
Beispiel #5
0
        private static async Task RespondAsync(BotArguments args, string text)
        {
            string response = null;
            var    words    = text.RemoveStopWords(args.Language).RemovePunctuation().Replace(args.TriggerWord, string.Empty).Split(' ');

            for (var index = words.Length - 1; index >= 0; index--)
            {
                response = _model.Walk(1, words[index]).First();

                if (response == words[index])
                {
                    response = null;
                }
            }

            if (response == null)
            {
                response = _model.Walk(1).First();
            }

            Console.WriteLine(response);
            await WriteChatAsync(args.ResponseTemplate + " " + response);
        }