Exemplo n.º 1
0
 protected Robot(LoggerConfigurator logConfig)
 {
     LogConfig = logConfig;
     Logger    = logConfig == null
         ? new TraceLogger(false, "trace", LogLevel.Error, true, false, false, "F")
         : logConfig.GetLogger();
     AutoLoadScripts = true;
 }
Exemplo n.º 2
0
 public Robot(string name, IDictionary <string, string> config, LoggerConfigurator logConfig, IDictionary <string, IAdapter> adapters, IRouter router, IBrain brain, IScriptStore scriptStore, IScriptRunner scriptRunner)
     : this(logConfig)
 {
     _name         = name;
     _config       = config;
     _scriptStore  = scriptStore;
     _adapters     = adapters;
     _brain        = brain;
     _router       = router;
     _scriptRunner = scriptRunner;
     _isConfigured = true;
     Initialize(adapters.Values.ToArray().Concat(new object[] { router, brain, scriptRunner }).ToArray());
 }
 public NuGetPackageAssemblyResolver(LoggerConfigurator logConfig)
 {
     _log = logConfig.GetLogger();
     AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
     RefreshAssemblies(_log);
 }
Exemplo n.º 4
0
        public static LoggerConfigurator CreateLogConfig(Options options)
        {
            var logConfig = new LoggerConfigurator(options.Verbose ? LogLevel.Debug : LogLevel.Info);
            if (Environment.UserInteractive)
            {
                logConfig.ConfigureForConsole();
            }
            else
            {
                logConfig.AddTraceListener();
            }

            var logger = logConfig.GetLogger();

            if (!string.IsNullOrWhiteSpace(options.LogFile))
            {
                if (Directory.Exists(Path.GetDirectoryName(options.LogFile)))
                    logConfig.ConfigureForFile(options.LogFile);
                else
                    logger.Warn(string.Format("Failed to load log file.  Path for {0} does not exist.", options.LogFile));
            }
            return logConfig;
        }
Exemplo n.º 5
0
 public static Common.Logging.ILog GetConsoleLogger(LogLevel logLevel)
 {
     var configurator = new LoggerConfigurator(logLevel);
     configurator.ConfigureForConsole();
     return configurator.GetLogger();
 }
Exemplo n.º 6
0
        public async Task XmppRobot()
        {
            //enter config values to enable this test
            var config = new Dictionary<string, string>();         
            //config.Add("MMBOT_XMPP_HOST", "userver");
            //config.Add("MMBOT_XMPP_CONNECT_HOST", "userver");
            //config.Add("MMBOT_XMPP_USERNAME", "mmbot");
            //config.Add("MMBOT_XMPP_PASSWORD", "password");
            //config.Add("MMBOT_XMPP_CONFERENCE_SERVER", "conference.userver");
            //config.Add("MMBOT_XMPP_ROOMS", "testroom");
            //config.Add("MMBOT_XMPP_LOGROOMS", "logroom");

            if (config.Count() == 0)
                return;

            var logConfig = new LoggerConfigurator(LogLevel.Trace);
            logConfig.AddTraceListener();

            var robot = new RobotBuilder(logConfig)
                        .WithConfiguration(config)
                        .UseAdapter<XmppAdapter>()
                        .Build(); 
            
            robot.AutoLoadScripts = false;
            robot.LoadScript<CompiledScripts.Ping>();

            bool robotReady = false;
            robot.On<bool>("RobotReady", result =>
            {
                robotReady = result;
            });

            await robot.Run();

            Assert.True(robotReady);

            int cmdReceived = 0;
            robot.Hear("mmbot", msg => { cmdReceived++; });

            //will wait for two commands
            while (cmdReceived < 2)
                Thread.Sleep(1000);            
        }
Exemplo n.º 7
0
        public async Task WhenMultipleAdaptersAreConfigured_ResponsesAreOnlySentToTheOriginatingAdapter()
        {
            var logConfig = new LoggerConfigurator(LogLevel.Trace);
            logConfig.ConfigureForConsole();
            using(var robot = new RobotBuilder(logConfig)
                        .DisablePluginDiscovery()
                        .DisableScriptDiscovery()
                        .UseAdapter<StubAdapter>()
                        .UseAdapter<StubAdapter2>()
                        .UseBrain<StubBrain>()
                        .Build()){
            
                robot.AutoLoadScripts = false;

                var adapter1 = robot.Adapters.Values.OfType<StubAdapter>().First();
                var adapter2 = robot.Adapters.Values.OfType<StubAdapter2>().First();

                robot.LoadScript<StubEchoScript>();

                var expectedMessages = new[]
                {
                    Tuple.Create("test1", "Hello Test 1"),
                    Tuple.Create("test2", "Hello Test 2"),
                    Tuple.Create("test3", "Hello Test 3")
                };
                await robot.Run();

                Console.WriteLine("Testing Adapter 1");
                expectedMessages.ForEach(t => adapter1.SimulateMessage(t.Item1, "mmbot " + t.Item2));

                var expectedMessagesValues = expectedMessages.Select(t => string.Concat(t.Item1, t.Item2));
                Console.WriteLine("Expected:");
                Console.WriteLine(string.Join(Environment.NewLine, expectedMessagesValues));
                var actualMessagesValues = adapter1.Messages.Select(t => string.Concat(t.Item1.User.Name, t.Item2.FirstOrDefault()));
                Console.WriteLine("Actual:");
                Console.WriteLine(string.Join(Environment.NewLine, actualMessagesValues));

                Assert.True(expectedMessagesValues.SequenceEqual(actualMessagesValues));
                Assert.Equal(0, adapter2.Messages.Count());

                Console.WriteLine("Testing Adapter 2");
                expectedMessages.ForEach(t => adapter2.SimulateMessage(t.Item1, "mmbot " + t.Item2));

            
                Console.WriteLine("Expected:");
                Console.WriteLine(string.Join(Environment.NewLine, expectedMessagesValues));
                actualMessagesValues = adapter2.Messages.Select(t => string.Concat(t.Item1.User.Name, t.Item2.FirstOrDefault()));
                Console.WriteLine("Actual:");
                Console.WriteLine(string.Join(Environment.NewLine, actualMessagesValues));

                Assert.True(expectedMessagesValues.SequenceEqual(actualMessagesValues));
                Assert.Equal(3, adapter1.Messages.Count());
            };
        }
Exemplo n.º 8
0
        public async Task WhenRobotIsReset_ScriptCleanupIsInvoked()
        {
            var loggerConfigurator = new LoggerConfigurator(LogLevel.All);
            var builder = new RobotBuilder(loggerConfigurator)
                .UseAdapter<StubAdapter>()
                .UseBrain<StubBrain>()
                .DisablePluginDiscovery()
                .DisableScriptDiscovery();

            var scriptRunner = new ScriptRunner(loggerConfigurator.GetLogger());
            
            var robot = builder
                        .Build(c => c.Register<IScriptRunner>(scriptRunner));

            scriptRunner.Initialize(robot);

            robot.LoadScript<StubEchoScript>();

            bool isCleanedUp = false;
            using(scriptRunner.StartScriptProcessingSession(new ScriptSource("TestScript", string.Empty)))
            {
                robot.RegisterCleanup(() => isCleanedUp = true);
            }

            await robot.Reset();

            Assert.True(isCleanedUp);
        }
Exemplo n.º 9
0
 public RobotBuilder(LoggerConfigurator logConfig) : this()
 {
     _logConfig = logConfig;
 }