RelayCall

on

View as MarkdownOpen in Claude

Register a listener for events on this call. The handler is called each time an event of the specified type is received. Handlers can be regular functions or async coroutines.

Parameters

event_type
strRequired

The event type string to listen for (e.g., "calling.call.state", "calling.call.play"). See the Events section on the Call page for class-level events, or individual method pages for method-specific events.

handler
Callable[[RelayEvent], Any]Required

Function or coroutine to invoke when the event fires. Receives a RelayEvent instance.

Returns

None

Example

from signalwire.relay import RelayClient
client = RelayClient(
project="your-project-id",
token="your-api-token",
host="your-space.signalwire.com",
contexts=["default"],
)
@client.on_call
async def handle_call(call):
await call.answer()
# Register a listener for play events
call.on("calling.call.play", lambda event: print(f"Play state: {event.params.get('state')}"))
# Register a listener for state changes
def on_state_change(event):
print(f"Call state changed: {event.params.get('call_state')}")
call.on("calling.call.state", on_state_change)
action = await call.play([{"type": "tts", "params": {"text": "Hello!"}}])
await action.wait()
client.run()