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

Commit 78ca75b

Browse files
author
Bren Briggs
committed
Cleanup README formatting
1 parent 3c3f823 commit 78ca75b

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

README.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,39 @@ Installation
2727

2828
1. Create your project
2929

30+
```
3031
mkdir myproject
3132
cd myproject
33+
```
3234

3335
2. Install rtmbot (ideally into a [virtualenv](https://virtualenv.readthedocs.io/en/latest/))
3436

37+
```
3538
pip install rtmbot
39+
```
3640

3741
3. Create an rtmbot.conf file and [create a bot for your team](https://api.slack.com/bot-users)
3842

43+
```
3944
# Add the following to rtmbot.conf
4045
DEBUG: True # make this False in production
4146
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
4247
ACTIVE_PLUGINS:
4348
- plugins.repeat.RepeatPlugin
49+
```
4450

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.
4652

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)
4854

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.
5056

5157
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.
5258

5359
A Word on Structure
5460
-------
5561
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+
5663
```
5764
RtmBot
5865
├── Plugin
@@ -69,20 +76,24 @@ Plugins can live within any python module, but we recommend just putting them in
6976

7077
To add a plugin, create a file within your plugin directory (./plugins is a good place for it).
7178

79+
```
7280
mkdir plugins
7381
touch plugins/__init__.py
7482
cd plugins
7583
vi myplugin.py
84+
```
7685

7786
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.
7887

88+
```python
7989
from __future__ import print_function
8090
from rtmbot.core import Plugin
8191

8292
class MyPlugin(Plugin):
8393

8494
def catch_all(self, data):
8595
print(data)
96+
```
8697

8798
You can install as many plugins as you like, and each will handle every event received by the bot independently.
8899

@@ -92,6 +103,7 @@ Open `plugins/repeat.py`
92103

93104
Add the following:
94105

106+
```python
95107
from __future__ import print_function
96108
from __future__ import unicode_literals
97109

@@ -107,23 +119,29 @@ Add the following:
107119
data['text'], data['channel']
108120
)]
109121
)
122+
```
110123

111124
The repeat plugin will now be loaded by the bot on startup. Run `rtmbot` from console to start your RtmBot.
112125

126+
```
113127
rtmbot
128+
```
114129

115130
Create Plugins
116131
--------
117132

118133
#### Incoming data
134+
119135
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:
120136

137+
```python
121138
def process_message(self, data):
122139
print data
140+
```
123141

124142
This will print the incoming message json (dict) to the screen where the bot is running.
125143

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.
127145

128146
For a list of all possible API Methods, look here: https://api.slack.com/rtm
129147

@@ -132,27 +150,34 @@ Note: If you're using Python 2.x, the incoming data should be a unicode string,
132150
#### Outgoing data
133151

134152
##### 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:
136153

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
137157
class myPlugin(Plugin):
138158

139159
def process_message(self, data):
140160
self.outputs.append(["C12345667", "hello world"])
161+
```
141162

142163
##### SlackClient Web API Output
164+
143165
Plugins also have access to the connected SlackClient instance for more complex output (or to fetch data you may need).
144166

167+
```python
145168
def process_message(self, data):
146169
self.slack_client.api_call(
147170
"chat.postMessage", channel="#general", text="Hello from Python! :tada:",
148171
username="pybot", icon_emoji=":robot_face:"
149-
172+
```
150173

151174
#### Timed jobs
175+
152176
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 and return any 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.
153177

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.
155179

180+
```python
156181
from core import Plugin, Job
157182

158183

@@ -167,7 +192,7 @@ For example, this will print "hello world" every 10 seconds. You can output mult
167192
def register_jobs(self):
168193
job = myJob(10, debug=True)
169194
self.jobs.append(job)
170-
171-
195+
```
172196
#### Plugin misc
197+
173198
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

Comments
 (0)