Skip to content

Commit 4da5efc

Browse files
Shay DeWaelmwbrooksseratch
authored
Revise README (#115)
* Make README revisions` Co-authored-by: Michael Brooks <[email protected]> Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent 3b9e48c commit 4da5efc

File tree

1 file changed

+69
-70
lines changed

1 file changed

+69
-70
lines changed

README.md

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Bolt for Python
1+
# Bolt ![Bolt logo](docs/assets/bolt-logo.svg) for Python (beta)
22

33
[![Python Version][python-version]][pypi-url]
44
[![pypi package][pypi-image]][pypi-url]
55
[![Build Status][travis-image]][travis-url]
66
[![Codecov][codecov-image]][codecov-url]
77

8-
A Python framework to build Slack apps in a flash with the latest platform features. Check the [document](https://slack.dev/bolt-python/) and [examples](https://github.com/slackapi/bolt-python/tree/main/examples) to know how to use this framework.
8+
A Python framework to build Slack apps in a flash with the latest platform features. Read the [getting started guide](https://slack.dev/bolt-python/tutorial/getting-started) and look at our [code examples](https://github.com/slackapi/bolt-python/tree/main/examples) to learn how to build apps using Bolt.
99

1010
## Setup
1111

@@ -18,9 +18,9 @@ pip install -U pip
1818
pip install slack_bolt
1919
```
2020

21-
## First Bolt App (app.py)
21+
## Creating an app
2222

23-
Create an app by calling a constructor, which is a top-level export.
23+
Create a Bolt for Python app by calling a constructor, which is a top-level export. If you'd prefer, you can create an [async app](#creating-an-async-app).
2424

2525
```python
2626
import logging
@@ -32,59 +32,13 @@ from slack_bolt import App
3232
# export SLACK_BOT_TOKEN=xoxb-***
3333
app = App()
3434

35-
# Events API: https://api.slack.com/events-api
36-
@app.event("app_mention")
37-
def event_test(say):
38-
say("What's up?")
39-
40-
# Interactivity: https://api.slack.com/interactivity
41-
@app.shortcut("callback-id-here")
42-
# @app.command("/hello-bolt-python")
43-
def open_modal(ack, client, logger, body):
44-
# acknowledge the incoming request from Slack immediately
45-
ack()
46-
# open a modal
47-
api_response = client.views_open(
48-
trigger_id=body["trigger_id"],
49-
view={
50-
"type": "modal",
51-
"callback_id": "view-id",
52-
"title": {
53-
"type": "plain_text",
54-
"text": "My App",
55-
},
56-
"submit": {
57-
"type": "plain_text",
58-
"text": "Submit",
59-
},
60-
"blocks": [
61-
{
62-
"type": "input",
63-
"block_id": "b",
64-
"element": {
65-
"type": "plain_text_input",
66-
"action_id": "a"
67-
},
68-
"label": {
69-
"type": "plain_text",
70-
"text": "Label",
71-
}
72-
}
73-
]
74-
})
75-
logger.debug(api_response)
76-
77-
@app.view("view-id")
78-
def view_submission(ack, view, logger):
79-
ack()
80-
# Prints {'b': {'a': {'type': 'plain_text_input', 'value': 'Your Input'}}}
81-
logger.info(view["state"]["values"])
35+
# Add functionality here
8236

8337
if __name__ == "__main__":
8438
app.start(3000) # POST http://localhost:3000/slack/events
8539
```
8640

87-
## Run the Bolt App
41+
## Running an app
8842

8943
```bash
9044
export SLACK_SIGNING_SECRET=***
@@ -95,9 +49,61 @@ python app.py
9549
ngrok http 3000
9650
```
9751

98-
## AsyncApp Setup
52+
## Listening for events
53+
Apps typically react to a collection of incoming events, which can correspond to [Events API events](https://api.slack.com/events-api), [actions](https://api.slack.com/interactivity/components), [shortcuts](https://api.slack.com/interactivity/shortcuts), [slash commands](https://api.slack.com/interactivity/slash-commands) or [options requests](https://api.slack.com/reference/block-kit/block-elements#external_select). For each type of
54+
request, there's a method to build a listener function.
55+
56+
```python
57+
# Listen for an event from the Events API
58+
app.event(event_type, fn)
59+
60+
# Convenience method to listen to only `message` events using a string or re.Pattern
61+
app.message([pattern ,] fn)
62+
63+
# Listen for an action from a Block Kit element (buttons, select menus, date pickers, etc)
64+
app.action(action_id, fn)
65+
66+
# Listen for dialog submissions
67+
app.action({"callback_id": callbackId}, fn)
68+
69+
# Listen for a global or message shortcuts
70+
app.shortcut(callback_id, fn)
71+
72+
# Listen for slash commands
73+
app.command(command_name, fn)
74+
75+
# Listen for view_submission modal events
76+
app.view(callback_id, fn)
77+
78+
# Listen for options requests (from select menus with an external data source)
79+
app.options(action_id, fn)
80+
```
81+
82+
The recommended way to use these methods are decorators:
83+
84+
```python
85+
@app.event(event_type)
86+
def handle_event(event):
87+
pass
88+
```
89+
90+
## Making things happen
91+
92+
Most of the app's functionality will be inside listener functions (the `fn` parameters above). These functions are called with a set of arguments.
93+
94+
| Argument | Description |
95+
| :---: | :--- |
96+
| `payload` | Contents of the incoming event. The payload structure depends on the listener. For example, for an Events API event, `payload` will be the [event type structure](https://api.slack.com/events-api#event_type_structure). For a block action, it will be the action from within the `actions` list. The `payload` dictionary is also accessible via the alias corresponding to the listener (`message`, `event`, `action`, `shortcut`, `view`, `command`, or `options`). For example, if you were building a `message()` listener, you could use the `payload` and `message` arguments interchangably. **An easy way to understand what's in a payload is to log it**. |
97+
| `say` | Utility function to send a message to the channel associated with the incoming event. This argument is only available when the listener is triggered for events that contain a `channel_id` (the most common being `message` events). `say` accepts simple strings (for plain-text messages) and dictionaries (for messages containing blocks).
98+
| `ack` | Function that **must** be called to acknowledge that your app received the incoming event. `ack` exists for all actions, shortcuts, view submissions, slash command and options requests. `ack` returns a promise that resolves when complete. Read more in [Acknowledging events](https://slack.dev/bolt-python/concepts#acknowledge).
99+
| `client` | Web API client that uses the token associated with the event. For single-workspace installations, the token is provided to the constructor. For multi-workspace installations, the token is returned by using [the OAuth library](https://slack.dev/bolt-python/concepts#authenticating-oauth), or manually using the `authorize` function.
100+
| `respond` | Utility function that responds to incoming events **if** it contains a `response_url` (shortcuts, actions, and slash commands).
101+
| `context` | Event context. This dictionary contains data about the event and app, such as the `botId`. Middleware can add additional context before the event is passed to listeners.
102+
| `body` | Dictionary that contains the entire body of the request (superset of `payload`). Some accessory data is only available outside of the payload (such as `trigger_id` and `authed_users`).
103+
104+
## Creating an async app
99105

100-
If you prefer building Slack apps using [asyncio](https://docs.python.org/3/library/asyncio.html), you can go with `AsyncApp` instead. You can use async/await style for everything in the app. To use `AsyncApp`, [AIOHTTP](https://docs.aiohttp.org/en/stable/) library is required for asynchronous Slack Web API calls and the default web server.
106+
If you'd prefer to build your app with [asyncio](https://docs.python.org/3/library/asyncio.html), you can import the [AIOHTTP](https://docs.aiohttp.org/en/stable/) library and call the `AsyncApp` constructor. Within async apps, you can use the async/await pattern.
101107

102108
```bash
103109
# Python 3.6+ required
@@ -109,9 +115,10 @@ pip install -U pip
109115
pip install slack_bolt aiohttp
110116
```
111117

112-
Import `slack_bolt.async_app.AsyncApp` instead of `slack_bolt.App`. All middleware/listeners must be async functions. Inside the functions, all utility methods such as `ack`, `say`, and `respond` requires `await` keyword.
118+
In async apps, all middleware/listeners must be async functions. When calling utility methods (like `ack` and `say`) within these functions, it's required to use the `await` keyword.
113119

114120
```python
121+
# Import the async app instead of the regular one
115122
from slack_bolt.async_app import AsyncApp
116123

117124
app = AsyncApp()
@@ -130,29 +137,21 @@ if __name__ == "__main__":
130137
app.start(3000)
131138
```
132139

133-
Starting the app is exactly the same with the way using `slack_bolt.App`.
134-
135-
```bash
136-
export SLACK_SIGNING_SECRET=***
137-
export SLACK_BOT_TOKEN=xoxb-***
138-
python app.py
139-
140-
# in another terminal
141-
ngrok http 3000
142-
```
143-
144140
If you want to use another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at the built-in adapters and their examples.
145141

146142
* [The Bolt app examples](https://github.com/slackapi/bolt-python/tree/main/examples)
147143
* [The built-in adapters](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter)
144+
Apps can be run the same way as the syncronous example above. If you'd prefer another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at [the built-in adapters](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter) and their corresponding [sample code](https://github.com/slackapi/bolt-python/tree/main/samples).
145+
146+
## Getting Help
148147

149-
# Feedback
148+
[The documentation](https://slack.dev/bolt-python) has more information on basic and advanced concepts for Bolt for Python.
150149

151-
We are keen to hear your feedback. Please feel free to [submit an issue](https://github.com/slackapi/bolt-python/issues)!
150+
If you otherwise get stuck, we're here to help. The following are the best ways to get assistance working through your issue:
152151

153-
# License
152+
* [Issue Tracker](http://github.com/slackapi/bolt-python/issues) for questions, bug reports, feature requests, and general discussion related to Bolt for Python. Try searching for an existing issue before creating a new one.
153+
* [Email](mailto:[email protected]) our developer support team: `[email protected]`
154154

155-
The MIT License
156155

157156
[pypi-image]: https://badge.fury.io/py/slack-bolt.svg
158157
[pypi-url]: https://pypi.org/project/slack-bolt/

0 commit comments

Comments
 (0)