Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit a4641c8

Browse files
authored
Merge pull request #57 from philipyun103/master
Adding API Access to Plugin Context (Updated)
2 parents f785ed9 + 6a4b3ea commit a4641c8

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,18 @@ Plugins can also run methods on a schedule. This allows a plugin to poll for upd
8989
####Plugin misc
9090
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.
9191

92+
####Direct API Calls
93+
You can directly call the Slack web API in your plugins by including the following import:
94+
95+
from client import slack_client
96+
97+
You can also rename the client on import so it can be easily referenced like shown below:
98+
99+
from client import slack_client as sc
100+
101+
Direct API calls can be called in your plugins in the following form:
102+
103+
sc.api_call("API.method", "parameters")
104+
92105
####Todo:
93106
Some rtm data should be handled upstream, such as channel and user creation. These should create the proper objects on-the-fly.

client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from rtmbot import RtmBot
2+
3+
slack_client = None
4+
5+
6+
def init(config):
7+
global slack_client
8+
bot = RtmBot(config)
9+
slack_client = bot.slack_client
10+
return bot
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __future__ import unicode_literals
2+
from client import slack_client as sc
3+
4+
for user in sc.api_call("users.list")["members"]:
5+
print(user["name"], user["id"])

rtmbot.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env python
2-
import sys
32
from argparse import ArgumentParser
4-
3+
import sys
4+
import os
55
import yaml
6-
from rtmbot import RtmBot
6+
import client
7+
8+
sys.path.append(os.getcwd())
79

810

911
def parse_args():
@@ -19,7 +21,7 @@ def parse_args():
1921
# load args with config path
2022
args = parse_args()
2123
config = yaml.load(open(args.config or 'rtmbot.conf', 'r'))
22-
bot = RtmBot(config)
24+
bot = client.init(config)
2325
try:
2426
bot.start()
2527
except KeyboardInterrupt:

rtmbot/core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ def __init__(self, config):
4848
# initialize stateful fields
4949
self.last_ping = 0
5050
self.bot_plugins = []
51-
self.slack_client = None
51+
self.slack_client = SlackClient(self.token)
5252

5353
def _dbg(self, debug_string):
5454
if self.debug:
5555
logging.info(debug_string)
5656

5757
def connect(self):
5858
"""Convenience method that creates Server instance"""
59-
self.slack_client = SlackClient(self.token)
6059
self.slack_client.rtm_connect()
6160

6261
def _start(self):

0 commit comments

Comments
 (0)