Code Monkey home page Code Monkey logo

python-rtmbot's Introduction

python-rtmbot

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 and Python. The configuration file format is YAML.

Some differences to webhooks:

  1. Doesn't require a webserver to receive messages
  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

Dependencies

Installation

  1. Download the python-rtmbot code

     git clone [email protected]:slackhq/python-rtmbot.git
     cd python-rtmbot
    
  2. Install dependencies (virtualenv is recommended.)

     pip install -r requirements.txt
    
  3. Configure rtmbot (https://api.slack.com/bot-users)

     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.

Add Plugins

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.

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.

./rtmbot.py

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:

def process_message(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(data) will receive ALL events from the websocket. This is useful for learning the names of events and debugging.

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

outputs = []
outputs.append(["C12345667", "hello world"])

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.

outputs = []
crontable = []
crontable.append([10, "say_hello"])
def say_hello():
    outputs.append(["C12345667", "hello world"])

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

####Todo: Some rtm data should be handled upstream, such as channel and user creation. These should create the proper objects on-the-fly.

python-rtmbot's People

Contributors

rawdigits avatar jiujitsu avatar mattskone avatar

Watchers

Mitch Garnaat avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.