@@ -49,25 +49,35 @@ <h1 class="title">Module <code>slack_bolt.middleware.ignoring_self_events.ignori
4949 next: Callable[[], BoltResponse],
5050 ) -> BoltResponse:
5151 auth_result = req.context.authorize_result
52- if self._is_self_event(auth_result, req.context.user_id, req.body):
52+ # message events can have $.event.bot_id while it does not have its user_id
53+ bot_id = req.body.get("event", {}).get("bot_id")
54+ if self._is_self_event(auth_result, req.context.user_id, bot_id, req.body):
5355 self._debug_log(req.body)
5456 return req.context.ack()
5557 else:
5658 return next()
5759
5860 # -----------------------------------------
5961
60- # Its an Events API event that isn't of type message,
62+ # It's an Events API event that isn't of type message,
6163 # but the user ID might match our own app. Filter these out.
6264 # However, some events still must be fired, because they can make sense.
6365 events_that_should_be_kept = ["member_joined_channel", "member_left_channel"]
6466
6567 @classmethod
66- def _is_self_event(cls, auth_result: AuthorizeResult, user_id: str, body: Dict[str, Any]):
68+ def _is_self_event(
69+ cls,
70+ auth_result: AuthorizeResult,
71+ user_id: Optional[str],
72+ bot_id: Optional[str],
73+ body: Dict[str, Any],
74+ ):
6775 return (
6876 auth_result is not None
69- and user_id is not None
70- and user_id == auth_result.bot_user_id
77+ and (
78+ (user_id is not None and user_id == auth_result.bot_user_id)
79+ or (bot_id is not None and bot_id == auth_result.bot_id) # for bot_message events
80+ )
7181 and body.get("event") is not None
7282 and body.get("event", {}).get("type") not in cls.events_that_should_be_kept
7383 )
@@ -111,25 +121,35 @@ <h2 class="section-title" id="header-classes">Classes</h2>
111121 next: Callable[[], BoltResponse],
112122 ) -> BoltResponse:
113123 auth_result = req.context.authorize_result
114- if self._is_self_event(auth_result, req.context.user_id, req.body):
124+ # message events can have $.event.bot_id while it does not have its user_id
125+ bot_id = req.body.get("event", {}).get("bot_id")
126+ if self._is_self_event(auth_result, req.context.user_id, bot_id, req.body):
115127 self._debug_log(req.body)
116128 return req.context.ack()
117129 else:
118130 return next()
119131
120132 # -----------------------------------------
121133
122- # Its an Events API event that isn't of type message,
134+ # It's an Events API event that isn't of type message,
123135 # but the user ID might match our own app. Filter these out.
124136 # However, some events still must be fired, because they can make sense.
125137 events_that_should_be_kept = ["member_joined_channel", "member_left_channel"]
126138
127139 @classmethod
128- def _is_self_event(cls, auth_result: AuthorizeResult, user_id: str, body: Dict[str, Any]):
140+ def _is_self_event(
141+ cls,
142+ auth_result: AuthorizeResult,
143+ user_id: Optional[str],
144+ bot_id: Optional[str],
145+ body: Dict[str, Any],
146+ ):
129147 return (
130148 auth_result is not None
131- and user_id is not None
132- and user_id == auth_result.bot_user_id
149+ and (
150+ (user_id is not None and user_id == auth_result.bot_user_id)
151+ or (bot_id is not None and bot_id == auth_result.bot_id) # for bot_message events
152+ )
133153 and body.get("event") is not None
134154 and body.get("event", {}).get("type") not in cls.events_that_should_be_kept
135155 )
0 commit comments