Exemplo n.º 1
0
Arquivo: Worker.cs Projeto: nma76/jBot
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("jBot daemon started at at: {time}", DateTimeOffset.Now);
            _logger.LogInformation("Looking for hashtag {basehashtag}", _serviceInstance.BotConfiguration.BaseHashTag);

            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                //Iterate all bots capabilities and execute them
                foreach (var capability in Capabilities.GetAll())
                {
                    _logger.LogInformation("Running capability {capability} - {description}", capability.ActionMethod, capability.Description);

                    ActionHandler actionHandler = new ActionHandler(_serviceInstance);
                    string        result        = actionHandler.RunAction(capability);

                    _logger.LogInformation(result);
                }

                _logger.LogInformation("Total tweet read: {readtweets}", Diagnostics.GetTotalRead());
                _logger.LogInformation("Total tweet sent: {senttweets}", Diagnostics.GetTotalSent());

                //Wait x seconds and then run again
                await Task.Delay(_serviceInstance.BotConfiguration.Interval * 1000, stoppingToken);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //Configure configuration file
            IConfiguration Configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                           .Build();

            //Read configurarion sections
            var authSection          = Configuration.GetSection("Authentication");
            var dataFileSection      = Configuration.GetSection("DataStore");
            var configurationSection = Configuration.GetSection("Configuration");

            //Create a file to keep track of last used twitter id
            StorageSettings storageSettings = new StorageSettings()
            {
                Datafolder    = dataFileSection["DataFolder"],
                FilePrefix    = dataFileSection["DataFilePrefix"],
                OverlayFolder = dataFileSection["OverlayFolder"]
            };
            DataStorage dataStorage = new DataStorage(storageSettings);

            //Create authentication object
            AuthToken authToken = new AuthToken()
            {
                AccessToken       = authSection["AccessToken"],
                AccessTokenSecret = authSection["AccessTokenSecret"],
                ConsumerKey       = authSection["ConsumerKey"],
                ConsumerKeySecret = authSection["ConsumerKeySecret"]
            };

            //Create configuration object
            BotConfiguration configuration = new BotConfiguration
            {
                BaseHashTag = configurationSection["BaseHashTag"],
                AuthToken   = authToken,
                DataStorage = dataStorage
            };

            //Create a twitter service instance
            ServiceInstance serviceInstance = new ServiceInstance(configuration);

            //Create action handler and make sure it gets called
            foreach (var capability in Capabilities.GetAll())
            {
                ActionHandler actionHandler = new ActionHandler(serviceInstance);
                string        result        = actionHandler.RunAction(capability);

                Console.WriteLine(result);
            }
        }