Beispiel #1
0
class IrcPlugin(Plugin):
    def __init__(self):
        self._handlers = Handlers()
        super(IrcPlugin, self).__init__()

    def register_command(self, handler):
        self._handlers.register(handler.__name__, handler)

    def onMessageReceived(self, sender_nick, channel, msg):
        split = msg.split()
        if len(split) >= 2:
            highlight = split[0].lower()
        else:
            highlight = ""

        highlight = highlight.lstrip("@")

        if not highlight.startswith(self.config.nick):
            return

        command, args = (split[1].lower(), split[2:])

        try:
            self.handlers.process(command, self.bot, sender_nick, channel, *args)
        except NoHandlerError as exc:
            if exc.close_matches:
                self.bot.send_message(channel, "@%s: did you mean `%s`?" % (sender_nick, exc.close_matches[0]))
        except:
            traceback.print_exc()
Beispiel #2
0
    def __init__(self, api_client):
        super(SlackPlugin, self).__init__()

        self._handlers = Handlers()
        self._data_cache = SlackDataCache(api_client)
        self._bot = SlackBot(api_client, self._data_cache)
Beispiel #3
0
class SlackPlugin(Plugin):
    def __init__(self, api_client):
        super(SlackPlugin, self).__init__()

        self._handlers = Handlers()
        self._data_cache = SlackDataCache(api_client)
        self._bot = SlackBot(api_client, self._data_cache)

    @property
    def bot(self):
        return self._bot

    def register_command(self, handler):
        self._handlers.register(handler.__name__, handler)

    def _onMessage(self, payload):
        event_type = payload["type"]

        if event_type == "message":
            self._onChat(payload)
        elif event_type == "user_change":
            self._data_cache.onUserChange(payload)
        elif event_type.startswith("channel_"):
            self._data_cache.onChannelChange(payload)
        elif event_type == "hello":
            print("Connected to Slack RTM!")
            self._data_cache.initialize()

    @inlineCallbacks
    def _onChat(self, payload):
        if payload.get("subtype") == "bot_message":
            returnValue(None)

        if payload.get("bot_id") is not None:
            returnValue(None)

        try:
            words = payload["text"].split()
        except KeyError:
            returnValue(None)

        if len(words) < 2:
            returnValue(None)

        self_info = yield self._data_cache.get_self()
        my_id = "<@" + self_info["user_id"] + ">"
        my_name = ("harold", self_info["user"])

        mention, command, args = (words[0], words[1].lower(), words[2:])
        if not (mention.lower().startswith(my_name) or my_id in mention):
            returnValue(None)

        channel_id = payload["channel"]
        channel_info = yield self._data_cache.get_channel_by_id(channel_id)
        channel = '#' + channel_info["name"]

        user = yield self._data_cache.get_user_by_id(payload["user"])
        sender_nick = user["name"]

        try:
            self._handlers.process(command, self.bot, sender_nick, channel, *args)
        except NoHandlerError as exc:
            if exc.close_matches:
                self.bot.send_message(channel, "@%s: did you mean `%s`?" % (sender_nick, exc.close_matches[0]))
        except:
            print("Exception while handling command %r" % words)
            traceback.print_exc()
Beispiel #4
0
 def __init__(self):
     self._handlers = Handlers()
     super(IrcPlugin, self).__init__()
Beispiel #5
0
 def __init__(self, api_client):
     self._handlers = Handlers()
     self._data_cache = SlackDataCache(api_client)
     self._bot = SlackBot(api_client, self._data_cache)
Beispiel #6
0
class SlackPlugin(object):
    def __init__(self, api_client):
        self._handlers = Handlers()
        self._data_cache = SlackDataCache(api_client)
        self._bot = SlackBot(api_client, self._data_cache)

    @property
    def bot(self):
        return self._bot

    def register_command(self, handler):
        self._handlers.register(handler.__name__, handler)

    def _onMessage(self, payload):
        event_type = payload["type"]

        if event_type == "message":
            self._onChat(payload)
        elif event_type == "user_change":
            self._data_cache.onUserChange(payload)
        elif event_type.startswith("channel_"):
            self._data_cache.onChannelChange(payload)
        elif event_type == "hello":
            print("Connected to Slack RTM!")
            self._data_cache.initialize()

    @inlineCallbacks
    def _onChat(self, payload):
        if payload.get("subtype") == "bot_message":
            returnValue(None)

        if payload.get("bot_id") is not None:
            returnValue(None)

        try:
            words = payload["text"].split()
        except KeyError:
            returnValue(None)

        if len(words) < 2:
            returnValue(None)

        self_info = yield self._data_cache.get_self()
        my_id = "<@" + self_info["user_id"] + ">"
        my_name = ("harold", self_info["user"])

        mention, command, args = (words[0], words[1].lower(), words[2:])
        if not (mention.lower().startswith(my_name) or my_id in mention):
            returnValue(None)

        channel_id = payload["channel"]
        channel_info = yield self._data_cache.get_channel_by_id(channel_id)
        channel = '#' + channel_info["name"]

        user = yield self._data_cache.get_user_by_id(payload["user"])
        sender_nick = user["name"]

        try:
            self._handlers.process(command, self.bot, sender_nick, channel,
                                   *args)
        except NoHandlerError as exc:
            if exc.close_matches:
                self.bot.send_message(
                    channel, "@%s: did you mean `%s`?" %
                    (sender_nick, exc.close_matches[0]))
        except:
            print("Exception while handling command %r" % words)
            traceback.print_exc()
Beispiel #7
0
 def __init__(self):
     self._handlers = Handlers()
     super(IrcPlugin, self).__init__()