Spaces:
Sleeping
Sleeping
Add Pydantic models to fix 422 validation errors
Browse files- main.py +5 -5
- telegram_handlers.py +25 -14
main.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from telegram_handlers import telegram_webhook, test_webhook
|
| 3 |
from utils import dns_test, test_ai_response
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
@@ -9,12 +9,12 @@ async def root():
|
|
| 9 |
return {"message": "Hadhramout Bank AI Backend is Live"}
|
| 10 |
|
| 11 |
@app.post("/webhook")
|
| 12 |
-
async def webhook(
|
| 13 |
-
return await telegram_webhook(
|
| 14 |
|
| 15 |
@app.post("/test")
|
| 16 |
-
async def test(
|
| 17 |
-
return await test_webhook(
|
| 18 |
|
| 19 |
@app.get("/dns-test")
|
| 20 |
async def dns():
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from telegram_handlers import telegram_webhook, test_webhook, WebhookData
|
| 3 |
from utils import dns_test, test_ai_response
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
|
|
| 9 |
return {"message": "Hadhramout Bank AI Backend is Live"}
|
| 10 |
|
| 11 |
@app.post("/webhook")
|
| 12 |
+
async def webhook(data: WebhookData):
|
| 13 |
+
return await telegram_webhook(data)
|
| 14 |
|
| 15 |
@app.post("/test")
|
| 16 |
+
async def test(data: WebhookData):
|
| 17 |
+
return await test_webhook(data)
|
| 18 |
|
| 19 |
@app.get("/dns-test")
|
| 20 |
async def dns():
|
telegram_handlers.py
CHANGED
|
@@ -1,23 +1,35 @@
|
|
| 1 |
from fastapi import Request
|
|
|
|
| 2 |
import httpx
|
| 3 |
from config import TELEGRAM_URL
|
| 4 |
from ai_service import get_ai_response
|
| 5 |
from database import db_manager
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
data = await request.json()
|
| 10 |
print(f"Received webhook data: {data}")
|
| 11 |
|
| 12 |
-
if
|
| 13 |
-
telegram_id = data
|
| 14 |
-
user_text = data
|
| 15 |
|
| 16 |
# Extract user info
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
last_name = user_info.get("last_name")
|
| 21 |
|
| 22 |
if user_text:
|
| 23 |
# Save user message to database if available
|
|
@@ -50,16 +62,15 @@ async def telegram_webhook(request: Request):
|
|
| 50 |
print(f"Error in webhook: {str(e)}")
|
| 51 |
return {"status": "error", "message": str(e)}
|
| 52 |
|
| 53 |
-
async def test_webhook(
|
| 54 |
try:
|
| 55 |
-
data = await request.json()
|
| 56 |
print(f"Received test webhook data: {data}")
|
| 57 |
|
| 58 |
-
if
|
| 59 |
return {"error": "Missing 'message' field in request"}
|
| 60 |
|
| 61 |
-
telegram_id = data
|
| 62 |
-
user_text = data
|
| 63 |
|
| 64 |
# Save user message to database if available
|
| 65 |
if db_manager:
|
|
|
|
| 1 |
from fastapi import Request
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import httpx
|
| 4 |
from config import TELEGRAM_URL
|
| 5 |
from ai_service import get_ai_response
|
| 6 |
from database import db_manager
|
| 7 |
|
| 8 |
+
class ChatInfo(BaseModel):
|
| 9 |
+
id: int
|
| 10 |
+
username: str = None
|
| 11 |
+
first_name: str = None
|
| 12 |
+
last_name: str = None
|
| 13 |
+
|
| 14 |
+
class Message(BaseModel):
|
| 15 |
+
chat: ChatInfo
|
| 16 |
+
text: str = ""
|
| 17 |
+
|
| 18 |
+
class WebhookData(BaseModel):
|
| 19 |
+
message: Message = None
|
| 20 |
+
|
| 21 |
+
async def telegram_webhook(data: WebhookData):
|
| 22 |
try:
|
|
|
|
| 23 |
print(f"Received webhook data: {data}")
|
| 24 |
|
| 25 |
+
if data.message:
|
| 26 |
+
telegram_id = data.message.chat.id
|
| 27 |
+
user_text = data.message.text
|
| 28 |
|
| 29 |
# Extract user info
|
| 30 |
+
username = data.message.chat.username
|
| 31 |
+
first_name = data.message.chat.first_name
|
| 32 |
+
last_name = data.message.chat.last_name
|
|
|
|
| 33 |
|
| 34 |
if user_text:
|
| 35 |
# Save user message to database if available
|
|
|
|
| 62 |
print(f"Error in webhook: {str(e)}")
|
| 63 |
return {"status": "error", "message": str(e)}
|
| 64 |
|
| 65 |
+
async def test_webhook(data: WebhookData):
|
| 66 |
try:
|
|
|
|
| 67 |
print(f"Received test webhook data: {data}")
|
| 68 |
|
| 69 |
+
if not data.message:
|
| 70 |
return {"error": "Missing 'message' field in request"}
|
| 71 |
|
| 72 |
+
telegram_id = data.message.chat.id
|
| 73 |
+
user_text = data.message.text
|
| 74 |
|
| 75 |
# Save user message to database if available
|
| 76 |
if db_manager:
|