If you've ever tried to put a Python AI agent onto WhatsApp, Facebook Messenger, and Instagram DMs, you know the pain starts after the LLM works.
The agent logic itself is the easy part. The hard part is that Meta's messaging surfaces look similar on the surface, yet behave like four completely different platforms underneath:
- WhatsApp Cloud API delivers
entry[].changes[].value.messages[], introducesfrom_user_id(bsuid) alongside phone numbers, splits interactive replies into 1–3 reply buttons vs. 4–10 list rows, and couplesmark_read+typing_indicatorin one call. - Facebook Messenger delivers
entry[].messaging[], routes byPSID, usesquick_replies(up to 13) andsender_action, and caps text at 2,000 characters. - Instagram Messaging splits into two different APIs and hosts depending on your app login flow: Facebook Login (
graph.facebook.com/{page_id}/messages) vs. Instagram Direct Login (graph.instagram.com/{ig_account_id}/messages), with a 1,000-character limit and strict media/choice separation.
Most teams end up either paying a SaaS messaging middleman per message or writing fragile if channel == "whatsapp": ... elif channel == "messenger": ... glue code across every webhook and worker.
So I built and published meta-agent-bridge (v0.1.0, Python 3.12+, MIT) — an async, agentic-first SDK that lets one AI agent speak to all four Meta surfaces through a single canonical schema.
Proof It Works Live: One FastAPI Agent, Three Real Channels
To stress-test v0.1.0 end-to-end against real Meta Graph API v27.0 traffic (not mocks), I wired up a NovaGear Sales & Support AI agent powered by FastAPI + a tool-calling LLM (search_products, lookup_order, create_support_ticket) behind a single /webhook route.
Here is the exact same prompt — "What laptops do you have in stock?" — plus interactive button taps running live across all three platforms:
1. WhatsApp Cloud API (Interactive Buttons + Follow-Up Tap)
2. Facebook Messenger (Product Catalog + Quick Replies)
3. Instagram Direct (graph.instagram.com Login Flow)
4. One Server Handling All Three Surfaces (uvicorn Logs)
Look at the server trace: every channel hits the same /webhook endpoint, verifies X-Hub-Signature-256, deduplicates event_id, dispatches MARK_READ + TYPING_ON + reply (200 OK), and cleanly processes sent / delivered / read status webhooks:
How meta-agent-bridge Is Designed
flowchart TD Meta["Meta Platforms (WhatsApp / Messenger / Instagram)"] -->|"POST /webhook + X-Hub-Signature-256"| FastAPI["FastAPI Webhook Route"] FastAPI -->|"bridge.parse_webhook()"| Verify["HMAC-SHA256 Verify & Surface Detect (core/crypto)"] Verify --> Drivers["Channel Drivers (whatsapp.py / meta_messaging.py)"] Drivers -->|"Inbound Message"| MsgEvt["AgentMessageEvent (JSON-serializable, sans-I/O)"] Drivers -->|"Sent / Delivered / Read"| StatusEvt["AgentStatusEvent"] Drivers -->|"Echo / Reaction / Unknown"| UnknownEvt["UnknownEvent (safe fallback)"] MsgEvt -->|"Optional media_id or URL"| Media["bridge.media (MediaDownloader)"] MsgEvt --> Agent["AI Agent (LLM + Tools)"] Media --> Agent Agent -->|"Channel-neutral AgentResponse"| Compile["bridge.compile() (core/compiler + capabilities)"] Compile --> Plan["CompiledPlan (ActionStep / TextStep / MediaStep / ButtonsStep)"] Plan -->|"bridge.send_plan() + per-call SecretStr"| Transport["GraphTransport (shared httpx.AsyncClient)"] Transport -->|"WhatsApp / Messenger / IG FB"| FB["graph.facebook.com/v27.0"] Transport -->|"IG Direct Login"| IG["graph.instagram.com/v27.0"] Four architectural choices make it safe for real agent workloads:
- One Canonical Schema (
AgentEvent/AgentResponse) Your agent never inspects raw Meta JSON. Whether an inbound click comes from a WhatsAppbutton_replyor a Messengerquick_reply, your agent receives anAgentMessageEventwith a normalizedinteractive_replyand returns a plainAgentResponse(text=..., choices=(Choice(...),), actions=(SenderAction.MARK_READ, SenderAction.TYPING_ON)). - Pure Sync Sans-I/O
core/bridge.parse_webhook()andbridge.compile()have zerohttpxor network imports. Events are plain JSON-serializable Pydantic models (schema_version="1.0", deterministicevent_idfor deduplication) with no.reply()method or bound HTTP client — serialize them straight to Redis, Celery, or FastAPIBackgroundTasks. - Multi-Tenant Connection Pooling Share one
httpx.AsyncClientacross all tenants and channels.GraphTransportbindsAuthorization: Bearer ...andbase_url(graph.facebook.comvs.graph.instagram.com) strictly per call. - Forward-Compatible
UnknownEventResilience Meta sends delivery echoes (echo_message), emoji reactions, and new webhook fields constantly. Instead of raising aValidationErrorand dropping your queue,meta-agent-bridgewraps unrecognized payloads safely inUnknownEventwithrawpreserved.
All the Webhook Plumbing in ~30 Lines of FastAPI
Here is the actual /webhook route running in the live test above:
And inside your agent worker, there is zero channel-specific formatting:
from meta_agent_bridge import AgentMessageEvent, AgentResponse, Choice, SenderAction async def handle_message(event: AgentMessageEvent) -> AgentResponse: # Run your LLM / LangGraph / PydanticAI / tool-calling loop here return AgentResponse( text="In stock: NovaBook Pro 14 ($1,499) and NovaBook Air 13 ($999).", choices=( Choice(id="compare_laptops", title="Compare Laptops"), Choice(id="track_order", title="Track Order"), ), actions=(SenderAction.MARK_READ, SenderAction.TYPING_ON), reply_to_message_id=event.message_id, ) When bridge.compile(response, event.target, creds) runs:
- On WhatsApp, it splits
MARK_READ+TYPING_ON, renders interactive reply buttons (or a list menu if 4–10 choices), and routes via phone orbsuid. - On Messenger and Instagram, it renders
sender_actionsteps andquick_replies(automatically targetinggraph.facebook.com/{page_id}/messagesorgraph.instagram.com/{ig_account_id}/messages).
What Live Testing Taught Us (Real Graph API v27.0 Gotchas)
Testing against live Meta apps caught real-world quirks that mocks miss:
- WhatsApp Business Account Subscriptions: Configuring the webhook URL in the Meta App Dashboard isn't enough by itself — your WABA ID also has to be subscribed via
POST /{waba_id}/subscribed_appsbefore Meta forwards inbound messages. - WhatsApp BSUIDs (
from_user_id): Graph APIv27.0already sendsfrom_user_id(e.g.PK.2438501963353892) in live payloads —meta-agent-bridgecapturesbsuiddirectly onTenantTarget. - Self-Contained Replies for Instagram Web: Instagram's desktop web UI hides
quick_replieschips while mobile shows them, so agents should keep key product/order details insidereply_textand usechoicesstrictly for follow-up actions.
Get Started in 30 Seconds
uv add meta-agent-bridge # or pip install meta-agent-bridge - GitHub (includes the runnable example in
examples/nvidia_sales_agent/): github.com/Umar4880/meta-agent-bridge - PyPI: pypi.org/project/meta-agent-bridge
If you're building AI agents on WhatsApp, Messenger, or Instagram, give it a try — issues, PRs, and feedback on v0.2.0 (templates, URL buttons, reactions) are very welcome!




