protected async virtual void OnMessageReceived(MessageSlackMessage messageSlackMessage) { if (messageSlackMessage != null) { WriteLineToConsole($"Message received"); string text = messageSlackMessage.Text; if ((String.IsNullOrEmpty(text) == false) && ((text.StartsWith($"@{_name}") == true) || (text.StartsWith($"<@{_id}>") == true))) { string channelID = messageSlackMessage.ChannelID; User user = _users.Where(m => (m.ID == messageSlackMessage.UserID)).FirstOrDefault(); SlackMessageContext slackMessageContext = new SlackMessageContext(channelID, user, text); WriteLineToConsole($"Message for {_name} from {user.Name}"); foreach (MessageHandler messageHandler in _messageHandlers.Where(m => (m.IsHandle(slackMessageContext) == true))) { SlackMessageResponse oSlackMessageResponse = messageHandler.GetResponse(slackMessageContext); await SendTypingResponse(slackMessageContext); await Send(slackMessageContext, oSlackMessageResponse); } } } }
private async Task Receive() { while (_clientWebSocket.State == WebSocketState.Open) { byte[] buffer = new byte[1024]; WebSocketReceiveResult webSocketReceiveResult = await _clientWebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None); if (webSocketReceiveResult.MessageType == WebSocketMessageType.Close) { await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); } else { string message = Encoding.UTF8.GetString(buffer).TrimEnd('\0'); string messageType = ""; if (message.Contains("\"type\":\"") == true) { messageType = message.Substring(message.IndexOf("\"type\":\"") + "\"type\":\"".Length); if (messageType.Contains("\"") == true) { messageType = messageType.Substring(0, messageType.IndexOf("\"")); } } if (String.IsNullOrWhiteSpace(messageType) == false) { SlackMessage slackMessage = null; switch (messageType.ToLower()) { case "desktop_notification": slackMessage = Serialiser.FromString <DesktopNotificationSlackMessage>(message); break; case "hello": slackMessage = Serialiser.FromString <HelloSlackMessage>(message); break; case "message": MessageSlackMessage oMessageSlackMessage = Serialiser.FromString <MessageSlackMessage>(message); OnMessageReceived(oMessageSlackMessage); slackMessage = oMessageSlackMessage; break; case "presence_change": slackMessage = Serialiser.FromString <PresenceChangeSlackMessage>(message); break; case "reconnect_url": slackMessage = Serialiser.FromString <ReconnectUrlSlackMessage>(message); break; case "user_typing": slackMessage = Serialiser.FromString <UserTypingSlackMessage>(message); break; default: WriteLineToConsole($"Unhandled message type: {messageType}"); break; } } } } }