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

1from signalwire.relay import RelayClient
2
3client = RelayClient(
4 project="your-project-id",
5 token="your-api-token",
6 host="your-space.signalwire.com",
7 contexts=["default"],
8)
9
10@client.on_call
11async def handle_call(call):
12 await call.answer()
13
14 # Register a listener for play events
15 call.on("calling.call.play", lambda event: print(f"Play state: {event.params.get('state')}"))
16
17 # Register a listener for state changes
18 def on_state_change(event):
19 print(f"Call state changed: {event.params.get('call_state')}")
20
21 call.on("calling.call.state", on_state_change)
22
23 action = await call.play([{"type": "tts", "text": "Hello!"}])
24 await action.wait()
25
26client.run()