Example #1
0
        public void Initialize(SlackHandshake handshake)
        {
            // Clear the dictionary.
            _dictionary.Clear();

            // Add channels
            foreach (var channel in handshake.Channels)
            {
                var room = new Room {
                    Id = channel.Id, Name = channel.Name
                };
                _dictionary.AddOrUpdate(channel.Id, room, (k, v) =>
                                        new Room {
                    Id = k, Name = v.Name
                });
            }

            // Add groups
            foreach (var group in handshake.Groups)
            {
                var room = new Room {
                    Id = group.Id, Name = group.Name
                };
                _dictionary.AddOrUpdate(group.Id, room, (k, v) =>
                                        new Room {
                    Id = k, Name = v.Name
                });
            }
        }
Example #2
0
        private void ProcessEvent(Stream stream, SlackHandshake handshake, User bot)
        {
            // Get the event type.
            var eventType = Deserialize <SlackEvent>(stream).Type ?? string.Empty;

            stream.Seek(0, SeekOrigin.Begin);

            if (eventType.Equals("message", StringComparison.OrdinalIgnoreCase))
            {
                // We received a message.
                ProcessMessage(stream, handshake, bot);
            }
            else if (eventType.Equals("team_join", StringComparison.OrdinalIgnoreCase))
            {
                // Someone joined the team.
                ProcessTeamJoin(stream);
            }
            else if (eventType.Equals("channel_created", StringComparison.OrdinalIgnoreCase))
            {
                // Someone created a channel.
                ProcessChannelCreated(stream);
            }
            else if (eventType.Equals("group_joined", StringComparison.OrdinalIgnoreCase))
            {
                // We joined a group.
                ProcessGroupJoined(stream);
            }
            else if (eventType.Equals("group_left", StringComparison.OrdinalIgnoreCase))
            {
                // We left a group.
                ProcessGroupLeft(stream);
            }
        }
Example #3
0
        public void Initialize(SlackHandshake handshake)
        {
            // Clear the dictionary.
            _dictionary.Clear();

            // Add channels
            foreach (var slackUser in handshake.Users)
            {
                var user = new User {
                    Id = slackUser.Id, DisplayName = slackUser.Profile.FirstName, Username = slackUser.Name
                };
                _dictionary.AddOrUpdate(slackUser.Id, user, (k, v) => user);
            }
        }
Example #4
0
        private void ProcessMessage(Stream stream, SlackHandshake handshake, User bot)
        {
            var message = Deserialize <SlackMessage>(stream);

            if (string.IsNullOrWhiteSpace(message.SubType))
            {
                var room = _rooms.Get(message.ChannelId);
                var user = _users.Get(message.UserId);

                // Unknown room or user?
                if (room == null || user == null)
                {
                    return;
                }

                // Replace <@USERID> with the user name.
                var text = handshake.Users.Aggregate(message.Text,
                                                     (m, u) => Regex.Replace(m, $"<@{u.Id}>", $"@{u.Name}"));

                _messageQueue.Enqueue(new MessageEvent(_broker)
                {
                    Bot     = bot,
                    Message = new Message {
                        Text = text, User = user
                    },
                    Room = room
                });
            }
            else if (message.SubType.Equals("channel_join", StringComparison.OrdinalIgnoreCase))
            {
                // A user joined the channel.
            }
            else if (message.SubType.Equals("channel_left", StringComparison.OrdinalIgnoreCase))
            {
                // A user left the channel.
            }
        }