You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+41-36Lines changed: 41 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,30 +24,29 @@ Installation
24
24
-----------
25
25
26
26
1. Create your project
27
+
27
28
mkdir myproject
28
29
cd myproject
29
30
30
31
2. Install rtmbot (ideally into a virtualenv https://virtualenv.readthedocs.io/en/latest/)
31
32
32
33
pip install rtmbot
33
34
34
-
3. Create conf file and configure rtmbot (https://api.slack.com/bot-users)
35
-
36
-
vi rtmbot.conf
35
+
3. Create and rtmbot.conf file and configure rtmbot (https://api.slack.com/bot-users)
37
36
38
-
DEBUG: True # make this False in production
39
-
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
40
-
ACTIVE_PLUGINS:
41
-
- plugins.repeat.RepeatPlugin
42
-
- plugins.test.TestPlugin
37
+
# Add the following to rtmbot.conf
38
+
DEBUG: True # make this False in production
39
+
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
40
+
ACTIVE_PLUGINS:
41
+
- plugins.repeat.RepeatPlugin
43
42
44
43
```DEBUG``` will adjust logging verbosity and cause the runner to exit on exceptions, generally making dubugging more pleasant.
45
44
46
45
```SLACK_TOKEN``` is needed to authenticate with your Slack team. More info at https://api.slack.com/web#authentication
47
46
48
47
```ACTIVE_PLUGINS``` RTMBot will attempt to import any Plugin specified in `ACTIVE_PLUGINS` (relative to your python path) and instantiate them as plugins. These specified classes should inherit from the core Plugin class.
49
48
50
-
For example, if your python path includes '/path/to/myproject' and you include `plugins.repeat.RepeatPlugin` in ACTIVE_PLUGINS, it will find the RepeatPlugin class within /path/to/myproject/plugins/repeat.py and instantiate it then attach it to your running RTMBot.
49
+
For example, if your python path includes '/path/to/myproject' and you include `plugins.repeat.RepeatPlugin` in ACTIVE_PLUGINS, it will find the RepeatPlugin class within /path/to/myproject/plugins/repeat.py and instantiate it, then attach it to your running RTMBot.
51
50
52
51
A Word on Structure
53
52
-------
@@ -64,13 +63,16 @@ RtmBot
64
63
65
64
Add Plugins
66
65
-------
66
+
Plugins can live within any python module, but we recommend just putting them in ./plugins. (Don't forget to add an __init__.py file to your directory to make it a module -- use `touch __init__.py` within your plugin directory to create one)
67
67
68
68
To add a plugin, create a file within your plugin directory (./plugins is a good place for it).
69
69
70
+
mkdir plugins
71
+
touch plugins/__init__.py
70
72
cd plugins
71
73
vi myplugin.py
72
74
73
-
Add your plugin content into this file. Here's an example that will just print all of the requests it receives. See below for more information on available methods.
75
+
Add your plugin content into this file. Here's an example that will just print all of the requests it receives to the console. See below for more information on available methods.
74
76
75
77
from future import print_function
76
78
from rtmbot.core import Plugin
@@ -82,27 +84,44 @@ Add your plugin content into this file. Here's an example that will just print a
82
84
83
85
You can install as many plugins as you like, and each will handle every event received by the bot indepentently.
[data['channel'], 'from repeat1 "{}" in channel {}'.format(
105
+
data['text'], data['channel']
106
+
)]
107
+
)
89
108
90
-
The repeat plugin will now be loaded by the bot on startup.
109
+
The repeat plugin will now be loaded by the bot on startup. Run `rtmbot` from console to start your RtmBot.
91
110
92
-
./rtmbot.py
111
+
rtmbot
93
112
94
113
Create Plugins
95
114
--------
96
115
97
116
####Incoming data
98
-
All events from the RTM websocket are sent to the registered plugins. To act on an event, create a function definitioncalled process_(api_method) that accepts a single arg. For example, to handle incoming messages:
117
+
All events from the RTM websocket are sent to the registered plugins. To act on an event, create a function definition, inside your Plugin class, called process_(api_method) that accepts a single arg for data. For example, to handle incoming messages:
99
118
100
119
def process_message(self, data):
101
120
print data
102
121
103
122
This will print the incoming message json (dict) to the screen where the bot is running.
104
123
105
-
Plugins having a method defined as ```catch_all(data)``` will receive ALL events from the websocket. This is useful for learning the names of events and debugging.
124
+
Plugins having a method defined as ```catch_all(self, data)``` will receive ALL events from the websocket. This is useful for learning the names of events and debugging.
106
125
107
126
For a list of all possible API Methods, look here: https://api.slack.com/rtm
108
127
@@ -130,37 +149,23 @@ Plugins also have access to the connected SlackClient instance for more complex
130
149
####Timed jobs
131
150
Plugins can also run methods on a schedule. This allows a plugin to poll for updates or perform housekeeping during its lifetime. This is done by appending a two item array to the crontable array. The first item is the interval in seconds and the second item is the method to run. For example, this will print "hello world" every 10 seconds.
132
151
152
+
Note that the Job uses the associated Plugin's output array to output back to the RTM stream.
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.
151
-
152
-
####Direct API Calls
153
-
You can directly call the Slack web API in your plugins by including the following import:
154
-
155
-
from client import slack_client
156
-
157
-
You can also rename the client on import so it can be easily referenced like shown below:
158
-
159
-
from client import slack_client as sc
160
-
161
-
Direct API calls can be called in your plugins in the following form:
162
-
163
-
sc.api_call("API.method", "parameters")
164
-
165
-
####Todo:
166
-
Some rtm data should be handled upstream, such as channel and user creation. These should create the proper objects on-the-fly.
171
+
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.
0 commit comments