Created At: 2026-11-19T08:24:02Z Completed At: 2026-11-19T08:24:03Z Command exited with code 0. Command output: 218: async def handle_webhook_update( 219- self, 220- bot_id: UUID, 221- received_secret_token: str, 222- update_dict: dict[str, Any], 223- ) -> dict[str, Any]: 224- """ 225- Processes incoming Telegram Webhook updates. 226- 1. Validates X-Telegram-Bot-Api-Secret-Token. 227- 2. Deduplicates update_id to prevent duplicate executions. 228- 3. Upserts bot user. 229- 4. Renders and dispatches responses via MessageStudio templates. 230- """ 231- # 1. Fetch bot and webhook details 232- bot = await self.repo.get_by_id(bot_id) 233- if not bot or bot.status != "active": 234- raise NotFoundError("Bot is inactive or not found") 235- 236- webhook = await self.repo.get_webhook(bot_id) 237- if not webhook or not webhook.is_active: 238- raise UnauthorizedError("Bot webhook is not active") 239- 240- # 2. Verify Secret Token 241- if not verify_webhook_secret(received_secret_token, webhook.secret_hash): 242- logger.warning("webhook_secret_mismatch", bot_id=str(bot_id)) 243- raise UnauthorizedError("Invalid Telegram webhook secret token") 244- 245- # 3. Deduplicate update 246- update_id = update_dict.get("update_id") 247- if update_id is not None: 248- is_new = await self.repo.check_and_record_update(bot_id, update_id) 249- if not is_new: 250- logger.info("duplicate_update_dropped", bot_id=str(bot_id), update_id=update_id) 251- return {"ok": True, "duplicate": True} 252- 253- # 4. Decrypt bot token to reply 254- creds = await self.repo.get_credentials(bot_id) 255- if not creds: 256- return {"ok": False, "error": "No credentials"} 257- raw_token = decrypt_string(creds.encrypted_token, creds.key_version) 258- 259- # 5. Extract event data 260- message = update_dict.get("message") 261- callback_query = update_dict.get("callback_query") 262- 263- from_user = None 264- chat_id = None 265- text = "" 266- 267- if message: 268- from_user = message.get("from", {}) 269- chat_id = message.get("chat", {}).get("id") 270- text = message.get("text", "") 271- elif callback_query: 272- from_user = callback_query.get("from", {}) 273- chat_id = callback_query.get("message", {}).get("chat", {}).get("id") 274- text = callback_query.get("data", "") 275- 276- if not from_user or not chat_id: 277- return {"ok": True, "skipped": True} 278- 279- # 6. Upsert Telegram Bot User with automatic BCP 47 locale normalization 280- tg_uid = from_user.get("id") 281- detected_lang = from_user.get("language_code") 282- normalized_initial_locale = LocaleManager.normalize_locale(detected_lang) if detected_lang else bot.default_locale 283- 284- user_record = await self.repo.upsert_bot_user( 285- bot_id=bot_id, 286- telegram_user_id=tg_uid, 287- username=from_user.get("username"), 288- first_name=from_user.get("first_name"), 289- last_name=from_user.get("last_name"), 290- preferred_locale=normalized_initial_locale, 291- ) 292- 293- user_locale = user_record.preferred_locale or bot.default_locale 294- context = { 295- "first_name": user_record.first_name or "User", 296- "last_name": user_record.last_name or "", 297- "username": user_record.username or "user", 298- "user_id": str(user_record.telegram_user_id),