Two Bugs Causing Internal Server Error in WhatsApp Bot on Hugging Face Spaces
Bug Report: Internal Server Error on Hugging Face Spaces
Two bugs in main.py were causing the 500 Internal Server Error.
Bug 1: Unescaped CSS braces in DASHBOARD_HTML (primary cause)
The DASHBOARD_HTML string is rendered using Python's .format() method, which treats any { and } in the string as template variable placeholders. The HTML template is full of CSS rules like * { margin: 0; padding: 0; }, body { font-family: ... }, and so on. Python tries to look these up as named variables and immediately raises a KeyError, crashing the GET / dashboard route with a 500.
The fix is to escape every CSS brace as {{ and }} throughout the template string, while leaving the actual template variables (e.g. {messages_received}, {messages_html}) as single braces. Python's .format() then treats {{ as a literal { in the output.
Bug 2: Deprecated @app.on_event("startup") decorator
The startup logic uses @app.on_event("startup"), which was deprecated in FastAPI 0.93 and can cause warnings or failures depending on the FastAPI/uvicorn version deployed in the Space. HF Spaces tends to use recent package versions, which makes this a real risk.
The fix is to replace it with the modern lifespan context manager pattern an async generator decorated with @asynccontextmanager, passed as lifespan= to FastAPI(...). The startup logic runs before yield, and any shutdown logic would go after. The lifespan function also needs to be defined before app = FastAPI(...) is called, since it's passed in at instantiation time.
Both fixes have been validated: the module loads cleanly, and DASHBOARD_HTML.format(...) now works without errors.
Looks like the app works now. Here to help for any other requirements. Great work on the app @miftahulkhairim
@miftahulkhairim is required to add keys for the following
| Name | Status | Secret Key to Add |
|---|---|---|
| WhatsApp Verify Token | ⚠️ Not set | WHATSAPP_VERIFY_TOKEN |
| WhatsApp Access Token | ⚠️ Not set | WHATSAPP_TOKEN |
| Phone Number ID | ⚠️ Not set | PHONE_NUMBER_ID |
| HF Token (AI Inference) | ⚠️ Not set | HF_TOKEN |
Hi @miftahulkhairim , great work on the app! Just wanted to share something from my testing: outbound messaging is working fine from the Meta platform, but inbound messages from Meta are not coming through. However, inbound works perfectly when tested via a curl call..
Hi @miftahulkhairim , great work on the app! Just wanted to share something from my testing: outbound messaging is working fine from the Meta platform, but inbound messages from Meta are not coming through. However, inbound works perfectly when tested via a curl call..