-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamic_swml_service.py
More file actions
412 lines (334 loc) · 14.4 KB
/
Copy pathdynamic_swml_service.py
File metadata and controls
412 lines (334 loc) · 14.4 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
402
403
404
405
406
407
408
409
410
411
412
#!/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.
"""
"""
Dynamic SWML Service Example
This example demonstrates creating a SWML service that generates
different responses based on POST data. It shows how to use the
on_request() method to customize SWML documents dynamically.
"""
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
# Create structured logger for this example
logger = structlog.get_logger("dynamic_swml")
class DynamicGreetingService(SWMLService):
"""
A service that customizes its greeting based on POST data
"""
def __init__(self, host="0.0.0.0", port=3000):
super().__init__(
name="dynamic-greeting",
route="/greeting",
host=host,
port=port
)
# Build the default SWML document
self.build_default_document()
def build_default_document(self):
"""Build the default SWML document with a generic greeting"""
# Reset the document
self.reset_document()
# Add answer verb
self.add_answer_verb()
# Add play verb for generic greeting
self.add_verb("play", {
"url": "say:Hello, thank you for calling our service."
})
# Add a generic menu prompt
self.add_verb("prompt", {
"play": "say:Please press 1 for sales, 2 for support, or 3 to leave a message.",
"max_digits": 1,
"terminators": "#"
})
# Add hang up
self.add_verb("hangup", {})
self.log.debug("default_document_built")
def on_request(self, request_data: dict = None) -> dict:
"""
Customize the SWML document based on the request data
Args:
request_data: Dictionary containing the parsed POST body
Returns:
Modified SWML document or None to use the default
"""
# If there's no request data, use the default document
if not request_data:
self.log.debug("no_request_data_using_default")
return None
self.log.debug("customizing_document",
caller_name=request_data.get("caller_name"),
caller_type=request_data.get("caller_type"),
department=request_data.get("department"))
# Reset the document to start fresh
self.reset_document()
# Add answer verb
self.add_answer_verb()
# Customize greeting based on caller_name if provided
caller_name = request_data.get("caller_name")
if caller_name:
self.add_verb("play", {
"url": f"say:Hello {caller_name}, welcome back to our service!"
})
else:
self.add_verb("play", {
"url": "say:Hello, thank you for calling our service."
})
# Customize menu based on caller_type
caller_type = request_data.get("caller_type", "").lower()
if caller_type == "vip":
# VIP callers get priority routing
self.add_verb("play", {
"url": "say:As a VIP customer, you'll be connected to our priority support team immediately."
})
# Connect to VIP support
self.add_verb("connect", {
"to": "+15551234567", # VIP support number
"timeout": 30,
"answer_on_bridge": True
})
elif caller_type == "existing":
# Existing customers get customized options
self.add_verb("prompt", {
"play": "say:Please press 1 for account management, 2 for technical support, or 3 for billing.",
"max_digits": 1,
"terminators": "#"
})
elif caller_type == "new":
# New customers get sales-focused options
self.add_verb("prompt", {
"play": "say:Please press 1 to learn about our products, 2 to speak with a sales representative, or 3 to request a demo.",
"max_digits": 1,
"terminators": "#"
})
else:
# Generic options for unknown caller types
self.add_verb("prompt", {
"play": "say:Please press 1 for sales, 2 for support, or 3 to leave a message.",
"max_digits": 1,
"terminators": "#"
})
# Add dynamic routing based on department if specified
department = request_data.get("department", "").lower()
if department:
self.add_verb("play", {
"url": f"say:We'll connect you to our {department} department right away."
})
# Different phone numbers for different departments
phone_numbers = {
"sales": "+15551112222",
"support": "+15553334444",
"billing": "+15555556666",
"technical": "+15557778888"
}
# Connect to the appropriate department
to_number = phone_numbers.get(department, "+15559990000") # Default number as fallback
self.add_verb("connect", {
"to": to_number,
"timeout": 30,
"answer_on_bridge": True
})
# Add hang up as fallback
self.add_verb("hangup", {})
self.log.info("document_customized",
caller_type=caller_type,
department=department,
has_name=caller_name is not None)
# Return None to use the document we just built
return None # The document has already been modified
class CallRouterService(SWMLService):
"""
A service that routes calls based on POST data
"""
def __init__(self, host="0.0.0.0", port=3000):
super().__init__(
name="call-router",
route="/router",
host=host,
port=port
)
# Build the default SWML document
self.build_default_document()
def build_default_document(self):
"""Build the default SWML document with a fallback routing"""
# 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 an available agent."
})
# Add a fallback connection
self.add_verb("connect", {
"to": "+15551234567", # Fallback number
"timeout": 30
})
# Add hang up
self.add_verb("hangup", {})
self.log.debug("default_document_built")
def on_request(self, request_data: dict = None) -> dict:
"""
Route calls differently based on POST data
Args:
request_data: Dictionary containing the parsed POST body
Returns:
Modified SWML document or None to use the default
"""
# If there's no request data, use the default document
if not request_data:
self.log.debug("no_request_data_using_default")
return None
self.log.debug("customizing_routing",
region=request_data.get("region"),
high_volume=request_data.get("high_volume"),
queue_length=request_data.get("queue_length"),
callback_number=request_data.get("callback_number"))
# Create a new document
self.reset_document()
self.add_answer_verb()
# Check if this is a high-volume period
high_volume = request_data.get("high_volume", False)
# Get the caller's region
region = request_data.get("region", "").lower()
# Get agent queue status
queue_length = request_data.get("queue_length", 0)
# Check if this is a callback request
callback_number = request_data.get("callback_number")
# Handle callback requests
if callback_number:
self.log.info("processing_callback_request", callback_number=callback_number)
self.add_verb("play", {
"url": "say:We'll call you back at the number you provided. Thank you for your patience."
})
self.add_verb("hangup", {})
return None
# Inform caller if we're experiencing high volume
if high_volume or queue_length > 10:
self.log.info("high_volume_detected", queue_length=queue_length)
self.add_verb("play", {
"url": "say:We're currently experiencing higher than normal call volume. Your wait time may be extended."
})
# Offer callback option
self.add_verb("prompt", {
"play": "say:Press 1 to continue holding, or press 2 to receive a callback when an agent becomes available.",
"max_digits": 1
})
# Add conditional logic with switch
self.add_verb("switch", {
"variable": "prompt_digits",
"case": {
"2": [
{"play": {"url": "say:Please enter your 10-digit phone number followed by the pound key."}},
{"prompt": {"play": "silence:1", "max_digits": 10, "terminators": "#"}}
# In a real implementation, we'd have more logic here to handle the callback
]
},
"default": [
{"play": {"url": "say:Thank you for your patience. Please hold for the next available agent."}}
]
})
else:
self.add_verb("play", {
"url": "say:Thank you for calling. We'll connect you with an agent shortly."
})
# Route based on region if provided
if region:
self.log.info("routing_by_region", region=region)
region_numbers = {
"east": ["+15551112222", "+15551113333"],
"west": ["+15552223333", "+15552224444"],
"central": ["+15553334444", "+15553335555"],
"international": ["+15554445555"]
}
numbers = region_numbers.get(region, ["+15551234567"]) # Default to fallback
# If we have multiple numbers for the region, try them in parallel
if len(numbers) > 1:
self.log.debug("using_parallel_connection", numbers=numbers)
parallel_targets = [{"to": num} for num in numbers]
self.add_verb("connect", {
"parallel": parallel_targets,
"timeout": 30,
"answer_on_bridge": True
})
else:
# Just one number, connect directly
self.log.debug("using_direct_connection", number=numbers[0])
self.add_verb("connect", {
"to": numbers[0],
"timeout": 30,
"answer_on_bridge": True
})
else:
# No region specified, use default routing
self.log.info("using_default_routing")
self.add_verb("connect", {
"to": "+15551234567", # Default number
"timeout": 30
})
# Add fallback message and hangup
self.add_verb("play", {
"url": "say:We're sorry, but all of our agents are currently busy. Please try your call again later."
})
self.add_verb("hangup", {})
return None # The document has already been modified
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description="Dynamic SWML Service Examples")
parser.add_argument("--service", choices=["greeting", "router"],
default="greeting", 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("--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 == "greeting":
service = DynamicGreetingService(host=args.host, port=args.port)
elif args.service == "router":
service = CallRouterService(host=args.host, port=args.port)
# 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("\nFor dynamic behavior, send POST requests with JSON data like:")
if args.service == "greeting":
print(json.dumps({
"caller_name": "John Doe",
"caller_type": "vip",
"department": "technical"
}, indent=2))
elif args.service == "router":
print(json.dumps({
"region": "west",
"high_volume": True,
"queue_length": 15
}, indent=2))
# 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()