11from __future__ import annotations
22
33import asyncio
4+ import contextlib
45import logging
56import os
67import tempfile
1516 Update ,
1617)
1718from telegram .ext import (
18- CallbackQueryHandler ,
19- CommandHandler ,
2019 ContextTypes ,
21- MessageHandler ,
22- filters ,
2320)
2421
2522from opencode_telegram_bot .api import OpenCodeClient , OpenCodeServer
@@ -146,21 +143,15 @@ async def _ensure_session(self) -> str:
146143
147144 async def _cache_metadata (self ) -> None :
148145 if not self ._cached_providers :
149- try :
146+ with contextlib . suppress ( Exception ) :
150147 providers_data = await self .client .get_providers ()
151- self ._cached_providers = providers_data .get ("all" , [])
152- except Exception :
153- pass
148+ self ._cached_providers = providers_data .get ("providers" , [])
154149 if not self ._cached_agents :
155- try :
150+ with contextlib . suppress ( Exception ) :
156151 self ._cached_agents = await self .client .get_agents ()
157- except Exception :
158- pass
159152 if not self ._cached_commands :
160- try :
153+ with contextlib . suppress ( Exception ) :
161154 self ._cached_commands = await self .client .get_commands ()
162- except Exception :
163- pass
164155
165156 async def cmd_start (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
166157 if not self ._is_authorized (update .effective_user .id ):
@@ -296,7 +287,6 @@ async def cmd_mode(self, update: Update, context: ContextTypes.DEFAULT_TYPE) ->
296287 current = self .bot_settings .get ("agent_mode" , "build" )
297288 new_mode = "plan" if current == "build" else "build"
298289 self .bot_settings .set ("agent_mode" , new_mode )
299- key = "mode_plan" if new_mode == "plan" else "mode_build"
300290 await update .message .reply_text (t ("mode_switched" , locale = self ._locale (), mode = new_mode .capitalize ()))
301291
302292 async def cmd_models (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
@@ -410,8 +400,11 @@ async def callback_handler(self, update: Update, context: ContextTypes.DEFAULT_T
410400 parts = data .split (":" , 2 )
411401 provider = parts [1 ]
412402 model_id = parts [2 ]
413- model_str = f"{ provider } /{ model_id } " if model_id else provider
414- await self ._send (context , chat_id , t ("model_switched" , locale = self ._locale (), provider = provider , model = model_id or "default" ))
403+ await self ._send (
404+ context , chat_id ,
405+ t ("model_switched" , locale = self ._locale (),
406+ provider = provider , model = model_id or "default" ),
407+ )
415408
416409 elif data .startswith ("runcmd:" ):
417410 command = data .split (":" , 1 )[1 ]
@@ -498,7 +491,7 @@ async def _process_prompt(
498491 await self ._send (context , chat_id , t ("error" , locale = self ._locale (), message = str (e )))
499492 return
500493
501- thinking_msg = await self ._send (context , chat_id , t ("thinking" , locale = self ._locale ()))
494+ await self ._send (context , chat_id , t ("thinking" , locale = self ._locale ()))
502495
503496 try :
504497 agent_mode = self .bot_settings .get ("agent_mode" , "build" )
@@ -517,25 +510,34 @@ async def _process_prompt(
517510
518511 if response_text :
519512 if len (response_text ) > 4000 :
520- with tempfile .NamedTemporaryFile (suffix = ".txt" , delete = False , mode = "w" , encoding = "utf-8" ) as f :
513+ with tempfile .NamedTemporaryFile (
514+ suffix = ".txt" , delete = False , mode = "w" , encoding = "utf-8" ,
515+ ) as f :
521516 f .write (response_text )
522517 f .flush ()
523- await context .bot .send_document (
524- chat_id = chat_id ,
525- document = open (f .name , "rb" ),
526- filename = "response.txt" ,
527- )
528- os .unlink (f .name )
518+ tmp_path = f .name
519+ with open (tmp_path , "rb" ) as doc :
520+ await context .bot .send_document (
521+ chat_id = chat_id ,
522+ document = doc ,
523+ filename = "response.txt" ,
524+ )
525+ os .unlink (tmp_path )
529526 else :
530527 await self ._send (context , chat_id , response_text )
531528
532529 if self .bot_settings .tts_enabled and self .tts .is_configured :
533530 audio = await self .tts .synthesize (response_text [:4000 ])
534531 if audio :
535- with tempfile .NamedTemporaryFile (suffix = ".mp3" , delete = False ) as f :
532+ with tempfile .NamedTemporaryFile (
533+ suffix = ".mp3" , delete = False ,
534+ ) as f :
536535 f .write (audio )
537536 audio_path = f .name
538- await context .bot .send_voice (chat_id = chat_id , voice = open (audio_path , "rb" ))
537+ with open (audio_path , "rb" ) as voice_file :
538+ await context .bot .send_voice (
539+ chat_id = chat_id , voice = voice_file ,
540+ )
539541 os .unlink (audio_path )
540542 else :
541543 raw = str (response )[:4000 ]
0 commit comments