-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_swml_service.py
More file actions
401 lines (325 loc) · 12.9 KB
/
Copy pathbasic_swml_service.py
File metadata and controls
401 lines (325 loc) · 12.9 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
"""
Copyright (c) 2025 SignalWire
This file is part of the SignalWire SDK.
Licensed under the MIT License.
See LICENSE file in the project root for full license information.
"""
"""
Basic SWML Service Example
This example demonstrates using SWMLService directly to create
and serve SWML documents without AI components. It shows how to
build various SWML flows for telephony applications.
Examples include:
1. Voicemail system
2. Interactive phone menu (IVR)
3. Call transfer system
4. Call recording
"""
import os
import sys
import json
import argparse
import logging
# Import structlog for logger instance creation
import structlog
# Import the SWMLService class - this will set up the logging configuration
from signalwire.core.swml_service import SWMLService
from signalwire.core.swml_builder import SWMLBuilder
# Create structured logger for this example
logger = structlog.get_logger("basic_swml")
class VoicemailService(SWMLService):
"""
Simple voicemail service that plays a greeting and records a message
"""
def __init__(self, host="0.0.0.0", port=3000):
super().__init__(
name="voicemail",
route="/voicemail",
host=host,
port=port
)
# Build the SWML document
self.build_voicemail_document()
def build_voicemail_document(self):
"""Build the voicemail SWML document"""
# Reset the document
self.reset_document()
# Add answer verb
self.add_answer_verb()
# Add play verb for greeting
self.add_verb("play", {
"url": "say:Hello, you've reached the voicemail service. Please leave a message after the beep."
})
# Add a small pause
self.add_verb("sleep", 1000) # 1 second in milliseconds
# Play a beep
self.add_verb("play", {
"url": "https://example.com/beep.wav" # Replace with actual beep sound URL
})
# Record the message
self.add_verb("record", {
"format": "mp3",
"stereo": False,
"beep": False, # We already played a beep
"max_length": 120, # 2 minutes max
"terminators": "#", # Allow # to stop recording
"status_url": "https://example.com/voicemail-status" # Replace with your status webhook
})
# Thank the caller
self.add_verb("play", {
"url": "say:Thank you for your message. Goodbye!"
})
# Hang up
self.add_verb("hangup", {})
self.log.debug("voicemail_document_built")
class IvrMenuService(SWMLService):
"""
Interactive Voice Response (IVR) menu system
"""
def __init__(self, host="0.0.0.0", port=3000):
super().__init__(
name="ivr",
route="/ivr",
host=host,
port=port
)
# Build the SWML document
self.build_ivr_document()
def build_ivr_document(self):
"""Build the IVR menu SWML document"""
# Reset the document
self.reset_document()
# Add answer verb
self.add_answer_verb()
# Add main menu section
self.add_section("main_menu")
self.log.debug("adding_section", section="main_menu")
# Add prompt verb for the main menu
self.add_verb_to_section("main_menu", "prompt", {
"play": "say:Welcome to our service. Press 1 for sales, 2 for support, or 3 to leave a message.",
"max_digits": 1,
"terminators": "#",
"digit_timeout": 5.0,
"initial_timeout": 10.0
})
# Add switch verb to handle menu options
self.add_verb_to_section("main_menu", "switch", {
"variable": "prompt_digits",
"case": {
"1": [
{"transfer": {"dest": "sales"}}
],
"2": [
{"transfer": {"dest": "support"}}
],
"3": [
{"transfer": {"dest": "voicemail"}}
]
},
"default": [
{"play": {"url": "say:I'm sorry, I didn't understand your selection."}},
{"transfer": {"dest": "main_menu"}}
]
})
# Add sales section
self.add_section("sales")
self.log.debug("adding_section", section="sales")
self.add_verb_to_section("sales", "play", {
"url": "say:Connecting you to sales. Please hold."
})
self.add_verb_to_section("sales", "connect", {
"to": "+15551234567" # Replace with actual sales number
})
# Add support section
self.add_section("support")
self.log.debug("adding_section", section="support")
self.add_verb_to_section("support", "play", {
"url": "say:Connecting you to support. Please hold."
})
self.add_verb_to_section("support", "connect", {
"to": "+15557654321" # Replace with actual support number
})
# Add voicemail section
self.add_section("voicemail")
self.log.debug("adding_section", section="voicemail")
self.add_verb_to_section("voicemail", "play", {
"url": "say:Please leave a message after the beep."
})
self.add_verb_to_section("voicemail", "sleep", 1000) # 1 second pause
self.add_verb_to_section("voicemail", "record", {
"format": "mp3",
"max_length": 120, # 2 minutes max
"terminators": "#"
})
self.add_verb_to_section("voicemail", "play", {
"url": "say:Thank you for your message. Goodbye!"
})
self.add_verb_to_section("voicemail", "hangup", {})
# Start with the main menu
self.add_verb("transfer", {
"dest": "main_menu"
})
self.log.debug("ivr_document_built")
class CallTransferService(SWMLService):
"""
Call transfer service with parallel dialing
"""
def __init__(self, host="0.0.0.0", port=3000):
super().__init__(
name="transfer",
route="/transfer",
host=host,
port=port
)
# Build the SWML document
self.build_transfer_document()
def build_transfer_document(self):
"""Build the call transfer SWML document"""
# Reset the document
self.reset_document()
# Add answer verb
self.add_answer_verb()
# Add play verb for greeting
self.add_verb("play", {
"url": "say:Thank you for calling. We'll connect you with the next available agent."
})
# Add connect verb with parallel dialing
self.log.debug("setting_up_parallel_dialing", agents=3)
self.add_verb("connect", {
"from": "+15551234567", # Replace with your from number
"timeout": 30, # 30 seconds timeout
"answer_on_bridge": True,
"ringback": ["ring:us"], # US ringback tone
"parallel": [
{"to": "+15552223333"}, # Replace with actual agent numbers
{"to": "+15554445555"},
{"to": "+15556667777"}
]
})
# Add fallback if no one answers
self.add_verb("play", {
"url": "say:We're sorry, but all of our agents are currently busy. Please leave a message."
})
# Record a message if no agents available
self.add_verb("record", {
"format": "mp3",
"stereo": False,
"beep": True,
"max_length": 120, # 2 minutes max
"terminators": "#" # Allow # to stop recording
})
# Thank the caller
self.add_verb("play", {
"url": "say:Thank you for your message. We'll get back to you as soon as possible."
})
# Hang up
self.add_verb("hangup", {})
self.log.debug("transfer_document_built")
class CallRecordingService(SWMLService):
"""
Demonstrates call recording features
"""
def __init__(self, host="0.0.0.0", port=3000):
super().__init__(
name="record",
route="/record",
host=host,
port=port
)
# Build the SWML document
self.build_recording_document()
def build_recording_document(self):
"""Build the call recording SWML document"""
# Reset the document
self.reset_document()
# Add answer verb
self.add_answer_verb()
# Start recording the call in the background
self.log.debug("starting_call_recording", format="mp3", stereo=True)
self.add_verb("record_call", {
"control_id": "call_recording",
"format": "mp3",
"stereo": True,
"direction": "both",
"beep": True
})
# Inform the caller about recording
self.add_verb("play", {
"url": "say:This call is being recorded for quality and training purposes."
})
# Add play verb for prompt
self.add_verb("play", {
"url": "say:Please tell us about your experience with our product."
})
# Add sleep to wait for the caller's response
self.add_verb("sleep", 30000) # 30 seconds
# Add play verb for closing
self.add_verb("play", {
"url": "say:Thank you for your feedback. Is there anything else you'd like to add?"
})
# Add sleep to wait for the caller's response
self.add_verb("sleep", 30000) # 30 seconds
# Stop recording
self.log.debug("stopping_call_recording", control_id="call_recording")
self.add_verb("stop_record_call", {
"control_id": "call_recording"
})
# Thank the caller
self.add_verb("play", {
"url": "say:Thank you for your time. Goodbye!"
})
# Hang up
self.add_verb("hangup", {})
self.log.debug("recording_document_built")
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description="Basic SWML Service Examples")
parser.add_argument("--service", choices=["voicemail", "ivr", "transfer", "record"],
default="voicemail", help="Which service to run")
parser.add_argument("--host", default="0.0.0.0", help="Host to bind to")
parser.add_argument("--port", type=int, default=3000, help="Port to bind to")
parser.add_argument("--show-swml", action="store_true", help="Show SWML document only, don't start server")
parser.add_argument("--suppress-logs", action="store_true", help="Suppress structured logs")
args = parser.parse_args()
# Set log level based on suppress-logs flag
if args.suppress_logs:
logging.getLogger().setLevel(logging.WARNING)
# Create the selected service
if args.service == "voicemail":
service = VoicemailService(host=args.host, port=args.port)
elif args.service == "ivr":
service = IvrMenuService(host=args.host, port=args.port)
elif args.service == "transfer":
service = CallTransferService(host=args.host, port=args.port)
elif args.service == "record":
service = CallRecordingService(host=args.host, port=args.port)
# Either show the SWML or start the server
if args.show_swml:
swml_doc = service.render_document()
logger.info("displaying_swml_document", service=args.service, size=len(swml_doc))
print(json.dumps(json.loads(swml_doc), indent=2))
else:
# Get auth credentials
username, password = service.get_basic_auth_credentials()
logger.info("starting_service",
service=args.service,
url=f"http://{args.host}:{args.port}{service.route}",
username=username,
password_length=len(password))
print(f"Starting {args.service} service on http://{args.host}:{args.port}{service.route}")
print(f"Basic Auth: {username}:{password}")
print("\nYou can access this service via:")
print(f" - GET http://{args.host}:{args.port}{service.route}")
print(f" - POST http://{args.host}:{args.port}{service.route}")
print(f" - GET http://{args.host}:{args.port}{service.route}/")
print(f" - POST http://{args.host}:{args.port}{service.route}/")
print("\nAll endpoints respond with the same SWML document.")
# Start the server
try:
service.run(host=args.host, port=args.port)
except KeyboardInterrupt:
logger.info("server_shutdown")
print("\nShutting down...")
if __name__ == "__main__":
main()