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 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()
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 |
---|
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 |
---|
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.
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: |
|
---|
Usage:
@bot.listen("@me: hi")
def greetings(user, channel):
bot.post(channel, text="{!s}, greetings".format(user))
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 message to a channel
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: |
|
---|
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")
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 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 the bot process
Closes the socket and turns listeners off
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")