from fastapi import FastAPI, Request, HTTPException from fastapi.responses import PlainTextResponse from fastapi.middleware.cors import CORSMiddleware from twilio.twiml.messaging_response import MessagingResponse import os import google.generativeai as genai secret = os.environ["key"] genai.configure(api_key=secret) model = genai.GenerativeModel('gemini-pro') 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" ) 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() num_media = int(form_data.get("NumMedia", 0)) from_number = form_data.get("From") message_body = form_data.get("Body") response = MessagingResponse() msg = response.message(f"Hi, your number is {from_number} and you said {message_body}") msg.media(GOOD_BOY_URL) return PlainTextResponse(str(response), media_type="application/xml") # Run the application (Make sure you have the necessary setup to run FastAPI)