From 0a29dbebf4454611aa25ce6e94560774f47d720e Mon Sep 17 00:00:00 2001 From: Don Brown Date: Sun, 17 Jan 2016 01:23:29 -0700 Subject: [PATCH 1/2] Fixed to work with local hipslack proxy --- rtmbot.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/rtmbot.py b/rtmbot.py index 3bd4184..ca95498 100755 --- a/rtmbot.py +++ b/rtmbot.py @@ -14,10 +14,50 @@ from slackclient import SlackClient +import requests + +# These two lines enable debugging at httplib level (requests->urllib3->http.client) +# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA. +# The only thing missing will be the response.body which is not logged. +try: + import http.client as http_client +except ImportError: + # Python 2 + import httplib as http_client +http_client.HTTPConnection.debuglevel = 1 + +# You must initialize logging, otherwise you'll not see debug output. +logging.basicConfig() +logging.getLogger().setLevel(logging.DEBUG) +requests_log = logging.getLogger("requests.packages.urllib3") +requests_log.setLevel(logging.DEBUG) +requests_log.propagate = True + def dbg(debug_string): if debug: logging.info(debug_string) +try: + # Try for Python3 + from urllib.parse import urlencode + from urllib.request import urlopen +except: + # Looks like Python2 + from urllib import urlencode + from urllib2 import urlopen + +def patched_do(self, token, request="?", post_data={}, domain="172.17.0.1:8282"): + post_data["token"] = token + try: + post_data = urlencode(post_data) + except Exception as e: + logging.exception("blah") + url = 'http://{}/api/{}'.format(domain, request) + return urlopen(url, post_data.encode('utf-8')) + +from slackclient._slackrequest import SlackRequest +SlackRequest.do = patched_do + class RtmBot(object): def __init__(self, token): self.last_ping = 0 @@ -33,7 +73,9 @@ def start(self): self.load_plugins() while True: for reply in self.slack_client.rtm_read(): + time.sleep(2) self.input(reply) + time.sleep(2) self.crons() self.output() self.autoping() @@ -163,7 +205,8 @@ def main_loop(): except KeyboardInterrupt: sys.exit(0) except: - logging.exception('OOPS') + import time + logging.exception('OOPS: {}'.format(time.time())) def parse_args(): @@ -185,14 +228,14 @@ def parse_args(): directory )) - config = yaml.load(file(args.config or 'rtmbot.conf', 'r')) + config = yaml.load(open(args.config or 'rtmbot.conf', 'r')) debug = config["DEBUG"] bot = RtmBot(config["SLACK_TOKEN"]) site_plugins = [] files_currently_downloading = [] job_hash = {} - if config.has_key("DAEMON"): + if "DAEMON" in config: if config["DAEMON"]: import daemon with daemon.DaemonContext(): From 266404ae6223fb17432b5a66c174234178d31ff4 Mon Sep 17 00:00:00 2001 From: Don Brown Date: Tue, 19 Jan 2016 00:15:08 -0700 Subject: [PATCH 2/2] Fix socket error, reply on message --- .gitignore | 2 +- plugins/repeat.py | 8 ++++++++ rtmbot.py | 15 +++++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 plugins/repeat.py diff --git a/.gitignore b/.gitignore index a044a8c..1b144ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.pyc /rtmbot.conf -/plugins/** env +venv diff --git a/plugins/repeat.py b/plugins/repeat.py new file mode 100644 index 0000000..97158bb --- /dev/null +++ b/plugins/repeat.py @@ -0,0 +1,8 @@ +import time +crontable = [] +outputs = [] + +def process_message(data): + print("\nIncoming: {}".format(data)) + outputs.append([data['channel'], "from repeat1 \"{}\" in channel {}".format(data['text'], data['channel']) ]) + diff --git a/rtmbot.py b/rtmbot.py index ca95498..10b984a 100755 --- a/rtmbot.py +++ b/rtmbot.py @@ -10,6 +10,7 @@ import sys import time import logging +import errno from argparse import ArgumentParser from slackclient import SlackClient @@ -72,10 +73,12 @@ def start(self): self.connect() self.load_plugins() while True: - for reply in self.slack_client.rtm_read(): - time.sleep(2) - self.input(reply) - time.sleep(2) + try: + for reply in self.slack_client.rtm_read(): + self.input(reply) + except OSError as e: + if e.errno != errno.EAGAIN: + raise self.crons() self.output() self.autoping() @@ -98,12 +101,12 @@ def output(self): limiter = False for output in plugin.do_output(): channel = self.slack_client.server.channels.find(output[0]) - if channel != None and output[1] != None: + if channel is not None and output[1] != None: if limiter == True: time.sleep(.1) limiter = False message = output[1].encode('ascii','ignore') - channel.send_message("{}".format(message)) + channel.send_message("{}".format(message.decode('ascii'))) limiter = True def crons(self): for plugin in self.bot_plugins: