Connect an OpenAI Agents SDK Agent
An OpenAI Agents SDK Agent answers each message and replies with a @function_tool.
Tested with: openai-agents 0.26.0, Python 3.12, msend API v1.
1. Install
pip install -r examples/openai-agents-sdk/requirements.txt2. Define the send_message Tool
@function_tool
def send_message(conversation_id: str, text: str) -> str:
"""Reply to the person in a msend conversation. The reply is delivered on their channel."""
return msend("POST", f"/conversations/{conversation_id}/messages", json={"text": text})["id"]3. One Session per Conversation
msend’s conversation.id maps directly to SQLiteSession(conversation_id) (use a persistent file or database path in production):
def handle(data):
conversation_id = data["conversation"]["id"]
session = sessions.setdefault(conversation_id, SQLiteSession(conversation_id))
Runner.run_sync(
agent,
f"conversation_id={conversation_id}\nMessage: {data['message']['text']}",
session=session,
)4. Poll for Events
cursor = None
while True:
page = msend("GET", "/events", params={"after": cursor} if cursor else {})
for event in page["data"]:
data = event["data"]
if event["kind"] == "message.received" and data["message"]["addressed_to_agent"]:
handle(data)
cursor = page["cursor"] or cursor
time.sleep(1)Last updated on