Esempio n. 1
0
        private SlackBot(
            SlackBotState state,
            IDriver driver,
            IMessageBus bus,
            ISlackBotConfig config,
            ILogger <SlackBot> logger)
        {
            this.state  = state;
            this.config = config;
            this.logger = logger;

            this.driver     = driver;
            this.messageBus = bus;

            this.whenHandlers = new ConcurrentQueue <WhenHandler>();

            this.sendMessageQueue = new MessageThrottleQueue(
                TimeSpan.FromSeconds(1.0), // ~1/sec (see: https://api.slack.com/methods/chat.postMessage)
                driver,
                logger,
                (queue, msg, lg, ex) =>
            {
                config?.OnSendMessageFailure?.Invoke(queue, msg, lg, ex);
            });

            this.uploadFileQueue = new ThrottleQueue <File>(
                TimeSpan.FromSeconds(3), // ~20/min (see: https://api.slack.com/methods/files.upload)
                logger,
                this.driver.UploadFileAsync,
                null);
        }
Esempio n. 2
0
        public SendMessageTests()
        {
            this.state  = SlackBotState.Initialize("1", "testbot");
            this.config = new TestConfig();

            this.bus = new RxMessageBus();
        }
        public BotInitializeTests()
        {
            this.state  = SlackBotState.Initialize("1", "testbot");
            this.config = new TestConfig();

            this.driver = new TestDriver(this.state);
            this.bus    = new RxMessageBus();
        }
Esempio n. 4
0
        public async Task InitializeAsync()
        {
            this.state  = SlackBotState.Initialize("1", "testbot");
            this.config = new TestConfig();

            this.driver = new TestDriver(this.state);
            this.bus    = new RxMessageBus();

            this.bot = await SlackBot.InitializeAsync(this.driver, this.bus);
        }
Esempio n. 5
0
        private SlackBot(
            SlackBotState state,
            IDriver driver,
            IMessageBus bus,
            ISlackBotConfig config,
            ILogger <SlackBot> logger)
        {
            this.state  = state;
            this.config = config;
            this.logger = logger;

            this.driver     = driver;
            this.messageBus = bus;

            this.whenHandlers = new ConcurrentQueue <WhenHandler>();

            this.sendMessageQueue = new SendMessageQueue(TimeSpan.FromSeconds(1), driver, logger, config.OnSendMessageFailure);
        }
Esempio n. 6
0
        private async Task <SlackBotState> ConnectRtmAsync(IMessageBus bus, ILogger logger)
        {
            logger.LogDebug("Retrieving websocket URL");
            var json = await HttpClient.GetStringAsync($"https://slack.com/api/rtm.start?token={this.slackToken}");

            var jData = JObject.Parse(json);

            string websocketUrl = jData["url"].Value <string>();

            var state = SlackBotState.InitializeFromRtmStart(jData);

            this.websocket = new ClientWebSocket();
            this.websocket.Options.KeepAliveInterval = TimeSpan.FromSeconds(30);

            logger.LogDebug($"Opening connection to {websocketUrl}");

            await this.websocket.ConnectAsync(new Uri(websocketUrl), this.tokenSource.Token);

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Task.Factory.StartNew(async() => await this.Listen(bus, logger), this.tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            return(state);
        }
Esempio n. 7
0
 public TestDriver(SlackBotState state)
 {
     this.state = state;
 }