You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Slackify` is a light framework designed to accelerate your development of slack apps by letting you focus on **what you want** instead of fighting with *how to do it*
6
7
@@ -78,6 +79,21 @@ Yes! See [examples/actions.py](examples/actions.py) for a quickstart.
78
79
### And slack events?
79
80
As you may have guessed, they are also supported. See [examples/events.py](examples/events.py) for an example.
80
81
82
+
## Dependency Injection
83
+
As you add more and more commands you will find yourself repeating yourself while parsing slack request on every function
84
+
85
+
The lib offers a solution to this with dependency injection.
86
+
```python
87
+
@slackify.command
88
+
defhello(command, command_args, response_url):
89
+
return reply_text(f"You called `{command}{command_args}`. Use {response_url} for delayed responses")
90
+
```
91
+
Your view function will now get the request command, arguments and response_url for free! Pretty cool, right?
92
+
93
+
If you are a user of pytest, this idea is similar to pytest fixtures
94
+
95
+
See [examples/injection.py](examples/injection.py) for the full example
96
+
81
97
82
98
## Full example
83
99
Here you have a more complete example showcasing all functionality. It includes:
@@ -143,20 +159,18 @@ def hello():
143
159
144
160
145
161
@slackify.action("yes")
146
-
defyes():
162
+
defyes(payload):
147
163
"""If a user clicks yes on the message above, execute this callback"""
148
-
action = json.loads(request.form["payload"])
149
164
text_blok = text_block('Super! I do too :thumbsup:')
from slackify import Slackify, reply_text, Blueprint
282
293
283
294
bp = Blueprint('slackify_bp', __name__, url_prefix='/slack')
284
295
slackify = Slackify(app=bp)
@@ -299,6 +310,7 @@ def create_app():
299
310
return app
300
311
301
312
```
313
+
> Note: You must import Blueprint from slackify instead of flask to get it working
302
314
303
315
## API Reference
304
316
```python
@@ -333,7 +345,7 @@ or
333
345
@slackify.default
334
346
335
347
# Handle unexpected errors that occur inside handlers.
336
-
# By default returns status 500 and a generic message.
348
+
# By default returns status 500 and a generic message.
337
349
# The exception will be passed as a positional argument to the decorated function
338
350
@slackify.error
339
351
```
@@ -349,11 +361,11 @@ The lib exposes a main class called `Slackify` that can either receive a Flask i
349
361
as `app` argument or creates one on the fly.
350
362
It then binds two routes. One for commands, shortcuts, actions and another one for slack events.
351
363
352
-
The first route is `/` by default, it inspects the incoming requests and looks for any declared handler that is interested in handling this request to redirect it.
364
+
The first route is `/` by default, it inspects the incoming requests and looks for any declared handler that is interested in handling this request to redirect it.
353
365
354
-
If it finds a handler, it redirects the request to that function by overriding its `Request.url_rule.endpoint`
366
+
If it finds a handler, it injects any dependency the view may require as a view argument, and then call the view function, passing the return value as the request response.
355
367
356
-
If there's no match, it ignores the request and it follows the
368
+
If there's no match, it ignores the request and it follows the
357
369
normal request lifecycle.
358
370
359
371
If there's an error, an overridable function through `@slackify.error` is executed to show a friendly message.
@@ -363,9 +375,6 @@ The second route the lib adds is the events route at `/slack/events`.
363
375
When it receives a post request, it emits an event through `pyee.ExecutorEventEmitter` with the event type and quickly responds with the response acknowledgment slack requires to avoid showing an error to the user. This allows asynchronous execution of the function, while still responding quickly to slack.
364
376
In other words, when you decorate a function with `app.event('event_type')` what you are really doing is setting up a listener for the `event_type` that will receive the event payload. No magic.
365
377
366
-
If after reading this you have an idea of how we can extend or improve this lib in any way, i would be really grateful to receive an issue or pull request!
378
+
If after reading this you have an idea of how we can extend or improve this lib in any way, it would be great to receive an issue or pull request!
367
379
I feel there's still a void on slack bots with python that java and javascript have covered with [bolt's](https://github.com/slackapi/bolt) beautiful API.
368
380
Below you can find the current roadmap of features i would like to include.
369
-
370
-
## Roadmap
371
-
1. Inject payload argument to slack event handlers to avoid code repetition on loading request data.
0 commit comments