-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
59 lines (43 loc) · 1.62 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
This module contains the FastAPI application for the Audio Transcribe Bot.
Routes:
- GET /: Returns a welcome message.
- POST /webhook: Processes incoming updates from Telegram.
- GET /webhook: Confirms the webhook is working.
Logging is configured to output info and error messages.
"""
import logging
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse, PlainTextResponse
from mangum import Mangum
from bot_handlers import handle_message, handle_update
from config import Config
# Import your custom modules if they are compatible with AWS Lambda
# Ensure that 'config' and 'bot_handlers' are included in your deployment package
app = FastAPI()
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
@app.get("/")
async def index():
return PlainTextResponse("Welcome to the Audio Transcribe Bot!")
@app.post("/webhook")
async def webhook(request: Request):
try:
update = await request.json()
logger.info(f"Received update: {update}")
handle_update(update)
return JSONResponse({"status": "ok"})
except Exception as e:
logger.error(f"Error processing webhook: {str(e)}")
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
@app.get("/webhook")
async def webhook_get():
return PlainTextResponse(
"Webhook is working. Send a POST request with a Telegram update to use the bot.",
status_code=200,
)
# AWS Lambda handler using Mangum
# handler = Mangum(app) # if using AWS Lambda