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()
async def test_issue_530_async(self): try: rtm_client = RTMClient(token="I am not a token", run_async=True) await rtm_client.start() self.fail("Raising an error here was expected") except Exception as e: self.assertEqual( "The request to the Slack API failed.\n" "The server responded with: {'ok': False, 'error': 'invalid_auth'}", str(e)) finally: if not rtm_client._stopped: rtm_client.stop()
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()
def test_issue_530(self): try: rtm_client = RTMClient(token="I am not a token", run_async=False, loop=asyncio.new_event_loop()) rtm_client.start() self.fail("Raising an error here was expected") except Exception as e: self.assertEqual( str(e), "The server responded with: {'ok': False, 'error': 'invalid_auth'}" ) finally: if not rtm_client._stopped: rtm_client.stop()
class BddRTMClient: event_trail = [] def __init__(self): slack_token = os.environ["SLACK_BDD_API_TOKEN"] self.rtm_client = RTMClient(token=slack_token) self.rtm_client.on(event="message", callback=self.listen) self.rtm_thread = threading.Thread(name="rtm_client", target=self.rtm_client.start) self.rtm_thread.start() def listen(self, **payload): self.event_trail.append(payload["data"]) def stop(self): self.rtm_client.stop()
class Client: _web_client: WebClient _rtm_client: RTMClient def __init__( self, *, token: str, ) -> None: self._webclient = WebClient( token=token, run_async=True, ) self._rtm_client = RTMClient( token=token, run_async=True, ) async def post_message( self, channel: str, text: str, ) -> Dict[str, Any]: log.info('Sending slack message to channel %s: %s', channel, text) return await self._webclient.chat_postMessage( channel=channel, text=text, icon_emoji='robot_face', ) def start_rtm_client(self) -> asyncio.Future: return self._rtm_client.start() def stop_rtm_client(self) -> None: return self._rtm_client.stop() def get_message_queue(self) -> asyncio.Queue: return _message_queue