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 3bd4184..10b984a 100755 --- a/rtmbot.py +++ b/rtmbot.py @@ -10,14 +10,55 @@ import sys import time import logging +import errno from argparse import ArgumentParser 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 @@ -32,8 +73,12 @@ def start(self): self.connect() self.load_plugins() while True: - for reply in self.slack_client.rtm_read(): - self.input(reply) + 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() @@ -56,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: @@ -163,7 +208,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 +231,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():