Merge pull request #57 from philipyun103/master

Adding API Access to Plugin Context (Updated)
This commit is contained in:
Jeff Ammons 2016-08-25 15:44:38 -07:00 committed by GitHub
commit a4641c8936
5 changed files with 35 additions and 6 deletions

View file

@ -89,5 +89,18 @@ Plugins can also run methods on a schedule. This allows a plugin to poll for upd
####Plugin misc
The data within a plugin persists for the life of the rtmbot process. If you need persistent data, you should use something like sqlite or the python pickle libraries.
####Direct API Calls
You can directly call the Slack web API in your plugins by including the following import:
from client import slack_client
You can also rename the client on import so it can be easily referenced like shown below:
from client import slack_client as sc
Direct API calls can be called in your plugins in the following form:
sc.api_call("API.method", "parameters")
####Todo:
Some rtm data should be handled upstream, such as channel and user creation. These should create the proper objects on-the-fly.

10
client.py Normal file
View file

@ -0,0 +1,10 @@
from rtmbot import RtmBot
slack_client = None
def init(config):
global slack_client
bot = RtmBot(config)
slack_client = bot.slack_client
return bot

View file

@ -0,0 +1,5 @@
from __future__ import unicode_literals
from client import slack_client as sc
for user in sc.api_call("users.list")["members"]:
print(user["name"], user["id"])

View file

@ -1,9 +1,11 @@
#!/usr/bin/env python
import sys
from argparse import ArgumentParser
import sys
import os
import yaml
from rtmbot import RtmBot
import client
sys.path.append(os.getcwd())
def parse_args():
@ -19,7 +21,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:

View file

@ -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):