Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, File, UploadFile, HTTPException | |
from fastapi.responses import HTMLResponse | |
from fastapi.responses import StreamingResponse | |
from fastapi.responses import FileResponse | |
from fastapi.middleware.cors import CORSMiddleware | |
from io import StringIO | |
import os | |
import uuid,requests | |
GOOD_BOY_URL = ( | |
"https://images.unsplash.com/photo-1518717758536-85ae29035b6d?ixlib=rb-1.2.1" | |
"&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80" | |
) | |
from fastapi import FastAPI, Request, HTTPException | |
from fastapi.responses import PlainTextResponse | |
from twilio.twiml.messaging_response import MessagingResponse | |
app = FastAPI() | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
async def reply_whatsapp(request: Request): | |
form_data = await request.form() | |
try: | |
num_media = int(form_data.get("NumMedia", 0)) | |
except (ValueError, TypeError): | |
raise HTTPException(status_code=400, detail="Invalid request: invalid or missing NumMedia parameter") | |
response = MessagingResponse() | |
if not num_media: | |
msg = response.message("Send us an image!") | |
else: | |
msg = response.message("Thanks for the image. Here's one for you!") | |
msg.media(GOOD_BOY_URL) | |
return PlainTextResponse(str(response)) | |