public async Task StartAsync(CancellationToken cancellationToken) { _slackClient.Connect(); }
private void Run() { logger.Log("Starting SlackBot message loop"); try { running = true; ISlackClient client = slackClientFactory.CreateSlackClient(slackToken); client.OnMessageReceived += (message) => { try { if (message.channel == null) { return; } if (IsMessageFromBot(client, message)) { return; } if (client.IsDirectMessageChannel(message.channel)) { logger.Log("DM from " + client.GetUsername(message.user) + " at " + message.ts.ToShortTimeString() + ": " + message.text); string response = responseHandler.GenerateResponse(message.text, true); if (response != null) { client.PostMessage(message.channel, response, () => { logger.Log("Sent DM back to " + client.GetUsername(message.user) + ": " + response); }); } } else { logger.Log("Message in channel " + client.GetChannelName(message.channel) + " at " + message.ts.ToShortTimeString() + " from " + client.GetUsername(message.user) + ": " + message.text); string response = responseHandler.GenerateResponse(message.text, false); if (response != null) { client.PostMessage(message.channel, response, () => { logger.Log("Sent message to channel " + client.GetChannelName(message.channel) + ": " + response); }); } } } catch (Exception e) { logger.Log("Got exception when processing message:" + e); } }; client.Connect(() => { logger.Log("successfully connected"); }); client.RefreshUserList(() => { logger.Log("refreshed users"); }); client.RefreshChannelList(() => { logger.Log("refreshed channels"); }); while (running) { Thread.Sleep(1000); } } catch (Exception e) { logger.Log("Got exception when running SlackBot thread: " + e); running = false; } logger.Log("Finished SlackBot message loop"); }