RELAYCall

ai_message

View as MarkdownOpen in Claude

Send a message to an active AI agent session on the call. Use this to inject context, instructions, or simulated user input into a running AI conversation.

This method requires an active AI session started via ai(). Calling it without an active session has no effect.

Parameters

message_text
Optional[str]

The message text to send to the AI agent.

role
Optional[str]

The role of the message sender. Valid values:

  • "user" — simulate user input
  • "system" — send a system-level instruction
  • "assistant" — inject an assistant response
reset
Optional[dict]

Reset configuration. Allows resetting AI state such as the conversation history or functions.

global_data
Optional[dict]

Update the global data accessible to the AI and SWAIG functions.

Returns

dict — Server response confirming the message was sent.

Example

1import asyncio
2from signalwire.relay import RelayClient
3
4client = RelayClient(
5 project="your-project-id",
6 token="your-api-token",
7 host="your-space.signalwire.com",
8 contexts=["default"],
9)
10
11@client.on_call
12async def handle_call(call):
13 await call.answer()
14
15 # Start an AI agent
16 action = await call.ai(
17 prompt={"text": "You are a helpful assistant."},
18 )
19
20 # Inject a system message after 10 seconds
21 await asyncio.sleep(10)
22 await call.ai_message(
23 message_text="The caller is a VIP customer. Be extra helpful.",
24 role="system",
25 )
26
27 await action.wait()
28
29client.run()