示例#1
0
 def _register_plugin_actions(self, plugin_class, metadata, cls_instance,
                              fn_name, fn, class_help):
     fq_fn_name = "{}.{}".format(plugin_class, fn_name)
     if fn.__doc__:
         self._help['human'][class_help][
             fq_fn_name] = self._parse_human_help(fn.__doc__)
     for action, config in metadata['plugin_actions'].items():
         if action == 'process':
             event_type = config['event_type']
             RTMClient.on(event=event_type,
                          callback=callable_with_sanitized_event(fn))
         if action in ['respond_to', 'listen_to']:
             for regex in config['regex']:
                 event_handler = {
                     'class': cls_instance,
                     'class_name': plugin_class,
                     'function': fn,
                     'regex': regex
                 }
                 key = "{}-{}".format(fq_fn_name, regex.pattern)
                 self._plugin_actions[action][key] = event_handler
                 self._help['robot'][class_help].append(
                     self._parse_robot_help(regex, action))
         if action == 'schedule':
             Scheduler.get_instance().add_job(fq_fn_name,
                                              trigger='cron',
                                              args=[cls_instance],
                                              id=fq_fn_name,
                                              replace_existing=True,
                                              **config)
         if action == 'route':
             for route_config in config:
                 bottle.route(**route_config)(fn)
    async def test_issue_611(self):
        channel_id = os.environ[SLACK_SDK_TEST_RTM_TEST_CHANNEL_ID]
        text = "This message was sent by <https://slack.dev/python-slackclient/|python-slackclient>! (test_issue_611)"

        self.message_count, self.reaction_count = 0, 0

        async def process_messages(**payload):
            self.logger.info(payload)
            if ("subtype" in payload["data"]
                    and payload["data"]["subtype"] == "message_replied"):
                return  # skip

            self.message_count += 1
            raise Exception("something is wrong!"
                            )  # This causes the termination of the process

        async def process_reactions(**payload):
            self.logger.info(payload)
            self.reaction_count += 1

        rtm = RTMClient(token=self.bot_token, run_async=True)
        RTMClient.on(event="message", callback=process_messages)
        RTMClient.on(event="reaction_added", callback=process_reactions)

        web_client = WebClient(token=self.bot_token, run_async=True)
        message = await web_client.chat_postMessage(channel=channel_id,
                                                    text=text)
        ts = message["ts"]

        await asyncio.sleep(3)

        # intentionally not waiting here
        rtm.start()

        try:
            await asyncio.sleep(3)

            first_reaction = await web_client.reactions_add(channel=channel_id,
                                                            timestamp=ts,
                                                            name="eyes")
            self.assertFalse("error" in first_reaction)
            await asyncio.sleep(2)

            should_be_ignored = await web_client.chat_postMessage(
                channel=channel_id, text="Hello?", thread_ts=ts)
            self.assertFalse("error" in should_be_ignored)
            await asyncio.sleep(2)

            second_reaction = await web_client.reactions_add(
                channel=channel_id, timestamp=ts, name="tada")
            self.assertFalse("error" in second_reaction)
            await asyncio.sleep(2)

            self.assertEqual(self.message_count, 1)
            self.assertEqual(self.reaction_count, 2)
        finally:
            if not rtm._stopped:
                rtm.stop()
示例#3
0
    async def test_issue_558(self):
        channel_id = os.environ[SLACK_SDK_TEST_RTM_TEST_CHANNEL_ID]
        text = "This message was sent by <https://slack.dev/python-slackclient/|python-slackclient>! (test_issue_558)"

        self.message_count, self.reaction_count = 0, 0

        async def process_messages(**payload):
            self.logger.debug(payload)
            self.message_count += 1
            await asyncio.sleep(10)  # this used to block all other handlers

        async def process_reactions(**payload):
            self.logger.debug(payload)
            self.reaction_count += 1

        rtm = RTMClient(token=self.bot_token, run_async=True)
        RTMClient.on(event="message", callback=process_messages)
        RTMClient.on(event="reaction_added", callback=process_reactions)

        web_client = WebClient(token=self.bot_token, run_async=True)
        message = await web_client.chat_postMessage(channel=channel_id,
                                                    text=text)
        self.assertFalse("error" in message)
        ts = message["ts"]
        await asyncio.sleep(3)

        # intentionally not waiting here
        rtm.start()
        await asyncio.sleep(3)

        try:
            first_reaction = await web_client.reactions_add(channel=channel_id,
                                                            timestamp=ts,
                                                            name="eyes")
            self.assertFalse("error" in first_reaction)
            await asyncio.sleep(2)

            message = await web_client.chat_postMessage(channel=channel_id,
                                                        text=text)
            self.assertFalse("error" in message)
            # used to start blocking here

            # This reaction_add event won't be handled due to a bug
            second_reaction = await web_client.reactions_add(
                channel=channel_id, timestamp=ts, name="tada")
            self.assertFalse("error" in second_reaction)
            await asyncio.sleep(2)

            self.assertEqual(self.message_count, 1)
            self.assertEqual(self.reaction_count, 2)  # used to fail
        finally:
            if not rtm._stopped:
                rtm.stop()
示例#4
0
 def start(self, plugins):
     RTMClient.on(event='pong', callback=self.pong)
     RTMClient.on(event='message', callback=self.handle_message)
     self._client.start(plugins)
示例#5
0
 def start(self, plugins):
     self._plugins = plugins
     RTMClient.on(event='open', callback=self._on_open)
     RTMClient.on(event='team_join', callback=self._on_team_join)
     RTMClient.on(event='channel_created',
                  callback=self._on_channel_created)
     RTMClient.on(event='group_joined', callback=self._on_channel_created)
     RTMClient.on(event='mpim_joined', callback=self._on_channel_created)
     RTMClient.on(event='im_created', callback=self._on_channel_created)
     RTMClient.on(event='channel_deleted',
                  callback=self._on_channel_deleted)
     RTMClient.on(event='im_close', callback=self._on_channel_deleted)
     RTMClient.on(event='channel_rename', callback=self._on_channel_updated)
     RTMClient.on(event='channel_archive',
                  callback=self._on_channel_updated)
     RTMClient.on(event='channel_unarchive',
                  callback=self._on_channel_updated)
     RTMClient.on(event='user_change', callback=self._on_user_change)
     self.rtm_client.start()