Framework API

class lazbot.Lazbot(token)

Root class for bot creation and interaction

Create your bot from this (or extend it if you want to add something) class. All you need to pass in is the Slack token to use for communicating with the Slack servers.

Parameters:token – Slack API token to operate under

Usage:

from lazbot import Lazbot

bot = Lazbot("my-slack-token")
connect()

Connect this Slack bot to the Slack servers

This will initialize the client attribute to handle regular Slack API calls and if the stream attribute is True (it is by default) a websocket connection will be opened and begin listening.

Usage:

bot.stream = False

login_data = bot.connect()
get_channel(channel_id)

Helper function to lookup rich Channel object

Slack often provides an unuseful channel id for all users, if the channels lookup dictionary is populated, this will return the rich <Channel> object for the provided channel_id or None if there is no match.

Parameters:channel_id – slack’s channel id to be looked up
get_user(user_id)

Helper function to lookup rich User object

Slack often provides an unuseful user id for all users, if the users lookup dictionary is populated, this will return the rich <User> object for the provided user_id or None if there is no match.

Parameters:user_id – slack’s user id to be looked up
ignore(*channels)

Channel blacklisting

Takes a number of channel names and adds them to the blacklist so that all subsequent events on that channel do not trigger hooks.

listen(filter, channel=None, regex=False)

Register a message event listener

(decorator) Will register the decorated function to be called for all Message events that match the filter settings. The filter should be a string that will either be a literal match (default), a * to match any string, or a custom regex string (with the regex flag to True), see <regex logic> for more information on the options for the string. The filter will be checked for all message events that happen and if the logic passes, the data will be passed to the decorated function.

For more information see #Filter#

Parameters:
  • filter – message format to match against the message text
  • channel – list of channel names that this filter should check against
  • regex – boolean flag on whether the filter text should be treated as a regex string

Usage:

@bot.listen("@me: hi")
def greetings(user, channel):
    bot.post(channel, text="{!s}, greetings".format(user))
on(*events, **kwargs)

Register a generic event listener

(decorator) Will register the decorated function as a hook for the specified event(s). When the event is triggered, the handler will get a keyworded representation of the event (see the targetted events for what these could be).

Parameters:events – event types as specified in ##events##

Usage:

typing_count = {}

@bot.on(events.USER_TYPING)
def count_typing(user):
    if user not in typing_count:
        typing_count[user] = 0

    typing_count[user] += 1
post(channel, translate=True, **kwargs)

Post message to a channel

schedule(function=None, when=None, after=None, recurring=False, name=None)

Register a scheduled task

(decorator) Will register the decorated function (or can register a function normally) to be run at a specific time, after a specific time has elapsed, or a mixture if set to recurring.

Parameters:
  • function – function to be called when task is activated, if not set, this will return a decorator
  • when – (optional) date, time, or datetime object for the time the task will get run at (if a date, the time will be midnight, if a time it will be the next occurance of that time)
  • after – (optional) timedelta for the amount of time until the task will be run from now
  • recurring – if set to True, the task will be run again after it is completed, the after param is required for this
  • name – (optional) a name to provide to the task, useful for any attempts at resolving duplicate creation

Usage:

from datetime import timedelta, time

@bot.schedule(when=time(12, 0, 0), after=timedelta(hours=24),
              recurring=True)
def lunchtime():
    lunch_channel = bot.lookup_channel("#lunch-reminder")
    bot.post(lunch_channel, text="Lunch time")
setup(function=None, priority=False)

Register a setup hook

(decorator) Will register the decorated funtion to be called once the bot has connected to the Slack servers.

Parameters:priority – whether the function is a high priority setup (it gets called with other high priority setup functions)

Usage:

@bot.setup
def greetings():
    bot_channel = bot.lookup_channel("#bot-channel")
    bot.post(bot_channel, text="Hey guys")
start()

Start up the bot process

Calls the connect method and then (if stream is set) begins the event loop, reading events off of the socket, running scheduled tasks, and keeping the connection alive.

stop()

Stop the bot process

Closes the socket and turns listeners off

translate(channel='*', function=None)

Register a translation function

(decorator) Will register the decorated function in the series of translation functions that get called on message posts. These functions are meant for modifying messages in specific channels (or all channels).

Parameters:channel – single or list of channel names to apply this translator to

Usage:

@bot.translate(channel="#bot_test")
def self_reference(text):
    return text.replace(bot.user, "me")

https://secure.travis-ci.org/jdost/lazbot.png?branch=master

Navigation