예제 #1
0
    def handle(cls, input_message):
        text = input_message.text

        if text.lower().startswith('remind everyone '):
            channel = GENERAL_CHANNEL
            audience = 'everyone'
            text = text[16:]
        elif text.lower().startswith('remind channel '):
            channel = input_message.channel
            audience = 'this channel'
            text = text[15:]
        else:
            raise InvalidInputException('Bad reminder audience')

        try:
            timestamp, message = text.strip().split(' that ')
        except ValueError:
            raise InvalidInputException('No message in reminder')

        try:
            event_time = cls.get_datetime_from_timestamp(
                timestamp[3:], relative=text.lower().startswith('in '))

            if event_time < from_now(minutes=MINIMUM_WAIT_MINS - 1):
                raise TimeTooCloseException()

        except TimeTooCloseException:
            print "TimeTooCloseException: %s" % input_message.text
            return OutputMessage(
                channel=input_message.channel,
                text=("Sorry, I can't do that. Give me at least "
                      "5 minutes to schedule a reminder."))

        else:
            cls.collection.insert({
                'time':
                event_time,
                'user':
                input_message.user_id,
                'channel':
                channel,
                'message':
                message,
                'type':
                audience if audience is 'everyone' else 'channel',
                'pending':
                True
            })

            return OutputMessage(channel=input_message.channel,
                                 text="Okay, I'll remind %s then." % audience)
예제 #2
0
    def received_message(self, m):
        try:
            slack_activity = json.loads(m.data)

            message = self.get_input_message_from_activity(slack_activity)

            handler = self.map_message_to_handler(message.text.lower(),
                                                  authorized=message.user_id
                                                  in EBOARD)

            bot_response = handler.handle(message)

            self.send_response(bot_response)

        except (NotUserMessageException, NotZetebotActivityException):
            pass

        except NoResponseException as e:
            print e.message

        except InvalidInputException as e:
            print "InvalidInputException: %s" % e.message

        except Exception:
            # Something actually went wrong.
            trace = traceback.format_exc()
            print trace
            error_response = OutputMessage(channel=config.debug,
                                           text=slack_activity + trace)
            self.send_response(error_response)
예제 #3
0
    def handle(cls, input_message):
        author = input_message.text[5:].lstrip().lower()
        quote_doc = cls._get_random_quote_doc(author=author)

        if not quote_doc:
            message_text = ("I can't remember any quotes"
                            "%s, too blackout" %
                            (' from ' + author if author else ''))
        else:
            message_text = '>\"{0}\" - {1}'.format(
                quote_doc.get('quote'),
                quote_doc.get('user').title())

        return OutputMessage(channel=input_message.channel, text=message_text)
예제 #4
0
    def handle(cls, input_message):
        text = input_message.text.lower()

        x = ' '.join(text.split(' ')[2:]).strip()
        if x == 'you':
            x = config.botname

        result = cls.collection.find_one({"x": x})

        if result is None:
            output_text = "I don't know.  I'm just a useless bot."
        else:
            output_text = result.get('y')

        return OutputMessage(channel=input_message.channel, text=output_text)
예제 #5
0
    def handle(cls, input_message):
        text = input_message.text
        username = text[6:]

        plus, minus, meh = 0, 0, 0

        karma_doc = cls.collection.find_one({"name": username.title()})
        if karma_doc:
            plus = karma_doc.get('++', 0)
            minus = karma_doc.get('--', 0)
            meh = karma_doc.get('+-', 0)

        return OutputMessage(
            channel=input_message.channel,
            text='Karma for %s: %i++, %i--, %i+-' % (username, plus, minus, meh)
        )
예제 #6
0
    def handle(cls, input_message):
        text = input_message.text.lower()

        message = None

        # hello
        if any(match(r"(.*?)\b%s\b(.*?)" % words, text) for words in cls.hello):
            message = sample(cls.hello_response, 1)[0]

        # thanks!
        if any(words in text for words in cls.thanks):
            message = ':heart:'

        if message:
            return OutputMessage(
                channel=input_message.channel,
                text=message
            )

        raise NotZetebotActivityException()
예제 #7
0
    def handle(cls, input_message):
        """ text: user quote (with "") """
        words = input_message.text.split('"')
        user = words[0].lower().strip()[15:]
        quote = words[1].strip()

        if not quote:
            raise InvalidInputException()

        if not user:
            user = "******"

        cls.collection.update({
            "user": user,
            "quote": quote
        }, {
            "user": user,
            "quote": quote
        },
                              upsert=True)

        return OutputMessage(channel=input_message.channel,
                             text="Quote of the week, anyone?")
예제 #8
0
    def handle(cls, input_message):
        # text: x is y OR x are y
        text = input_message.text
        text = ' '.join(text.split(' ')[2:]).strip()

        words = text.split(' are ')
        if len(words) == 1:
            words = text.split(' is ')
        if len(words) == 1:
            raise InvalidInputException()

        x = words[0].lower()

        if x == 'you':
            x = config.botname
            text = text.replace('you are', 'I am')

        cls.collection.update({"x": x}, {"x": x, "y": text}, upsert=True)

        return OutputMessage(
            channel=input_message.channel,
            text="OK, %s" % text,
        )
예제 #9
0
 def closed(self, code, reason=None):
     closed_message = 'Connection closed unexpectedly. Reason: %s' % reason
     self.send_response(
         OutputMessage(channel=config.debug, text=closed_message))
     print closed_message