Skip to content

Instantly share code, notes, and snippets.

@jcarbaugh
Created January 19, 2015 19:09
Show Gist options
  • Save jcarbaugh/2f9e07085af514dd3801 to your computer and use it in GitHub Desktop.
Save jcarbaugh/2f9e07085af514dd3801 to your computer and use it in GitHub Desktop.
An example Slack bot using sunlightlabs/butterfield

This is an example of using butterfield to create a Slack bot. The bot's behavior is defined in the message handlers found in examplebot.py. botconfig.json defines the two bots that will be created and the plugins and daemons added to each. We run the bots using the butterfield CLI:

$ butterfield botconfig.json

or

$ python -m "butterfield.cli" botconfig.json
[
{
"key": "not-a-real-key",
"plugins": [
"examplebot.announce_new_channel",
"examplebot.startup_message",
"examplebot.ramen"
]
},
{
"key": "also-not-a-real-key",
"plugins": [
"examplebot.startup_message"
],
"daemons": [
"butterfield.handlers.devel.big_ben"
]
}
]
import asyncio
import json
import socket
import butterfield
@asyncio.coroutine
def ramen(bot, message: "message"):
channel = bot.get_channel('#gastronomy')
if message['channel'] == channel['id']:
if 'ramen' in message['text'].lower().split():
yield from bot.post(channel['id'], ':ramen:')
@asyncio.coroutine
def announce_new_channel(bot, message: "channel_created"):
name = message['channel']['name']
id = message['channel']['id']
text = 'A new channel was just created. Check out <#{}|{}>.'.format(id, name)
yield from bot.post("#general", text)
@asyncio.coroutine
def startup_message(bot, message: "hello"):
me = bot.environment['self']
host = socket.gethostname()
yield from bot.post("#butterfield", "{}:{} started on {}".format(me['name'], bot.uuid, host))
@asyncio.coroutine
def console_logger(bot, message: "*"):
print(json.dumps(message, sort_keys=True, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment