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

Commit

Permalink
Cleanup README formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Bren Briggs committed Aug 17, 2017
1 parent 3c3f823 commit 78ca75b
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,39 @@ Installation

1. Create your project

```
mkdir myproject
cd myproject
```

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

```
pip install rtmbot
```

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

```
# Add the following to rtmbot.conf
DEBUG: True # make this False in production
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
ACTIVE_PLUGINS:
- plugins.repeat.RepeatPlugin
```

```DEBUG``` will adjust logging verbosity and cause the runner to exit on exceptions, generally making debugging more pleasant.
`DEBUG` will adjust logging verbosity and cause the runner to exit on exceptions, generally making debugging more pleasant.

```SLACK_TOKEN``` is needed to [authenticate with your Slack team.](https://api.slack.com/web#authentication)
`SLACK_TOKEN` is needed to [authenticate with your Slack team.](https://api.slack.com/web#authentication)

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

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.

A Word on Structure
-------
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.

```
RtmBot
├── Plugin
Expand All @@ -69,20 +76,24 @@ Plugins can live within any python module, but we recommend just putting them in

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

```
mkdir plugins
touch plugins/__init__.py
cd plugins
vi myplugin.py
```

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.

```python
from __future__ import print_function
from rtmbot.core import Plugin

class MyPlugin(Plugin):

def catch_all(self, data):
print(data)
```

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

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

Add the following:

```python
from __future__ import print_function
from __future__ import unicode_literals

Expand All @@ -107,23 +119,29 @@ Add the following:
data['text'], data['channel']
)]
)
```

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

```
rtmbot
```

Create Plugins
--------

#### Incoming data

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:

```python
def process_message(self, data):
print data
```

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

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

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

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

##### RTM Output
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:

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:

```python
class myPlugin(Plugin):

def process_message(self, data):
self.outputs.append(["C12345667", "hello world"])
```

##### SlackClient Web API Output

Plugins also have access to the connected SlackClient instance for more complex output (or to fetch data you may need).

```python
def process_message(self, data):
self.slack_client.api_call(
"chat.postMessage", channel="#general", text="Hello from Python! :tada:",
username="pybot", icon_emoji=":robot_face:"

```

#### Timed jobs

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.

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

```python
from core import Plugin, Job


Expand All @@ -167,7 +192,7 @@ For example, this will print "hello world" every 10 seconds. You can output mult
def register_jobs(self):
job = myJob(10, debug=True)
self.jobs.append(job)


```
#### 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.

0 comments on commit 78ca75b

Please sign in to comment.