def test_issue_631_sharing_event_loop(self): self.success = None self.text = "This message was sent to verify issue #631" self.rtm_client = RTMClient( token=self.bot_token, run_async=False, loop=asyncio.new_event_loop(), # TODO: this doesn't work without this ) # @RTMClient.run_on(event="message") # def send_reply(**payload): # self.logger.debug(payload) # data = payload['data'] # web_client = payload['web_client'] # web_client._event_loop = self.loop # # Maybe you will also need the following line uncommented # # web_client.run_async = True # # if self.text in data['text']: # channel_id = data['channel'] # thread_ts = data['ts'] # try: # self.success = web_client.chat_postMessage( # channel=channel_id, # text="Thanks!", # thread_ts=thread_ts # ) # except Exception as e: # # slack.rtm.client:client.py:446 When calling '#send_reply()' # # in the 'test_rtm_client' module the following error was raised: This event loop is already running # self.logger.error(traceback.format_exc()) # raise e # Solution (1) for #631 @RTMClient.run_on(event="message") def send_reply(**payload): self.logger.debug(payload) data = payload['data'] web_client = payload['web_client'] try: if "text" in data and self.text in data["text"]: channel_id = data['channel'] thread_ts = data['ts'] self.success = web_client.chat_postMessage( channel=channel_id, text="Thanks!", thread_ts=thread_ts ) except Exception as e: self.logger.error(traceback.format_exc()) raise e def connect(): self.logger.debug("Starting RTM Client...") self.rtm_client.start() t = threading.Thread(target=connect) t.setDaemon(True) t.start() try: self.assertIsNone(self.success) time.sleep(5) self.web_client = WebClient( token=self.bot_token, run_async=False, ) new_message = self.web_client.chat_postMessage(channel=self.channel_id, text=self.text) self.assertFalse("error" in new_message) time.sleep(5) self.assertIsNotNone(self.success) finally: t.join(.3)
def init_client(token): web_client = WebClient(token=token) rtm_client = RTMClient(token=token) return web_client, rtm_client
(key, val) = line.split() assignments[userName[key]] = val f.close() print("Request:", cmd_user) api_call = web_client.users_list() users = api_call["members"] user_map = {user["id"]:user["name"] for user in users} # retrieve all users so we can find our bot user = user_map[cmd_user] if user in assignments.keys(): print("Bot ID for '" + user + "' is " + cmd_user) print("Selection for " + user + " is " + assignments[user]) giftee = assignments[user] text="This year you will be getting a gift for: " + giftee response = web_client.conversations_open(users=[cmd_user]) conversation_id = response['channel']['id'] send_direct_message(web_client, conversation_id, text) time.sleep(5) return # Instantiate Slack Client rtm_client = RTMClient(token="******************") rtm_client.start()
import os from slack import RTMClient from slack.errors import SlackApiError @RTMClient.run_on(event='message') def say_hello(**payload): data = payload['data'] web_client = payload['web_client'] rtm_client = payload['rtm_client'] if 'text' in data and 'Hello' in data.get('text', []): channel_id = data['channel'] thread_ts = data['ts'] user = data['user'] try: response = web_client.chat_postMessage(channel=channel_id, text=f"Hi <@{user}>!", thread_ts=thread_ts) except SlackApiError as e: # You will get a SlackApiError if "ok" is False assert e.response["ok"] is False assert e.response[ "error"] # str like 'invalid_auth', 'channel_not_found' print(f"Got an error: {e.response['error']}") rtm_client = RTMClient(token=os.environ["SLACK_API_TOKEN"]) rtm_client.start()
def run_echo_svr(): rtm_client = RTMClient(token=BOT_TOKEN) rtm_client.start()