Toolkit for creating Slack apps
Project description
Python3 Toolkit for building Slack apps
The slackapptk
package is used to facilitate the development of Slack
applications using Python3 and your choice of web frameworks. As a developer
of Slack apps in Python, I want a library that would allow me to accomplish two
primary goals:
-
Provide a mechanism to route messages received from api.slack.com to the proper code in my app. This process is sometimes referred to callback routing.
-
Create /slash-commands with a rich set of feature capabiltiies as found using the Python standard argparse package.
NOTE: The slapapptk package uses the Slack provided slackclient
package and is not meant to be a replacement. That said, there are widgets defined
in slackapptk.web
that are not yet available in slackclient; and as slackclient is updated
these widgets will be depreciated
NOTE: For SlackClient <= 2.5
If you are looking to develop on the Slack Client 2.5.0 release, please use the branch v1
.
TLDR; See working example
The documentation for using slackapptk
is currently under development. That
said you can find a complete working example that uses the Flask web-framework
in example-slackapp; see that
README.md more details.
Overview
A Slack app can receive messages from api.slack.com for a number of reasons and depending on the way the app was configured in the api.slack.com portal.
For the purpose of this README
the term entrypoint will be used:
- User entered a /slash-command
- User initiaties some kind of interactivity request, for example clicking a button
- User actuates a dropdown and the app has to fetch data from an external source
- User selected an action attached to a Slack message
- App receives a Slack Event as a result of its configured subscriptions
The term request will refer to the message recieved from api.slack.com as a result of any of these entrpoints.
When the app is configured in the api.slack.com portal, the developer will identify the specific app API routes to invoke. The developer would then need to write the prerequist code handlers using their API framework of choice, Flask for example.
To create a an app instance:
from slackapptk.app import SlackApp
app = SlackApp()
app.config.token = "<My app bot token value>"
app.config.signing_secret = "<My app signing secret"
You can bind your application specific code to handle inbound api.slack.com messages using the following:
-
app.ic.<interactive-component>.on
- where is one of the followingblock_actions
- to bind callbacks for block action requestselect
- to bind callbacks for external menu select populationview
- to bind callbacks for View submissionview_closed
- to bind callbacks for View closedimsg
- to bind callbacks to interactive message attachaments (outmoded)dialog
- to bind callbacks to Dialog submit (outmoded)
-
app.commands.register
- to register your /slash-command parser and callback handler -
app.events
- to register all for any Event subscriptions
To process inbound messages from api.slack.com you will need to call one of the app
handlers from the context of the API route handler. For example, if you defined
an API route handler for one of your /slash-commands, you would call
app.handler_slash_command()
to process ihe message.
Example
A primary motivator for slackapptk is to receive the request and direct it to the app entrypoint handler that is specific to the request payload. Consider an example where the app presents the User with a surface that contains a button, and the User clicks on that button. The app code creates the button in a block, and then binds that block ID to the app "interactive component" handler for block IDs, as shown in this code snippet:
from slack.web.classes.blocks import SectionBlock
from slack.web.classes.elements import ButtonElement
from slackapptk.app import SlackApp
from slackapptk.request.interactive import BlockActionRequest, ActionEvent
from slackapptk.response import Response
def demo(app: SlackApp):
# this function uses the provided app to send a message to the User so that
# they can click a button. Note this is an incomplete example, but used to
# show the machanics of the of the entrypoint bind / callback mechanism.
block = SectionBlock(
block_id='this is the block id',
text='Click the button and see what happens',
accessory=ButtonElement(
text='Click Me',
action_id='button action id',
value='10'
)
)
# register the block-id callback to the function for handling when the User
# clicks the button.
app.ic.block_actions.on(block.block_id, on_block_button)
# ... assume some code that sends the above block in a Slack
# surface like a message or modal view ...
def on_block_button(
rqst: BlockActionRequest,
action: ActionEvent
):
# this function is called when the app receives the request message from
# api.slack.com when the User clicked the button.
# send a message back to the User indicating the value of the button
resp = Response(rqst)
resp.send("button pressed, block vlaue is: %s", action.value)
The registration / callback mechanism could also be coded as a decorator, for example:
@app.ic.block_actions.on(block.block_id)
def on_block_button(
rqst: BlockActionRequest,
action: ActionEvent
):
# this function is called when the app receives the request message from
# api.slack.com when the User clicked the button.
# send a message back to the User indicating the value of the button
resp = Response(rqst)
resp.send("button pressed, block vlaue is: %s", action.value)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file slackapptk-2.0.3.tar.gz
.
File metadata
- Download URL: slackapptk-2.0.3.tar.gz
- Upload date:
- Size: 33.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71688838e5296267b84c8e5f353e60532d6071439e58a6e0a76dc8077b9e34e6 |
|
MD5 | 435b0b2fb2029fdc7f3f4b998b945e2a |
|
BLAKE2b-256 | ff1d028eb1d298254815875945f1d7c2bd5a0075693cc3e46eaea37af3fdbf13 |
File details
Details for the file slackapptk-2.0.3-py3-none-any.whl
.
File metadata
- Download URL: slackapptk-2.0.3-py3-none-any.whl
- Upload date:
- Size: 53.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 758382ba2fe179f1b431f3603c7f8a8762d638e5b77a6c04ef5d2bd61e9c5aac |
|
MD5 | 42de4ab5139496dee3ebf0157af9f625 |
|
BLAKE2b-256 | fd883f768f53cf8143772a5a815a33a47c6a3e358bb75fa41669dde692ff11d4 |