def start(self): """Run the Slack network connection.""" self.client = slackclient.SlackClient(self.api_token) self.client.rtm_connect() while True: try: for response in self.client.rtm_read(): self.log.debug(response) if all(x in response for x in ("text", "channel", "user")): r_channel = response["channel"] r_user = response["user"] try: channel = self.client.server.channels.find( r_channel).name user = self.client.server.users.find(r_user).name except AttributeError: continue msg = response["text"] self.log.info("-#%s- <%s> %s" % (channel, user, msg)) _msg = message.Reply(self, msg, user, channel) plugin.poll_messages(self, _msg) time.sleep(.1) except Exception: # NOTE(jk0): Disconnected? Try to reconnect. self.client.rtm_connect() continue
def on_privmsg(self, _connection, event): """Handle private messages.""" msg = event.arguments[0] source = event.source.split("@", 1)[0] target = event.source.nick _msg = message.Reply(self, msg, source, target) if self.target != self.nick: self.log.info("<%s> %s" % (target, msg)) plugin.poll_messages(self, _msg, private=True)
def on_pubmsg(self, _connection, event): """Handle public messages.""" nick = event.source.nick msg = event.arguments[0] source = event.source.split("@", 1)[0] target = event.target self.log.info("-%s- <%s> %s" % (target, nick, msg)) _msg = message.Reply(self, msg, source, target) plugin.poll_messages(self, _msg)
def start(self): """Run the Slack network connection.""" self.client = slackclient.SlackClient(self.api_token) self.client.rtm_connect() q = queue.MessageQueue() q.watch(self) count = 0 while True: try: for response in self.client.rtm_read(): self.log.debug(response) if all(x in response for x in ("text", "channel", "user")): if count == 0: # NOTE(jk0): rtm_read() often times comes back with # old messages after reconnecting. Let's attempt to # ignore them here. count += 1 break r_channel = response["channel"] r_user = response["user"] try: channel = self.client.server.channels.find( r_channel).name user = self.client.server.users.find(r_user).name except AttributeError: continue msg = response["text"] self.log.info("-#%s- <%s> %s" % (channel, user, msg)) _msg = message.Reply(self, msg, user, channel) plugin.poll_messages(self, _msg) time.sleep(1) except Exception: # NOTE(jk0): Disconnected? Try to reconnect. # https://github.com/slackhq/python-slackclient/issues/36 self.client.rtm_connect() continue
def start(self): """Run the Slack network connection.""" self.client = slackclient.SlackClient(self.api_token) self.client.rtm_connect() count = 0 while True: try: for response in self.client.rtm_read(): self.log.debug(response) if all(x in response for x in ("text", "channel", "user")): if count == 0: # NOTE(jk0): rtm_read() often times comes back with # old messages after reconnecting. Let's attempt to # ignore them here. count += 1 break r_channel = response["channel"] r_user = response["user"] try: channel = self.client.server.channels.find( r_channel).name user = self.client.server.users.find(r_user).name except AttributeError: continue msg = response["text"] self.log.info("-#%s- <%s> %s" % (channel, user, msg)) _msg = message.Reply(self, msg, user, channel) plugin.poll_messages(self, _msg) time.sleep(1) except Exception: # NOTE(jk0): Disconnected? Try to reconnect. # https://github.com/slackhq/python-slackclient/issues/36 self.client.rtm_connect() continue