Spaces:
Running

axeronis commited on
Commit
4fe7869
·
1 Parent(s): 5a3f04c

feat(telegram): add webhook re-registration endpoint for allowed_updates fix

Browse files
Files changed (1) hide show
  1. backend/app/api/telegram_webhook.py +49 -0
backend/app/api/telegram_webhook.py CHANGED
@@ -102,6 +102,55 @@ async def verify_webhook_status(tenant_id: UUID):
102
  }
103
 
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  async def process_telegram_message(
106
  tenant_id: UUID,
107
  chat_id: str,
 
102
  }
103
 
104
 
105
+ @router.post("/{tenant_id}/register")
106
+ async def register_telegram_webhook(
107
+ tenant_id: UUID,
108
+ session: AsyncSession = Depends(get_async_session)
109
+ ):
110
+ """
111
+ Re-register Telegram webhook with correct allowed_updates.
112
+ This MUST be called after initial setup to enable voice/photo/document messages!
113
+ """
114
+ tenant = await session.get(Tenant, tenant_id)
115
+ if not tenant:
116
+ raise HTTPException(status_code=404, detail="Tenant not found")
117
+
118
+ bot_token = tenant.telegram_bot_token or settings.TELEGRAM_NOTIFICATION_TOKEN
119
+ if not bot_token:
120
+ raise HTTPException(status_code=400, detail="No Telegram bot token configured")
121
+
122
+ # Construct webhook URL (Railway domain)
123
+ # User needs to provide their Railway domain
124
+ webhook_url = f"https://microfunnel-production.up.railway.app/api/v1/webhook/telegram/{tenant_id}"
125
+
126
+ # Register webhook with ALL message types
127
+ async with httpx.AsyncClient() as client:
128
+ # First, delete existing webhook
129
+ await client.get(f"https://api.telegram.org/bot{bot_token}/deleteWebhook")
130
+
131
+ # Then set new webhook with allowed_updates
132
+ response = await client.post(
133
+ f"https://api.telegram.org/bot{bot_token}/setWebhook",
134
+ json={
135
+ "url": webhook_url,
136
+ "allowed_updates": ["message", "edited_message", "channel_post"]
137
+ }
138
+ )
139
+ result = response.json()
140
+
141
+ # Also get current webhook info
142
+ info_res = await client.get(f"https://api.telegram.org/bot{bot_token}/getWebhookInfo")
143
+ info = info_res.json()
144
+
145
+ logger.info(f"📡 Webhook registered: {result}")
146
+ return {
147
+ "status": "registered",
148
+ "webhook_url": webhook_url,
149
+ "telegram_response": result,
150
+ "webhook_info": info.get("result", {})
151
+ }
152
+
153
+
154
  async def process_telegram_message(
155
  tenant_id: UUID,
156
  chat_id: str,