-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathplayback_example.py
64 lines (48 loc) · 1.74 KB
/
playback_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""Example demonstrating using the returned object from an API call.
This app plays demo-contrats on any channel sent to Stasis(hello). DTMF keys
are used to control the playback.
"""
#
# Copyright (c) 2013, Digium, Inc.
#
from __future__ import print_function
import ari
import sys
client = ari.connect('http://localhost:8088/', 'hey', 'peekaboo')
def on_start(channel, event):
"""Callback for StasisStart events.
On new channels, answer, play demo-congrats, and register a DTMF listener.
:param channel: Channel DTMF was received from.
:param event: Event.
"""
channel.answer()
playback = channel.play(media='sound:demo-congrats')
def on_dtmf(channel, event):
"""Callback for DTMF events.
DTMF events control the playback operation.
:param channel: Channel DTMF was received on.
:param event: Event.
"""
# Since the callback was registered to a specific channel, we can
# control the playback object we already have in scope.
digit = event['digit']
if digit == '5':
playback.control(operation='pause')
elif digit == '8':
playback.control(operation='unpause')
elif digit == '4':
playback.control(operation='reverse')
elif digit == '6':
playback.control(operation='forward')
elif digit == '2':
playback.control(operation='restart')
elif digit == '#':
playback.stop()
channel.continueInDialplan()
else:
print("Unknown DTMF %s" % digit, file=sys.stderr)
channel.on_event('ChannelDtmfReceived', on_dtmf)
client.on_channel_event('StasisStart', on_start)
# Run the WebSocket
client.run(apps='hello')