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
Copy file name to clipboardExpand all lines: docs/english/tutorial/order-confirmation/order-confirmation.md
+71-20Lines changed: 71 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,15 +27,18 @@ If you don't already have the Slack CLI, install it from your terminal: navigate
27
27
28
28
### Cloning the starter app
29
29
30
-
Once installed, use the command `slack create`in your terminal, select the `bolt-python-starter-template`, then choose your preferred language (this tutorial shows Python). Alternatively, you can clone the [Bolt for Python template](https://github.com/slack-samples/bolt-python-starter-template)using git.
30
+
Once installed, use the command `slack create`to get started with the Bolt for Python [starter template](https://github.com/slack-samples/bolt-python-starter-template). Alternatively, you can clone the template using Git.
31
31
32
32
You can remove the portions from the template that are not used within this tutorial to make things a bit cleaner for yourself. To do this, open your project in VS Code (you can do this from the terminal with the `code .` command) and delete the `commands`, `events`, and `shortcuts` folders from the `/listeners` folder. You can also do the same to the corresponding folders within the `/listeners/tests` folder as well. Finally, remove the imports of these files from the `/listeners/__init__.py` file.
33
33
34
34
## Creating your app
35
35
36
36
We’ll use the contents of the `manifest.json` file below. This file describes the metadata associated with your app, like its name and permissions that it requests.
37
37
38
-
Copy the contents of the file and [create a new app](https://api.slack.com/apps/new). Next, choose **From a manifest** and follow the prompts, pasting the manifest file contents you copied.
38
+
These values are used to create an app in one of two ways:
39
+
40
+
-**With the Slack CLI**: Save the contents of the file to your project's `manifest.json` file then skip ahead to [starting your app](#starting-your-app).
41
+
-**With app settings**: Copy the contents of the file and [create a new app](https://api.slack.com/apps/new). Next, choose **From a manifest** and follow the prompts, pasting the manifest file contents you copied.
39
42
40
43
```json
41
44
{
@@ -84,7 +87,7 @@ Once your app has been created, scroll down to **App-Level Tokens** on the **Bas
84
87
85
88
Still in the app settings, navigate to the **Install App** page in the left sidebar. Install your app. When you press **Allow**, this means you’re agreeing to install your app with the permissions that it’s requesting. Copy the bot token that you receive as well and store this in a safe place as well for subsequent steps.
86
89
87
-
## Starting your app's server
90
+
## Saving credentials
88
91
89
92
Within a terminal of your choice, set the two tokens from the previous step as environment variables using the commands below. Make sure not to mix these two up, `SLACK_APP_TOKEN` will start with “xapp-“ and `SLACK_BOT_TOKEN` will start with “xoxb-“.
Run the following commands to activate a virtual environment for your Python packages to be installed, install the dependencies, and start your app.
113
118
114
119
```bash
115
120
# Setup your python virtual environment
116
-
python3 -m venv .venv
121
+
python -m venv .venv
117
122
source .venv/bin/activate
118
123
119
124
# Install the dependencies
120
125
pip install -r requirements.txt
121
126
122
127
# Start your local server
123
-
python3 app.py
128
+
slack run
129
+
```
130
+
131
+
If you're not using the Slack CLI, a different `python` command can be used to start your app instead:
132
+
133
+
```sh
134
+
python app.py
124
135
```
125
136
126
137
Now that your app is running, you should be able to see it within Slack. In Slack, create a channel that you can test in and try inviting your bot to it using the `/invite @Your-app-name-here` command. Check that your app works by saying “hi” in the channel where your app is, and you should receive a message back from it. If you don’t, ensure you completed all the steps above.
@@ -138,23 +149,48 @@ For all of these steps, we will use [Block Kit Builder](https://app.slack.com/bl
138
149
139
150
### Updating the "hi" message
140
151
141
-
The first thing we want to do is change the “hi, how are you?” message from our app into something more useful. Here’s something that you can use to start off with, but you can make it your own within Block Kit Builder. Once you have something you like, copy the blocks by clicking the **Copy Payload** button in the top right.
152
+
The first thing we want to do is change the “hi, how are you?” message from our app into something more useful. Here’s a `blocks` object built withBlock Kit Builder:
142
153
143
-
Take the function below and place your blocks within the blocks dictionary `[]`. Update the payload:
144
-
* Remove the initial blocks key and convert any boolean true values to `True` to fit with Python conventions.
145
-
* If you see variables within `{}` brackets, this is part of an f-string, which allows you to insert variables within strings in a clean manner. Place the `f` character before these strings like this:
154
+
```json
155
+
156
+
"blocks": [
157
+
{
158
+
"type": "section",
159
+
"text": {
160
+
"type": "mrkdwn",
161
+
"text": "Confirm *{delivery_id}* is correct?"
162
+
}
163
+
},
164
+
{
165
+
"type": "actions",
166
+
"elements": [
167
+
{
168
+
"type": "button",
169
+
"text": {
170
+
"type": "plain_text",
171
+
"text": "Correct",
172
+
"emoji": true
173
+
},
174
+
"style": "primary",
175
+
"action_id": "approve_delivery"
176
+
},
177
+
{
178
+
"type": "button",
179
+
"text": {
180
+
"type": "plain_text",
181
+
"text": "Not correct",
182
+
"emoji": true
183
+
},
184
+
"style": "danger",
185
+
"action_id": "deny_delivery"
186
+
}
187
+
]
188
+
}
189
+
]
146
190
147
-
```python
148
-
{
149
-
"type": "section",
150
-
"text": {
151
-
"type": "mrkdwn",
152
-
"text": f"Confirm *{delivery_id}* is correct?", # place the "f" character here at the beginning of the string
153
-
},
154
-
},
155
191
```
156
192
157
-
Place all of this in the `sample_message.py` file.
193
+
Take the function below and place your blocks within the blocks dictionary `[]`.
Next, you’ll need to make some connections so that this function is called when a message is sent in the channel where your app is. Head to `messages/__init__.py` and add the line below to the register function. Don’t forget to add the import to the callback function as well!
206
+
Update the payload:
207
+
* Remove the initial blocks key and convert any boolean true values to `True` to fit with Python conventions.
208
+
* If you see variables within `{}` brackets, this is part of an f-string, which allows you to insert variables within strings in a clean manner. Place the `f` character before these strings like this:
209
+
210
+
```python
211
+
{
212
+
"type": "section",
213
+
"text": {
214
+
"type": "mrkdwn",
215
+
"text": f"Confirm *{delivery_id}* is correct?", # place the "f" character here at the beginning of the string
216
+
},
217
+
},
218
+
```
219
+
220
+
Place all of this in the `sample_message.py` file.
221
+
222
+
Next, you’ll need to register this listener to respond when a message is sent in the channel with your app. Head to `messages/__init__.py` and overwrite the function there with the one below, which registers the function. Don’t forget to add the import to the callback function as well!
171
223
172
224
```python
173
225
from .sample_message import delivery_message_callback # import the function to this file
174
226
175
227
defregister(app: App):
176
-
app.message(re.compile("(hi|hello|hey)"))(sample_message_callback) # This can be deleted!
177
228
# This regex will capture any number letters followed by dash
178
229
# and then any number of digits, our "confirmation number" e.g. ASDF-1234
179
230
app.message(re.compile(r"[A-Za-z]+-\d+"))(delivery_message_callback) ## add this line!
0 commit comments