File size: 1,385 Bytes
d92c861
 
 
 
 
 
 
54db18f
 
13ac926
 
 
 
005c659
d92c861
13ac926
 
 
d92c861
 
 
 
 
 
 
 
 
 
 
07fe890
 
13ac926
 
 
 
 
 
 
fed63e7
13ac926
 
 
 
 
 
 
e782b03
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
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=["*"],
)



@app.post("/whatsapp")
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))