From 1fb557b178c77a4f5ff76899a0fa73442d2d75b2 Mon Sep 17 00:00:00 2001 From: philipyun103 Date: Sun, 7 Aug 2016 00:42:37 -0700 Subject: [PATCH] Add API Context to Plugin Context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updated idea of @zbarahal’s PR (PR #20) to reflect the current master. --- client.py | 10 ++++++++++ docs/example-plugins/directAPIcall.py | 5 +++++ rtmbot.py | 7 +++++-- rtmbot/core.py | 3 +-- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 client.py create mode 100644 docs/example-plugins/directAPIcall.py diff --git a/client.py b/client.py new file mode 100644 index 0000000..bbf9314 --- /dev/null +++ b/client.py @@ -0,0 +1,10 @@ +from slackclient import SlackClient +from rtmbot import RtmBot + +client = None + +def init(config): + global client + bot = RtmBot(config) + client = bot.slack_client + return bot diff --git a/docs/example-plugins/directAPIcall.py b/docs/example-plugins/directAPIcall.py new file mode 100644 index 0000000..0ddebd5 --- /dev/null +++ b/docs/example-plugins/directAPIcall.py @@ -0,0 +1,5 @@ +from __future__ import unicode_literals +from client import client as sc + +for user in sc.api_call("users.list")["members"]: + print(user["name"], user["id"]) diff --git a/rtmbot.py b/rtmbot.py index 77c7fa3..a1ecc0c 100755 --- a/rtmbot.py +++ b/rtmbot.py @@ -1,9 +1,12 @@ #!/usr/bin/env python import sys +import os +sys.path.append(os.getcwd()) + from argparse import ArgumentParser import yaml -from rtmbot import RtmBot +import client def parse_args(): @@ -19,7 +22,7 @@ def parse_args(): # load args with config path args = parse_args() config = yaml.load(open(args.config or 'rtmbot.conf', 'r')) -bot = RtmBot(config) +bot = client.init(config) try: bot.start() except KeyboardInterrupt: diff --git a/rtmbot/core.py b/rtmbot/core.py index 1b391a3..dd33e19 100755 --- a/rtmbot/core.py +++ b/rtmbot/core.py @@ -48,7 +48,7 @@ class RtmBot(object): # initialize stateful fields self.last_ping = 0 self.bot_plugins = [] - self.slack_client = None + self.slack_client = SlackClient(self.token) def _dbg(self, debug_string): if self.debug: @@ -56,7 +56,6 @@ class RtmBot(object): def connect(self): """Convenience method that creates Server instance""" - self.slack_client = SlackClient(self.token) self.slack_client.rtm_connect() def _start(self):