@@ -209,6 +209,7 @@ def find_bot(
209209 c .client_id == self .client_id ,
210210 c .enterprise_id == enterprise_id ,
211211 c .team_id == team_id ,
212+ c .bot_token .is_not (None ), # the latest one that has a bot token
212213 )
213214 )
214215 .order_by (desc (c .installed_at ))
@@ -294,25 +295,24 @@ def find_installation(
294295 installed_at = row ["installed_at" ],
295296 )
296297
297- if user_id is not None and installation is not None :
298+ has_user_installation = user_id is not None and installation is not None
299+ no_bot_token_installation = installation is not None and installation .bot_token is None
300+ should_find_bot_installation = has_user_installation or no_bot_token_installation
301+ if should_find_bot_installation :
298302 # Retrieve the latest bot token, just in case
299303 # See also: https://github.com/slackapi/bolt-python/issues/664
300- where_clause = and_ (
301- c .client_id == self .client_id ,
302- c .enterprise_id == enterprise_id ,
303- c .team_id == team_id ,
304- c .bot_token .is_not (None ), # the latest one that has a bot token
304+ latest_bot_installation = self .find_bot (
305+ enterprise_id = enterprise_id ,
306+ team_id = team_id ,
307+ is_enterprise_install = is_enterprise_install ,
305308 )
306- query = self .installations .select ().where (where_clause ).order_by (desc (c .installed_at )).limit (1 )
307- with self .engine .connect () as conn :
308- result : object = conn .execute (query )
309- for row in result .mappings (): # type: ignore
310- installation .bot_token = row ["bot_token" ]
311- installation .bot_id = row ["bot_id" ]
312- installation .bot_user_id = row ["bot_user_id" ]
313- installation .bot_scopes = row ["bot_scopes" ]
314- installation .bot_refresh_token = row ["bot_refresh_token" ]
315- installation .bot_token_expires_at = row ["bot_token_expires_at" ]
309+ if latest_bot_installation is not None and installation .bot_token != latest_bot_installation .bot_token :
310+ installation .bot_id = latest_bot_installation .bot_id
311+ installation .bot_user_id = latest_bot_installation .bot_user_id
312+ installation .bot_token = latest_bot_installation .bot_token
313+ installation .bot_scopes = latest_bot_installation .bot_scopes
314+ installation .bot_refresh_token = latest_bot_installation .bot_refresh_token
315+ installation .bot_token_expires_at = latest_bot_installation .bot_token_expires_at
316316
317317 return installation
318318
0 commit comments