ubot2/README.md

94 lines
4.1 KiB
Markdown
Raw Normal View History

2014-11-18 09:40:49 -08:00
python-rtmbot
=============
2016-04-15 12:21:54 -07:00
[![Build Status](https://travis-ci.org/slackhq/python-rtmbot.png)](https://travis-ci.org/slackhq/python-rtmbot)
[![Coverage Status](https://coveralls.io/repos/github/slackhq/python-rtmbot/badge.svg?branch=master)](https://coveralls.io/github/slackhq/python-rtmbot?branch=master)
2016-04-15 12:21:54 -07:00
2014-11-18 09:40:49 -08:00
A Slack bot written in python that connects via the RTM API.
Python-rtmbot is a callback based bot engine. The plugins architecture should be familiar to anyone with knowledge to the [Slack API](https://api.slack.com) and Python. The configuration file format is YAML.
2014-11-18 10:18:26 -08:00
Some differences to webhooks:
2014-11-18 10:21:40 -08:00
2014-11-18 10:28:15 -08:00
1. Doesn't require a webserver to receive messages
2014-12-03 12:29:28 -08:00
2. Can respond to direct messages from users
3. Logs in as a slack user (or bot)
4. Bot users must be invited to a channel
2014-11-18 10:18:26 -08:00
2014-11-18 09:40:49 -08:00
Dependencies
----------
* websocket-client https://pypi.python.org/pypi/websocket-client/
* python-slackclient https://github.com/slackhq/python-slackclient
Installation
-----------
2014-12-08 16:01:33 -08:00
1. Download the python-rtmbot code
2014-11-18 09:40:49 -08:00
git clone git@github.com:slackhq/python-rtmbot.git
cd python-rtmbot
2014-12-08 16:01:33 -08:00
2. Install dependencies ([virtualenv](http://virtualenv.readthedocs.org/en/latest/) is recommended.)
pip install -r requirements.txt
2014-11-18 09:40:49 -08:00
2014-12-09 11:55:20 -08:00
3. Configure rtmbot (https://api.slack.com/bot-users)
2014-11-18 09:40:49 -08:00
cp doc/example-config/rtmbot.conf .
vi rtmbot.conf
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
*Note*: At this point rtmbot is ready to run, however no plugins are configured.
2014-11-18 10:11:25 -08:00
Add Plugins
2014-11-18 09:40:49 -08:00
-------
2014-11-21 17:29:54 -08:00
Plugins can be installed as .py files in the ```plugins/``` directory OR as a .py file in any first level subdirectory. If your plugin uses multiple source files and libraries, it is recommended that you create a directory. You can install as many plugins as you like, and each will handle every event received by the bot indepentently.
2014-11-18 09:40:49 -08:00
To install the example 'repeat' plugin
mkdir plugins/repeat
cp doc/example-plugins/repeat.py plugins/repeat
The repeat plugin will now be loaded by the bot on startup.
2016-04-26 17:20:05 +03:00
./rtmbot.py
2014-11-18 10:11:25 -08:00
Create Plugins
--------
####Incoming data
Plugins are callback based and respond to any event sent via the rtm websocket. To act on an event, create a function definition called process_(api_method) that accepts a single arg. For example, to handle incoming messages:
2014-11-18 10:14:05 -08:00
def process_message(data):
print data
2014-11-18 10:11:25 -08:00
This will print the incoming message json (dict) to the screen where the bot is running.
2014-11-18 16:30:04 -08:00
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.
Note: If you're using Python 2.x, the incoming data should be a unicode string, be careful you don't coerce it into a normal str object as it will cause errors on output. You can add `from __future__ import unicode_literals` to your plugin file to avoid this.
2014-11-18 10:11:25 -08:00
####Outgoing data
Plugins can send messages back to any channel, including direct messages. This is done by appending a two item array to the outputs global array. The first item in the array is the channel ID and the second is the message text. Example that writes "hello world" when the plugin is started:
2014-11-18 10:14:05 -08:00
outputs = []
outputs.append(["C12345667", "hello world"])
2014-11-18 10:11:25 -08:00
*Note*: you should always create the outputs array at the start of your program, i.e. ```outputs = []```
####Timed jobs
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.
2014-11-18 10:14:05 -08:00
outputs = []
crontable = []
crontable.append([10, "say_hello"])
def say_hello():
outputs.append(["C12345667", "hello world"])
2014-11-18 10:11:25 -08:00
####Plugin misc
2014-11-18 11:30:27 -08:00
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.
2014-11-18 10:18:26 -08:00
####Todo:
Some rtm data should be handled upstream, such as channel and user creation. These should create the proper objects on-the-fly.