API Reference¶
Slackify¶
-
class
slackify.
Slackify
(app: Optional[Union[flask.app.Flask, flask.blueprints.Blueprint]] = None, endpoint: str = '/', events_endpoint: str = '/slack/events', emitter: pyee.BaseEventEmitter = None, dispatcher: slackify.dispatcher.Dispatcher = None, injector: slackify.injection.Injector = None, **kwargs)¶ Slackify
is the orchestrator of the slack dance- It has two main responsabilities:
Registering callbacks for slack requests.
Dispatch incoming requests to the registered callback.
- Parameters
app (Union[Flask, Blueprint]) – Flask instance or Blueprint which will be used to register callbacks
endpoint (str) – Endpoint to expose in slack requests for automatic redirection
events_endpoint (str) – Endpoint to expose as slack events listener
emitter (pyee.BaseEventEmitter) – Class in charge of emitting events
dispatcher (Dispatcher) – Class in charge of adding and dispatching slack requests
injector (Injector) – Class in charge of injecting arguments to callback functions
-
action
(action_id=None, **options)¶ Register a function as an action callback.
It supports registering actions by just action_id and action_id + block_id
Usage:
>>> @slackify.action('action_id') >>> def action_id_callback(): ... return 'Hello' ... >>> @slackify.action(action_id='action_id', block_id='block_id') >>> def other_callback(): ... return 'Bye'
-
command
(func=None, **options)¶ Register a function as a command handler.
It can be used as a plain decorator or as a parametrized decorator factory.
- Usage:
>>> @slackify.command >>> def hola(): ... print('hola')
>>> @slackify.command(name='goodbye') >>> def chau(): ... print('chau')
-
default
(func: Callable[], flask.wrappers.Response])¶ Register function to execute when an unknown command is received
- Usage:
>>> @slackify.default >>> def unknown_command(): ... return 'Unknown Command'
-
error
(func: Callable[[Exception], flask.wrappers.Response])¶ Register function to execute when an exception was raised on any registered handler
- Usage:
>>> @slackify.error >>> def new_handler(exception): ... return f'Something went wrong! {exception!r}'
-
event
(event: str, func=None)¶ Register a function as an event callback.
Note
All event callbacks MUST accept a payload positional argument. The event payload from slack will be sent on that arg
Usage:
>>> @slackify.event('reaction_added') >>> def handle_reaction(payload): ... print(payload) ... return 'Hello'
-
message
(message, func=None, **kwargs)¶ Register a function as a callback for when a string matching message is found.
Note
The event callback MUST accept a payload positional argument. The event payload from slack will be sent on that arg
Usage:
>>> @slackify.message('hello') >>> def handle_reaction(payload): ... return 'How are you?' ... >>> BYE_REGEX = re.compile(r'bye|goodbye|see you|chau') >>> @slackify.message(BYE_REGEX) >>> def say_bye(payload): ... return 'Bye!
-
shortcut
(shortcut_id: str, **options)¶ Register a function as a shortcut callback
- Usage:
>>> @slackify.shortcut('my-shortcut') >>> def hello(): ... print('Someone followed `my-shortcut`')
-
view
(view_callback_id, **options)¶ Register a function as a view callback.
- Usage:
>>> @slackify.view('my_view') >>> def view_callback(): ... return 'Hello'
Slack Helpers¶
-
slackify.slack.
block_reply
(blocks: List) → Tuple[str, int, Dict]¶ Respond to slack with a block payload. See https://api.slack.com/block-kit/building for reference
-
slackify.slack.
reply
(body: dict) → Tuple[str, int, Dict]¶ Helper method that returns a complex response to slack
It may contain a blocks payload. A simple text structure or whatever slack admits as a valid response to certain action. It does nothing fancy. Just transforms dict to json, and passes json as Content-Type as slack requires.
-
slackify.slack.
reply_text
(text: str) → Tuple[str, int, Dict]¶ Helper method that returns a plain text response to slack
-
slackify.slack.
respond
(url: str, message: Dict[str, Any]) → requests.models.Response¶ Respond async to interaction to allow fast acknowledge of interactive message request
You should use this method when responding to actions. See
examples/actions.py
-
slackify.slack.
text_block
(text: str, markdown: bool = True) → Dict[str, Any]¶ Respond to slack with a block including just text with markdown support
Tasks¶
-
slackify.tasks.
async_task
(callable: Callable)¶ Run a Callable in a background thread so it doesn’t block main thread
This decorator might be useful if you want to avoid getting slack timeout because your handlers are taking more than 3 seconds to respond. See examples.async_task for usage details