OpenAI Agents SDK
OpenAI Agents SDK · OpenAI Agents SDK WhatsApp · OpenAI agent Telegram · OpenAI Agents SDK iMessage · connect OpenAI agent to WhatsApp · ChatGPT agent WhatsApp APIReceive messages and send an OpenAI Agents SDK run back as chat bubbles.
Run your OpenAI Agents SDK agent for each incoming message and pass its stream to Flow’s reply(). The messaging integration is the same on each connected channel.
// npm install @flow-engineer/messaging @openai/agentsimport { Agent, run } from "@openai/agents";import { FlowMessaging, contentText } from "@flow-engineer/messaging";
const flow = new FlowMessaging();const agent = new Agent({ name: "Bakery assistant", instructions: "You help customers of Asha's Bakery with orders and delivery. Keep answers short.", model: "gpt-4.1-mini",});
for await (const event of flow.events.stream({ types: ["message.received"] })) { const result = await run(agent, contentText(event.data.message.content), { stream: true }); await event.conversation.reply(result); // reads result.toTextStream()}# pip install openai-agents fastapi requests# Plain HTTP, no Flow SDK; the Python SDK (pip install flow-messaging) wraps the same calls. This calls the HTTP API.import os, requestsfrom fastapi import FastAPI, Request, BackgroundTasksfrom 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}/v0/conversations/{event['conversation']['id']}/basic", headers={**HEADERS, "Idempotency-Key": event["id"]}, json={"text": result.final_output}, # sent exactly as written: no markdown is rendered 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 {}How it fits
Section titled “How it fits”- In TypeScript,
run(agent, input, { stream: true })returns a streamed result;reply()reads its text stream and sends bubbles while the agent runs. - In Python, answer the webhook with
{}at once and send the agent’sfinal_outputafterwards. Text longer than the channel’s limit (4096 characters on Telegram) is refused, so split long answers at paragraph breaks (the bubble rule) and send each part with its ownIdempotency-Key. - Give the agent the conversation’s history from
GET /v0/conversations/{conversation_id}/messages, or keep your own session keyed byconversation.id. - Verify webhook signatures as shown in Events and webhooks.
- To see each turn’s model and tool calls and get every reply graded, send this agent’s traces to Flow Observe: Traces from agent frameworks.
Full example
Section titled “Full example”examples/openai-agents on GitHub.
Was this page helpful?
Thanks, we read every comment.