import re
import sys
import json
from time import time

translate_map = { ord(c): None for c in map(chr, list(range(256))) if not c.isalnum() }


class SlackEvent(dict):
    """
    Event received from the Slack RTM API
    params:
     - event_obj(dict)
    attributes:
     - type(str): Slack event type
     - user(str): Slack user ID (or name if translated), if applicable. default None.
     - channel(str): Slack channel ID (or name if translated), if applicable. default None.
     - ts(float): UTC event timestamp
     - metions(list): List of Slack user IDs (or names if translated) mentioned in event text
    """

    def __init__(self, event_obj):
        super(SlackEvent, self).__init__()
        self.update(event_obj)
        self.mentions = []

        self.type = self.get('type')
        self.ts = self.get('ts', int(time()))
        self.mentions = self._mentions(self.get('text', ''))

        self.user = self.get('user')
        self.channel = self.get('channel')

        if isinstance(self.channel, dict):
            # if channel is newly created, a channel object is returned from api
            # instead of a channel id
            self.channel = self.channel.get('id')

    @property
    def json(self):
        return json.dumps(self)

    def _mentions(self, text):
        mentions = re.findall('<@\w+>', text)

        if mentions and sys.version_info.major == 2:
            return [ unicode(m).translate(translate_map) for m in mentions ]

        if mentions and sys.version_info.major == 3:
            return [ str(m).translate(translate_map) for m in mentions ]

        return []


class SlackMsg(object):
    """
    Slack default formatted message capable of being sent via the RTM API
    params:
     - text(str)
     - channel(str)
    attributes:
     - type: Slack event type
     - ts: UTC time event was received
    """

    def __init__(self, id, channel, text):
        self.sent = False
        self.payload = {'id': id,
                        'type': 'message',
                        'text': text,
                        'channel': channel}
        self.json = json.dumps(self.payload)

@@ -53,96 +53,44 @@
 me%0A%0A
-translate_map = %7B ord(c): None for c in map(chr, list(range(256))) if not c.isalnum() %7D%0A
+mentions_re = re.compile('%3C@(%5Cw+)%3E')
 %0A%0Acl
@@ -814,22 +814,16 @@
 s = 
-self._
 mentions
 (sel
@@ -814,24 +814,35 @@
 s = mentions
+_re.findall
 (self.get('t
@@ -1181,16 +1181,16 @@
 (self):%0A
+
         
@@ -1218,363 +1218,8 @@
 f)%0A%0A
-    def _mentions(self, text):%0A        mentions = re.findall('%3C@%5Cw+%3E', text)%0A%0A        if mentions and sys.version_info.major == 2:%0A            return %5B unicode(m).translate(translate_map) for m in mentions %5D%0A%0A        if mentions and sys.version_info.major == 3:%0A            return %5B str(m).translate(translate_map) for m in mentions %5D%0A%0A        return %5B%5D%0A%0A
 %0Acla

simplify mentions regex