Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions docs/content/concepts/listener-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ If your listener middleware is a quite simple one, you can use a listener matche
Refer to [the module document](https://tools.slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html) to learn the available listener arguments.

```python
# Listener middleware which filters out messages with "bot_message" subtype
# Listener middleware which filters out messages from a bot
def no_bot_messages(message, next):
subtype = message.get("subtype")
if subtype != "bot_message":
next()
if "bot_id" not in message:
next()

# This listener only receives messages from humans
@app.event(event="message", middleware=[no_bot_messages])
Expand All @@ -24,10 +23,10 @@ def log_message(logger, event):

# Listener matchers: simplified version of listener middleware
def no_bot_messages(message) -> bool:
return message.get("subtype") != "bot_message"
return "bot_id" not in message

@app.event(
event="message",
event="message",
matchers=[no_bot_messages]
# or matchers=[lambda message: message.get("subtype") != "bot_message"]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ slug: /concepts/listener-middleware
<span>指定可能な引数の一覧は<a href="https://tools.slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html">モジュールドキュメント</a>を参考にしてください。</span>

```python
# "bot_message" サブタイプのメッセージを抽出するリスナーミドルウェア
# ボットからのメッセージをフィルタリングするリスナーミドルウェア
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

かんぱい

def no_bot_messages(message, next):
subtype = message.get("subtype")
if subtype != "bot_message":
next()
if "bot_id" not in message:
next()

# このリスナーは人間によって送信されたメッセージのみを受け取ります
@app.event(event="message", middleware=[no_bot_messages])
Expand All @@ -24,13 +23,13 @@ def log_message(logger, event):

# リスナーマッチャー: 簡略化されたバージョンのリスナーミドルウェア
def no_bot_messages(message) -> bool:
return message.get("subtype") != "bot_message"
return "bot_id" not in message

@app.event(
event="message",
event="message",
matchers=[no_bot_messages]
# or matchers=[lambda message: message.get("subtype") != "bot_message"]
)
def log_message(logger, event):
logger.info(f"(MSG) User: {event['user']}\nMessage: {event['text']}")
```
```
Loading