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
+34-9Lines changed: 34 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,32 +27,39 @@ Installation
27
27
28
28
1. Create your project
29
29
30
+
```
30
31
mkdir myproject
31
32
cd myproject
33
+
```
32
34
33
35
2. Install rtmbot (ideally into a [virtualenv](https://virtualenv.readthedocs.io/en/latest/))
34
36
37
+
```
35
38
pip install rtmbot
39
+
```
36
40
37
41
3. Create an rtmbot.conf file and [create a bot for your team](https://api.slack.com/bot-users)
38
42
43
+
```
39
44
# Add the following to rtmbot.conf
40
45
DEBUG: True # make this False in production
41
46
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
42
47
ACTIVE_PLUGINS:
43
48
- plugins.repeat.RepeatPlugin
49
+
```
44
50
45
-
```DEBUG``` will adjust logging verbosity and cause the runner to exit on exceptions, generally making debugging more pleasant.
51
+
`DEBUG` will adjust logging verbosity and cause the runner to exit on exceptions, generally making debugging more pleasant.
46
52
47
-
```SLACK_TOKEN``` is needed to [authenticate with your Slack team.](https://api.slack.com/web#authentication)
53
+
`SLACK_TOKEN` is needed to [authenticate with your Slack team.](https://api.slack.com/web#authentication)
48
54
49
-
```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.
55
+
`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.
50
56
51
57
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.
52
58
53
59
A Word on Structure
54
60
-------
55
61
To give you a quick sense of how this library is structured, there is a RtmBot class which does the setup and handles input and outputs of messages. It will also search for and register Plugins within the specified directory(ies). These Plugins handle different message types with various methods and can also register periodic Jobs which will be executed by the Plugins.
62
+
56
63
```
57
64
RtmBot
58
65
├── Plugin
@@ -69,20 +76,24 @@ Plugins can live within any python module, but we recommend just putting them in
69
76
70
77
To add a plugin, create a file within your plugin directory (./plugins is a good place for it).
71
78
79
+
```
72
80
mkdir plugins
73
81
touch plugins/__init__.py
74
82
cd plugins
75
83
vi myplugin.py
84
+
```
76
85
77
86
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.
78
87
88
+
```python
79
89
from__future__import print_function
80
90
from rtmbot.core import Plugin
81
91
82
92
classMyPlugin(Plugin):
83
93
84
94
defcatch_all(self, data):
85
95
print(data)
96
+
```
86
97
87
98
You can install as many plugins as you like, and each will handle every event received by the bot independently.
88
99
@@ -92,6 +103,7 @@ Open `plugins/repeat.py`
92
103
93
104
Add the following:
94
105
106
+
```python
95
107
from__future__import print_function
96
108
from__future__import unicode_literals
97
109
@@ -107,23 +119,29 @@ Add the following:
107
119
data['text'], data['channel']
108
120
)]
109
121
)
122
+
```
110
123
111
124
The repeat plugin will now be loaded by the bot on startup. Run `rtmbot` from console to start your RtmBot.
112
125
126
+
```
113
127
rtmbot
128
+
```
114
129
115
130
Create Plugins
116
131
--------
117
132
118
133
#### Incoming data
134
+
119
135
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:
120
136
137
+
```python
121
138
defprocess_message(self, data):
122
139
print data
140
+
```
123
141
124
142
This will print the incoming message json (dict) to the screen where the bot is running.
125
143
126
-
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.
144
+
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.
127
145
128
146
For a list of all possible API Methods, look here: https://api.slack.com/rtm
129
147
@@ -132,27 +150,34 @@ Note: If you're using Python 2.x, the incoming data should be a unicode string,
132
150
#### Outgoing data
133
151
134
152
##### RTM Output
135
-
Plugins can send messages back to any channel or direct message. This is done by appending a two item array to the Plugin's output array (```myPluginInstance.output```). The first item in the array is the channel or DM ID and the second is the message text. Example that writes "hello world" when the plugin is started:
136
153
154
+
Plugins can send messages back to any channel or direct message. This is done by appending a two item array to the Plugin's output array (`myPluginInstance.output`). The first item in the array is the channel or DM ID and the second is the message text. Example that writes "hello world" when the plugin is started:
155
+
156
+
```python
137
157
classmyPlugin(Plugin):
138
158
139
159
defprocess_message(self, data):
140
160
self.outputs.append(["C12345667", "hello world"])
161
+
```
141
162
142
163
##### SlackClient Web API Output
164
+
143
165
Plugins also have access to the connected SlackClient instance for more complex output (or to fetch data you may need).
144
166
167
+
```python
145
168
defprocess_message(self, data):
146
169
self.slack_client.api_call(
147
170
"chat.postMessage", channel="#general", text="Hello from Python! :tada:",
148
171
username="pybot", icon_emoji=":robot_face:"
149
-
172
+
```
150
173
151
174
#### Timed jobs
175
+
152
176
Plugins can also run methods on a schedule. This allows a plugin to poll for updates or perform housekeeping during its lifetime. Jobs define a run() method andreturnany outputs to be sent to channels. They also have access to a SlackClient instance that allows them to make calls to the Slack Web API.
153
177
154
-
For example, this will print "hello world" every 10 seconds. You can output multiple messages to the same or different channels by passing multiple pairs of [Channel, Message] combos.
178
+
For example, this will print"hello world" every 10 seconds. You can output multiple messages to the same or different channels by passing multiple pairs of `[Channel, Message]` combos.
155
179
180
+
```python
156
181
from core import Plugin, Job
157
182
158
183
@@ -167,7 +192,7 @@ For example, this will print "hello world" every 10 seconds. You can output mult
167
192
def register_jobs(self):
168
193
job= myJob(10, debug=True)
169
194
self.jobs.append(job)
170
-
171
-
195
+
```
172
196
#### Plugin misc
197
+
173
198
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