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
Layaboutinstance.- 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
- 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 APIToken, anEnvVarfrom which a token can be retrieved, or an establishedSlackClientinstance. If absent an attempt will be made to use theLAYABOUT_TOKENenvironment 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 afloat. The retry count begins at 1 and continues up toretries. 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 alistofdictrepresenting Slack RTM API events and return abool. If absent this method will run forever.
- Raises
TypeError – If an unsupported connector is given.
MissingToken – If no API token is available.
FailedConnection – If connecting to the Slack API fails.
- Return type
None
-
Connectors¶
In addition to a slackclient.SlackClient these classes may be
passed as a connector to Layabout.run().
Exceptions¶
-
exception
layabout.MissingToken¶ Bases:
layabout.LayaboutErrorRaised if a Slack API token could not be found.
-
exception
layabout.FailedConnection¶ Bases:
layabout.LayaboutError,ConnectionErrorRaised if the Slack client could not connect to the Slack API.
Inherits from both
LayaboutErrorand the built-inConnectionErrorfor convenience in exception handling.