Exemplo n.º 1
0
        private async Task ListenTo(InboundMessage inboundMessage)
        {
            if (inboundMessage?.MessageType != MessageType.Message)
            {
                return;
            }
            if (string.IsNullOrEmpty(inboundMessage.User))
            {
                return;
            }
            if (!string.IsNullOrEmpty(Self.Id) && inboundMessage.User == Self.Id)
            {
                return;
            }

            if (inboundMessage.Channel != null && !_connectedHubs.ContainsKey(inboundMessage.Channel))
            {
                _connectedHubs[inboundMessage.Channel] = _chatHubInterpreter.FromId(inboundMessage.Channel);
            }

            var message = new SlackMessage
            {
                User = new SlackUser
                {
                    Id   = inboundMessage.User,
                    Name = GetMessageUsername(inboundMessage),
                },
                Text        = inboundMessage.Text,
                ChatHub     = inboundMessage.Channel == null ? null : _connectedHubs[inboundMessage.Channel],
                RawData     = inboundMessage.RawData,
                MentionsBot = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, inboundMessage.Text)
            };

            await RaiseMessageReceived(message);
        }
Exemplo n.º 2
0
        private Task HandleMessage(ChatMessage inboundMessage)
        {
            if (string.IsNullOrEmpty(inboundMessage.User))
            {
                return(Task.CompletedTask);
            }

            if (!string.IsNullOrEmpty(Self.Id) && inboundMessage.User == Self.Id)
            {
                return(Task.CompletedTask);
            }

            //TODO: Insert into connectedHubs when DM is missing

            var message = new SlackMessage
            {
                User           = GetMessageUser(inboundMessage.User),
                Timestamp      = inboundMessage.Timestamp,
                Text           = inboundMessage.Text,
                ChatHub        = GetChatHub(inboundMessage.Channel),
                RawData        = inboundMessage.RawData,
                MentionsBot    = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, inboundMessage.Text),
                MessageSubType = inboundMessage.MessageSubType.ToSlackMessageSubType(),
                Files          = inboundMessage.Files.ToSlackFiles()
            };

            return(RaiseMessageReceived(message));
        }
Exemplo n.º 3
0
        private Task HandleMessage(ChatMessage inboundMessage)
        {
            if (string.IsNullOrEmpty(inboundMessage.User))
            {
                return(Task.FromResult(false));
            }

            if (!string.IsNullOrEmpty(Self.Id) && inboundMessage.User == Self.Id)
            {
                return(Task.FromResult(false));
            }

            //TODO: Insert into connectedHubs when DM is missing

            var message = new SlackMessage
            {
                User        = GetMessageUser(inboundMessage.User),
                TimeStamp   = inboundMessage.TimeStamp,
                Text        = inboundMessage.Text,
                ChatHub     = inboundMessage.Channel == null ? null : _connectedHubs[inboundMessage.Channel],
                RawData     = inboundMessage.RawData,
                MentionsBot = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, inboundMessage.Text)
            };

            return(RaiseMessageReceived(message));
        }
Exemplo n.º 4
0
        public async Task HandleInboundData(UserInboundMessage userInboundMessage)
        {
            if (string.IsNullOrEmpty(userInboundMessage.User))
            {
                return;
            }

            await UpdateHubCache(userInboundMessage);

            await UpdateUserCache(userInboundMessage);

            if (!string.IsNullOrEmpty(Self.Id) && userInboundMessage.User == Self.Id)
            {
                return;
            }

            SlackMessage message = new SlackMessage
            {
                User        = userInboundMessage.User == null ? null : _userCache[userInboundMessage.User],
                Text        = userInboundMessage.Text,
                ChatHub     = userInboundMessage.Channel == null ? null : _hubCache[userInboundMessage.Channel],
                Time        = userInboundMessage.Time,
                RawData     = userInboundMessage.RawData,
                MentionsBot = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, userInboundMessage.Text)
            };

            if (message.User == null && userInboundMessage.User != null)
            {
                message.User = new SlackUser {
                    Id = userInboundMessage.User, Name = ""
                }
            }
            ;
            if (message.ChatHub == null && userInboundMessage.Channel != null)
            {
                message.ChatHub = new SlackChatHub {
                    Id = userInboundMessage.Channel, Name = ""
                }
            }
            ;

            if (userInboundMessage.MessageSubType == MessageSubType.bot_message)
            {
                message.User.Name = ((BotInboundMessage)userInboundMessage).UserName;
            }

            await RaiseMessageReceived(message);
        }