# pip install openai-agents fastapi requests
# There is no Python SDK yet (only TypeScript is published); this calls the HTTP API.
import os, requests
from fastapi import FastAPI, Request, BackgroundTasks
from agents import Agent, Runner
API = "https://api.flow.engineer"
HEADERS = {"Authorization": f"Bearer {os.environ['FLOW_MESSAGING_KEY']}"}
agent = Agent(name="Bakery assistant", instructions="You help customers of Asha's Bakery. Keep answers short.")
app = FastAPI()
async def answer(event: dict):
text = event["data"]["message"]["content"].get("text", "")
result = await Runner.run(agent, text)
requests.post(
f"{API}/v1/conversations/{event['conversation']['id']}/messages",
headers={**HEADERS, "Idempotency-Key": event["id"]},
json={"content": {"type": "text", "text": result.final_output, "format": "markdown"}, "fallback": "auto"},
timeout=30,
)
@app.post("/flow")
async def flow_webhook(request: Request, tasks: BackgroundTasks):
event = await request.json() # verify Flow-Signature first: see Events and webhooks
if event["type"] == "message.received":
tasks.add_task(answer, event)
return {}