-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauto_vivified_example.py
More file actions
391 lines (323 loc) · 14.3 KB
/
Copy pathauto_vivified_example.py
File metadata and controls
391 lines (323 loc) · 14.3 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
#!/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.
"""
"""
Auto-Vivified SWML Service Example
This example demonstrates using the auto-vivification feature of SWMLService
to directly call verb methods instead of using add_verb. This provides a cleaner
and more intuitive API for building SWML documents.
Example: service.play(url="say:Hello") instead of service.add_verb("play", {"url": "say:Hello"})
"""
import os
import sys
import json
import argparse
import logging
import types
# Import structlog for logger instance creation
import structlog
# Import the SWMLService class
from signalwire.core.swml_service import SWMLService
# Create structured logger for this example
logger = structlog.get_logger("auto_vivified")
class VoicemailService(SWMLService):
"""
Simple voicemail service using auto-vivified verb methods
"""
def __init__(self, host="0.0.0.0", port=3000):
super().__init__(
name="voicemail",
route="/voicemail",
host=host,
port=port
)
# Debug: Print available verbs and schema details
verb_names = self.schema_utils.get_all_verb_names()
print(f"Available verbs: {verb_names}")
print(f"Schema path: {self.schema_utils.schema_path}")
print(f"Schema loaded: {bool(self.schema_utils.schema)}")
print(f"Number of verbs extracted: {len(self.schema_utils.verbs)}")
# Explicit testing of auto-vivification
print("\n--- Testing Auto-Vivification Mechanism ---")
# Try normal attribute access to see if __getattr__ gets triggered
try:
print("\nAttempting normal attribute access for 'play'...")
print(f"Before getattr - 'play' in dir(self): {'play' in dir(self)}")
play_method = getattr(self, "play", None)
print(f"getattr result: {play_method}")
print(f"After getattr - 'play' in dir(self): {'play' in dir(self)}")
print(f"hasattr(self, 'play'): {hasattr(self, 'play')}")
except Exception as e:
print(f"Error in getattr: {type(e)}: {e}")
# Examine the __dict__ to see if methods are being added
print("\nExamining instance __dict__:")
print(f"'play' in self.__dict__: {'play' in self.__dict__}")
print(f"'_verb_methods_cache' in self.__dict__: {'_verb_methods_cache' in self.__dict__}")
if hasattr(self, '_verb_methods_cache'):
print(f"Cache contents: {list(self._verb_methods_cache.keys())}")
print("--- End of Auto-Vivification Testing ---\n")
# Build the SWML document
self.build_voicemail_document()
def build_voicemail_document(self):
"""Build the voicemail SWML document using auto-vivified methods"""
# Reset the document
self.reset_document()
# Add answer verb - temporarily using add_verb
self.add_answer_verb()
# Add play verb for greeting - try using auto-vivified method
try:
print("Attempting to use auto-vivified play() method...")
self.play(url="say:Hello, you've reached the voicemail service. Please leave a message after the beep.")
print("Successfully used auto-vivified play() method")
except Exception as e:
print(f"Error using auto-vivified method: {type(e)}: {e}")
# Fallback to add_verb
self.add_verb("play", {
"url": "say:Hello, you've reached the voicemail service. Please leave a message after the beep."
})
print("Fell back to add_verb")
# Add a small pause - try using auto-vivified method
try:
print("Attempting to use auto-vivified sleep() method...")
self.sleep(1000) # 1 second in milliseconds
print("Successfully used auto-vivified sleep() method")
except Exception as e:
print(f"Error using auto-vivified method: {type(e)}: {e}")
# Fallback to add_verb
self.add_verb("sleep", 1000)
print("Fell back to add_verb")
# Play a beep - try using auto-vivified method
try:
print("Attempting to use auto-vivified play() method for beep...")
self.play(url="https://example.com/beep.wav")
print("Successfully used auto-vivified play() method")
except Exception as e:
print(f"Error using auto-vivified method: {type(e)}: {e}")
# Fallback to add_verb
self.add_verb("play", {
"url": "https://example.com/beep.wav"
})
print("Fell back to add_verb")
# Record the message - try using auto-vivified method
try:
print("Attempting to use auto-vivified record() method...")
self.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"
)
print("Successfully used auto-vivified record() method")
except Exception as e:
print(f"Error using auto-vivified method: {type(e)}: {e}")
# Fallback to add_verb
self.add_verb("record", {
"format": "mp3",
"stereo": False,
"beep": False,
"max_length": 120,
"terminators": "#",
"status_url": "https://example.com/voicemail-status"
})
print("Fell back to add_verb")
# Thank the caller - try using auto-vivified method
try:
print("Attempting to use auto-vivified play() method for thank you...")
self.play(url="say:Thank you for your message. Goodbye!")
print("Successfully used auto-vivified play() method")
except Exception as e:
print(f"Error using auto-vivified method: {type(e)}: {e}")
# Fallback to add_verb
self.add_verb("play", {
"url": "say:Thank you for your message. Goodbye!"
})
print("Fell back to add_verb")
# Hang up - use the add_verb method
self.add_verb("hangup", {})
self.log.debug("voicemail_document_built")
class IvrMenuService(SWMLService):
"""
Interactive Voice Response (IVR) menu system using auto-vivified verb methods
"""
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 using auto-vivified methods"""
# 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")
# Add play and connect verbs to sales section
self.add_verb_to_section("sales", "play", {"url": "say:Connecting you to sales. Please hold."})
self.add_verb_to_section("sales", "connect", {"to": "+15551234567"})
# Add support section
self.add_section("support")
self.log.debug("adding_section", section="support")
# Add play and connect verbs to support section
self.add_verb_to_section("support", "play", {"url": "say:Connecting you to support. Please hold."})
self.add_verb_to_section("support", "connect", {"to": "+15557654321"})
# Add voicemail section
self.add_section("voicemail")
self.log.debug("adding_section", section="voicemail")
# Add verbs to voicemail section
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,
"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 using auto-vivified verb methods
"""
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 using auto-vivified methods"""
# 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", # Using "from" directly in the config
"timeout": 30,
"answer_on_bridge": True,
"ringback": ["ring:us"],
"parallel": [
{"to": "+15552223333"},
{"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,
"terminators": "#"
})
# 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")
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description="Auto-Vivified SWML Service Examples")
parser.add_argument("--service", choices=["voicemail", "ivr", "transfer"],
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)
# 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}")
# 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()