API Reference

Layabout

class layabout.Layabout

An event handler on top of the Slack RTM API.

Example

from layabout import Layabout

app = Layabout()


@app.handle('message')
def echo(slack, event):
    """ Echo all messages seen by the app. """
    channel = event['channel']
    message = event['text']
    subtype = event.get('subtype')

    # Avoid an infinite loop of echoing our own messages.
    if subtype != 'bot_message':
        slack.rtm_send_message(channel, message)

app.run()
handle(type, *, kwargs=None)

Register an event handler with the Layabout instance.

Parameters
  • type (str) – The name of a Slack RTM API event to be handled. As a special case, although it is not a proper RTM event, * may be provided to handle all events. For more information about available events see the Slack RTM API.

  • kwargs (Optional[dict]) – Optional arbitrary keyword arguments passed to the event handler when the event is triggered.

Return type

Callable

Returns

A decorator that validates and registers a Layabout event handler.

Raises

TypeError – If the decorated Callable’s signature does not accept at least 2 parameters.

run(*, connector=None, interval=0.5, retries=16, backoff=None, until=None)

Connect to the Slack API and run the event handler loop.

Parameters
  • connector (Union[EnvVar, Token, SlackClient, None]) – A means of connecting to the Slack API. This can be an API Token, an EnvVar from which a token can be retrieved, or an established SlackClient instance. If absent an attempt will be made to use the LAYABOUT_TOKEN environment variable.

  • interval (float) – The number of seconds to wait between fetching events from the Slack API.

  • retries (int) – The number of retry attempts to make if a connection to Slack is not established or is lost.

  • backoff (Optional[Callable[[int], float]]) – The strategy used to determine how long to wait between retries. Must take as input the number of the current retry and output a float. The retry count begins at 1 and continues up to retries. If absent a truncated exponential backoff strategy will be used.

  • until (Optional[Callable[[List[dict]], bool]]) – The condition used to evaluate whether this method terminates. Must take as input a list of dict representing Slack RTM API events and return a bool. If absent this method will run forever.

Raises
Return type

None

Connectors

In addition to a slackclient.SlackClient these classes may be passed as a connector to Layabout.run().

class layabout.EnvVar

Bases: str

A subclass for differentiating env var names from strings.

class layabout.Token

Bases: str

A subclass for differentiating Slack API tokens from strings.

Exceptions

exception layabout.LayaboutError

Bases: Exception

Base error for all Layabout exceptions.

exception layabout.MissingToken

Bases: layabout.LayaboutError

Raised if a Slack API token could not be found.

exception layabout.FailedConnection

Bases: layabout.LayaboutError, ConnectionError

Raised if the Slack client could not connect to the Slack API.

Inherits from both LayaboutError and the built-in ConnectionError for convenience in exception handling.