private void CheckConnectionHealth(object dummyData) { bool shouldReconnect = false; try { string botUserId = _noobotCore.GetUserIdForUsername(_noobotCore.GetBotUserName()); var message = new ResponseMessage { ResponseType = ResponseType.DirectMessage, UserId = botUserId, Text = "Test Message" }; //TODO: Find a better way of testing if the connection is alive... _noobotCore.SendMessage(message).Wait(TimeSpan.FromSeconds(30)); } catch (AggregateException ex) when (ex.InnerExceptions.Any() && ex.InnerExceptions[0].Message == EXPECTED_ERROR_MESSAGE) { _log.Info("Health check passed as expected."); } catch (Exception ex) { _log.Error(ex.ToString()); shouldReconnect = true; } if (shouldReconnect) { _log.Warn("Looks like we 'might' be disconnected (error detected)"); EnsureClientIsDisconnected(); Reconnect(); } }
private async Task<SlackChatHub> GetChatHub(ResponseMessage responseMessage) { SlackChatHub chatHub = null; if (responseMessage.ResponseType == ResponseType.Channel) { chatHub = new SlackChatHub { Id = responseMessage.Channel }; } else if (responseMessage.ResponseType == ResponseType.DirectMessage) { if (string.IsNullOrEmpty(responseMessage.Channel)) { chatHub = await GetUserChatHub(responseMessage.UserId); } else { chatHub = new SlackChatHub { Id = responseMessage.Channel }; } } return chatHub; }
public async Task SendMessage(ResponseMessage responseMessage) { SlackChatHub chatHub = await GetChatHub(responseMessage); if (chatHub != null) { if (responseMessage is TypingIndicatorMessage) { _log.Info($"Indicating typing on channel '{chatHub.Name}'"); await _connection.IndicateTyping(chatHub); } else { var botMessage = new BotMessage { ChatHub = chatHub, Text = responseMessage.Text, Attachments = GetAttachments(responseMessage.Attachment) }; string textTrimmed = botMessage.Text.Length > 50 ? botMessage.Text.Substring(0, 50) + "..." : botMessage.Text; _log.Info($"Sending message '{textTrimmed}'"); await _connection.Say(botMessage); } } else { _log.Error($"Unable to find channel for message '{responseMessage.Text}'. Message not sent"); } }