Mastra
Mastra · Mastra WhatsApp · Mastra agent Telegram · Mastra iMessage · Mastra messaging integrationDeliver a Mastra agent's streamed replies through Flow Messaging.
Receive messages through Flow, run your Mastra agent and deliver its answer as chat bubbles. Keep the agent’s existing tools and workflows.
// npm install @flow-engineer/messaging @mastra/core @ai-sdk/openaiimport { Agent } from "@mastra/core/agent";import { openai } from "@ai-sdk/openai";import { FlowMessaging, contentText } from "@flow-engineer/messaging";
const flow = new FlowMessaging();const agent = new Agent({ id: "bakery-assistant", name: "Bakery assistant", instructions: "You help customers of Asha's Bakery with orders and delivery. Keep answers short.", model: openai("gpt-4.1-mini"),});
for await (const event of flow.events.stream({ types: ["message.received"] })) { const stream = await agent.stream(contentText(event.data.message.content)); await event.conversation.reply(stream); // reads stream.textStream}How it fits
Section titled “How it fits”reply()reads the stream’stextStream, keeps typing on and sends a bubble at each paragraph break.- Mastra memory: use the Flow
conversation.idas the thread ID andconversation.contact(the contact’s ID) as the resource ID, so each person keeps their own history. - In a Mastra server, receive Flow webhooks on a custom route and call the same code; verify signatures with
flow.webhooks.constructEvent(rawBody, signatureHeader, secret), which returns the typed event withevent.conversationready toreplyon. See 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/mastra on GitHub.
Was this page helpful?
Thanks, we read every comment.