Flask Slack Events
Slack event subscriptions for Flask
Installation
Install last stable version from Pypi:
pip install flask-slack-events
Create a Slack bot user
See the Slack's documentation for further guidance on creating your bot (step 1).
Within the Basic Information about your application, copy the Signing Secret necessary to verify requests from Slack.
Configure your Application
You should create a SlackManager
object within your application:
slack_manager = SlackManager()
Configure your application object updating the SLACK_SIGNING_SECRET
key with the value obtained in the previous step 1:
app.config['SLACK_SIGNING_SECRET'] = '<your Signing Secret>'
Once the actual application object has been created, you can configure it for SlackManager object with:
slack_manager.init_app(app)
Configure your Slack Bot
Continue with the Slacks's documentation to setting up the Events API (step 2) and enter the URL to receive the subscriptions joining your host and the relative path /slack/events
:
Finally, install your bot to a workspace (step 3).
How it Works
Now in order to subscribe to Slack Events, use the SlackManager.on
decorator:
# Reply to only the message events that mention your bot
@slack_manager.on('app_mention')
def reply_to_app_mention(sender, data, **extra):
event = data['event']
slack_client.api_call(
'chat.postMessage',
channel=event['channel'],
text=f":robot_face: Hello <@{event['user']}>!")
Context Processors
To inject new variables automatically into the context of a handler, context processors exist in Flask-Slack-Events.
A context processor is a function that returns a dictionary:
@slack_manager.context_processor
def context_processor(data):
return dict(my_bot_id='UAZ02BCBH')
The injected variables will be sent as an extra
argument for each event handler f(sender, data, **extra)
.
Dispatch Events Asynchronously
Some event handlers can delay the execution of another, to avoid this you can configure the event dispatcher and call handlers asynchronously:
@slack_manager.dispatch_event_handler
def async_event_dispatcher(sender, data, handlers, **extra):
for handler in handlers:
task(handler)(data, **extra)
Subscribe to Signals
The following signals are sended internally by Flask-Slack-Events:
signals.request_unauthorized
Sent when the request received is unauthorized
Receiver:
f(sender, **extra)
signals.expired_event
Sent when the event has expired according to the value of
SLACK_EVENT_EXPIRATION_DELTA
and the HTTP headerX-Slack-Request-Timestamp
receivedReceiver:
f(sender, **extra)
signals.invalid_signature
Sent when the signature included within the HTTP header
X-Slack-Signature
is invalidReceiver:
f(sender, **extra)
signals.event_received
Sent when an event has been received
Receiver:
f(sender, data, **extra)
SlackManager Handlers
The following handlers are used internally by Flask-Slack-Events:
SlackManager.unauthorized_handler
Called to handle an unauthorized request
Handler:
f()
Default:
SlackManager.unauthorized()
SlackManager.expired_event_handler
Called to handle an expired event
Handler:
f()
Default:
SlackManager.expired_event()
SlackManager.invalid_signature_handler
Called to handle a request with an invalid signature
Handler:
f()
Default:
SlackManager.invalid_signature()
SlackManager.dispatch_event_handler
Called to dispatch the event to all handlers connected with
SlackManager.on(event_type)
decoratorHandler:
f(sender, data, handlers, **extra)
Default:
SlackManager.dispatch_event(data)
Configuration
The following configuration values are used internally by Flask-Slack-Events:
SLACK_SIGNING_SECRET
Signing Secret to verify whether requests from Slack are authentic
Default:
''
SLACK_EVENTS_URL
URL rule that is used to register the Subscription View
Default:
/slack/events
SLACK_EVENT_EXPIRATION_DELTA
Timedelta added to
time.time()
to set the expiration time of each event If the value isNone
then the event never expiresDefault:
timedelta(seconds=60 * 5)
(5 minutes)
Marvin the Paranoid Android
Marvin is a Slack Bot layout for Flask to develop Slack Event handlers and deploy on AWS Lambda + API Gateway