@@ -258,10 +258,11 @@ def find_installation(
258258
259259 query = self .installations .select ().where (where_clause ).order_by (desc (c .installed_at )).limit (1 )
260260
261+ installation : Optional [Installation ] = None
261262 with self .engine .connect () as conn :
262263 result : object = conn .execute (query )
263264 for row in result : # type: ignore
264- return Installation (
265+ installation = Installation (
265266 app_id = row ["app_id" ],
266267 enterprise_id = row ["enterprise_id" ],
267268 enterprise_name = row ["enterprise_name" ],
@@ -288,7 +289,28 @@ def find_installation(
288289 token_type = row ["token_type" ],
289290 installed_at = row ["installed_at" ],
290291 )
291- return None
292+
293+ if user_id is not None and installation is not None :
294+ # Retrieve the latest bot token, just in case
295+ # See also: https://github.com/slackapi/bolt-python/issues/664
296+ where_clause = and_ (
297+ c .client_id == self .client_id ,
298+ c .enterprise_id == enterprise_id ,
299+ c .team_id == team_id ,
300+ c .bot_token .is_not (None ), # the latest one that has a bot token
301+ )
302+ query = self .installations .select ().where (where_clause ).order_by (desc (c .installed_at )).limit (1 )
303+ with self .engine .connect () as conn :
304+ result : object = conn .execute (query )
305+ for row in result : # type: ignore
306+ installation .bot_token = row ["bot_token" ]
307+ installation .bot_id = row ["bot_id" ]
308+ installation .bot_user_id = row ["bot_user_id" ]
309+ installation .bot_scopes = row ["bot_scopes" ]
310+ installation .bot_refresh_token = row ["bot_refresh_token" ]
311+ installation .bot_token_expires_at = row ["bot_token_expires_at" ]
312+
313+ return installation
292314
293315 def delete_bot (self , * , enterprise_id : Optional [str ], team_id : Optional [str ]) -> None :
294316 table = self .bots
0 commit comments