Skip to content

Commit 8c883c6

Browse files
filmajseratch
andauthored
Docs on handling options listeners with a filtering example (#609)
Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent 3508337 commit 8c883c6

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

docs/_basic/ja_listening_responding_options.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,29 @@ order: 14
1414

1515
オプションのリクエストに応答するときは、有効なオプションを含む `options` または `option_groups` のリストとともに `ack()` を呼び出す必要があります。API サイトにある[外部データを使用する選択メニューに応答するサンプル例](https://api.slack.com/reference/messaging/block-elements#external-select)と、[ダイアログでの応答例](https://api.slack.com/dialogs#dynamic_select_elements_external)を参考にしてください。
1616

17+
さらに、ユーザーが入力したキーワードに基づいたオプションを返すようフィルタリングロジックを適用することもできます。 これは `payload` という引数の ` value` の値に基づいて、それぞれのパターンで異なるオプションの一覧を返すように実装することができます。 Bolt for Python のすべてのリスナーやミドルウェアでは、[多くの有用な引数](https://slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html)にアクセスすることができますので、チェックしてみてください。
18+
1719
</div>
1820

1921
<div>
2022
<span class="annotation">指定可能な引数の一覧は<a href="https://slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html" target="_blank">モジュールドキュメント</a>を参考にしてください。</span>
2123
```python
2224
# 外部データを使用する選択メニューオプションに応答するサンプル例
2325
@app.options("external_action")
24-
def show_options(ack):
26+
def show_options(ack, payload):
2527
options = [
2628
{
27-
"text": {"type": "plain_text", "text":"Option 1"},
28-
"value":"1-1",
29+
"text": {"type": "plain_text", "text": "Option 1"},
30+
"value": "1-1",
2931
},
3032
{
31-
"text": {"type": "plain_text", "text":"Option 2"},
32-
"value":"1-2",
33+
"text": {"type": "plain_text", "text": "Option 2"},
34+
"value": "1-2",
3335
},
3436
]
37+
keyword = payload.get("value")
38+
if keyword is not None and len(keyword) > 0:
39+
options = [o for o in options if keyword in o["text"]["text"]]
3540
ack(options=options)
3641
```
37-
</div>
42+
</div>

docs/_basic/listening_responding_options.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ While it's recommended to use `action_id` for `external_select` menus, dialogs d
1313

1414
To respond to options requests, you'll need to call `ack()` with a valid `options` or `option_groups` list. Both [external select response examples](https://api.slack.com/reference/messaging/block-elements#external-select) and [dialog response examples](https://api.slack.com/dialogs#dynamic_select_elements_external) can be found on our API site.
1515

16+
Additionally, you may want to apply filtering logic to the returned options based on user input. This can be accomplished by using the `payload` argument to your options listener and checking for the contents of the `value` property within it. Based on the `value` you can return different options. All listeners and middleware handlers in Bolt for Python have access to [many useful arguments](https://slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html) - be sure to check them out!
1617
</div>
1718

1819
<div>
1920
<span class="annotation">Refer to <a href="https://slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html" target="_blank">the module document</a> to learn the available listener arguments.</span>
2021
```python
2122
# Example of responding to an external_select options request
2223
@app.options("external_action")
23-
def show_options(ack):
24+
def show_options(ack, payload):
2425
options = [
2526
{
2627
"text": {"type": "plain_text", "text": "Option 1"},
@@ -31,6 +32,9 @@ def show_options(ack):
3132
"value": "1-2",
3233
},
3334
]
35+
keyword = payload.get("value")
36+
if keyword is not None and len(keyword) > 0:
37+
options = [o for o in options if keyword in o["text"]["text"]]
3438
ack(options=options)
3539
```
36-
</div>
40+
</div>

0 commit comments

Comments
 (0)