-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCustomerServiceFunctions.cs
More file actions
464 lines (421 loc) · 17 KB
/
CustomerServiceFunctions.cs
File metadata and controls
464 lines (421 loc) · 17 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using static Azure.AI.VoiceLive.Samples.FunctionModels;
using static Azure.AI.VoiceLive.Samples.SampleData;
namespace Azure.AI.VoiceLive.Samples;
/// <summary>
/// Interface for customer service function execution.
/// </summary>
public interface ICustomerServiceFunctions
{
/// <summary>
/// Executes a function by name with JSON arguments.
/// </summary>
/// <param name="functionName"></param>
/// <param name="argumentsJson"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<object> ExecuteFunctionAsync(string functionName, string argumentsJson, CancellationToken cancellationToken = default);
}
/// <summary>
/// Implementation of customer service functions with mock data for demonstration.
/// In a real implementation, these would connect to actual databases and services.
/// </summary>
public class CustomerServiceFunctions : ICustomerServiceFunctions
{
private readonly ILogger<CustomerServiceFunctions> _logger;
private readonly Dictionary<string, Order> _orders;
private readonly Dictionary<string, Customer> _customers;
private readonly List<SupportTicket> _supportTickets;
/// <summary>
/// Constructor for CustomerServiceFunctions.
/// </summary>
/// <param name="logger"></param>
/// <exception cref="ArgumentNullException"></exception>
public CustomerServiceFunctions(ILogger<CustomerServiceFunctions> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
// Initialize sample data
_orders = InitializeSampleOrders();
_customers = InitializeSampleCustomers();
_supportTickets = new List<SupportTicket>();
}
/// <summary>
/// Execute a function by name with JSON arguments.
/// </summary>
public async Task<object> ExecuteFunctionAsync(string functionName, string argumentsJson, CancellationToken cancellationToken = default)
{
try
{
_logger.LogInformation("Executing function: {FunctionName} with arguments: {Arguments}", functionName, argumentsJson);
return functionName switch
{
"check_order_status" => await CheckOrderStatusAsync(argumentsJson, cancellationToken).ConfigureAwait(false),
"get_customer_info" => await GetCustomerInfoAsync(argumentsJson, cancellationToken).ConfigureAwait(false),
"schedule_support_call" => await ScheduleSupportCallAsync(argumentsJson, cancellationToken).ConfigureAwait(false),
"initiate_return_process" => await InitiateReturnProcessAsync(argumentsJson, cancellationToken).ConfigureAwait(false),
"update_shipping_address" => await UpdateShippingAddressAsync(argumentsJson, cancellationToken).ConfigureAwait(false),
_ => new { success = false, error = $"Unknown function: {functionName}" }
};
}
catch (Exception ex)
{
_logger.LogError(ex, "Error executing function {FunctionName}", functionName);
return new { success = false, error = "Internal error occurred while processing your request." };
}
}
private async Task<object> CheckOrderStatusAsync(string argumentsJson, CancellationToken cancellationToken)
{
var args = JsonSerializer.Deserialize<CheckOrderStatusArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (args == null)
{
return new { success = false, message = "Invalid arguments provided." };
}
// Simulate async database lookup
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
if (!_orders.TryGetValue(args.order_number, out var order))
{
return new
{
success = false,
message = "Order not found. Please verify the order number and try again."
};
}
return new
{
success = true,
order = new
{
number = order.OrderNumber,
status = order.Status,
total = order.TotalAmount,
items = order.Items.Select(item => new
{
name = item.ProductName,
quantity = item.Quantity,
status = item.Status,
price = item.Price
}),
estimated_delivery = order.EstimatedDelivery?.ToString("yyyy-MM-dd"),
tracking = order.TrackingNumber,
order_date = order.CreatedAt.ToString("yyyy-MM-dd")
}
};
}
private async Task<object> GetCustomerInfoAsync(string argumentsJson, CancellationToken cancellationToken)
{
var args = JsonSerializer.Deserialize<GetCustomerInfoArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (args == null)
{
return new { success = false, message = "Invalid arguments provided." };
}
// Simulate async database lookup
await Task.Delay(150, cancellationToken).ConfigureAwait(false);
var customer = _customers.Values.FirstOrDefault(c =>
c.CustomerId == args.customer_id ||
c.Email.Equals(args.customer_id, StringComparison.OrdinalIgnoreCase));
if (customer == null)
{
return new
{
success = false,
message = "Customer account not found. Please verify the customer ID or email address."
};
}
var result = new
{
success = true,
customer = new
{
id = customer.CustomerId,
name = customer.Name,
email = customer.Email,
phone = customer.Phone,
tier = customer.Tier,
joined_date = customer.CreatedAt.ToString("yyyy-MM-dd")
}
};
if (args.include_history)
{
var customerOrders = _orders.Values
.Where(o => o.CustomerId == customer.CustomerId)
.OrderByDescending(o => o.CreatedAt)
.Take(5)
.Select(order => new
{
number = order.OrderNumber,
date = order.CreatedAt.ToString("yyyy-MM-dd"),
total = order.TotalAmount,
status = order.Status
});
return new
{
result.success,
customer = new
{
result.customer.id,
result.customer.name,
result.customer.email,
result.customer.phone,
result.customer.tier,
result.customer.joined_date,
recent_orders = customerOrders
}
};
}
return result;
}
private async Task<object> ScheduleSupportCallAsync(string argumentsJson, CancellationToken cancellationToken)
{
var args = JsonSerializer.Deserialize<ScheduleSupportCallArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (args == null)
{
return new { success = false, message = "Invalid arguments provided." };
}
// Validate customer exists
var customer = _customers.Values.FirstOrDefault(c =>
c.CustomerId == args.customer_id ||
c.Email.Equals(args.customer_id, StringComparison.OrdinalIgnoreCase));
if (customer == null)
{
return new
{
success = false,
message = "Customer not found. Please verify the customer ID."
};
}
// Simulate async scheduling system
await Task.Delay(200, cancellationToken).ConfigureAwait(false);
// Parse preferred time or use default
DateTime scheduledTime;
if (!string.IsNullOrEmpty(args.preferred_time) && DateTime.TryParse(args.preferred_time, out scheduledTime))
{
// Use provided time
}
else
{
// Default to next business day at 10 AM
scheduledTime = DateTime.Today.AddDays(1);
if (scheduledTime.DayOfWeek == DayOfWeek.Saturday)
scheduledTime = scheduledTime.AddDays(2);
if (scheduledTime.DayOfWeek == DayOfWeek.Sunday)
scheduledTime = scheduledTime.AddDays(1);
scheduledTime = scheduledTime.AddHours(10);
}
var ticket = new SupportTicket
{
TicketId = $"TICKET-{DateTime.Now:yyyyMMdd}-{Random.Shared.Next(1000, 9999)}",
CustomerId = customer.CustomerId,
Category = args.issue_category,
Urgency = args.urgency,
Description = args.description,
ScheduledTime = scheduledTime,
Status = "Scheduled"
};
_supportTickets.Add(ticket);
return new
{
success = true,
ticket = new
{
ticket_id = ticket.TicketId,
customer_name = customer.Name,
scheduled_time = ticket.ScheduledTime.ToString("yyyy-MM-dd HH:mm"),
category = ticket.Category,
urgency = ticket.Urgency,
description = ticket.Description,
status = ticket.Status
},
message = $"Support call scheduled for {ticket.ScheduledTime:yyyy-MM-dd HH:mm}. You will receive a confirmation email shortly."
};
}
private async Task<object> InitiateReturnProcessAsync(string argumentsJson, CancellationToken cancellationToken)
{
var args = JsonSerializer.Deserialize<InitiateReturnProcessArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (args == null)
{
return new { success = false, message = "Invalid arguments provided." };
}
// Simulate async database lookup
await Task.Delay(150, cancellationToken).ConfigureAwait(false);
if (!_orders.TryGetValue(args.order_number, out var order))
{
return new
{
success = false,
message = "Order not found. Please verify the order number."
};
}
var item = order.Items.FirstOrDefault(i => i.ProductId == args.product_id);
if (item == null)
{
return new
{
success = false,
message = "Product not found in this order."
};
}
// Check if return is eligible (within 30 days and not already returned)
if (order.CreatedAt < DateTime.Now.AddDays(-30))
{
return new
{
success = false,
message = "This order is outside the 30-day return window."
};
}
if (item.Status == "Returned")
{
return new
{
success = false,
message = "This item has already been returned."
};
}
var returnId = $"RTN-{DateTime.Now:yyyyMMdd}-{Random.Shared.Next(1000, 9999)}";
return new
{
success = true,
return_info = new
{
return_id = returnId,
order_number = order.OrderNumber,
product_name = item.ProductName,
return_type = args.return_type,
reason = args.reason,
refund_amount = args.return_type == "refund" ? item.Price : 0,
estimated_processing = "3-5 business days",
return_label_url = $"https://returns.techcorp.com/label/{returnId}"
},
message = "Return request initiated successfully. You will receive a return shipping label via email within 24 hours."
};
}
private async Task<object> UpdateShippingAddressAsync(string argumentsJson, CancellationToken cancellationToken)
{
var args = JsonSerializer.Deserialize<UpdateShippingAddressArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (args == null)
{
return new { success = false, message = "Invalid arguments provided." };
}
// Simulate async database lookup
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
if (!_orders.TryGetValue(args.order_number, out var order))
{
return new
{
success = false,
message = "Order not found. Please verify the order number."
};
}
// Check if order can be modified
if (order.Status == "Delivered" || order.Status == "Shipped")
{
return new
{
success = false,
message = $"Cannot update address for {order.Status.ToLower()} orders."
};
}
return new
{
success = true,
updated_order = new
{
order_number = order.OrderNumber,
status = order.Status,
new_shipping_address = new
{
args.new_address.street,
args.new_address.city,
args.new_address.state,
args.new_address.zip_code,
args.new_address.country
},
estimated_delivery = order.EstimatedDelivery?.AddDays(1).ToString("yyyy-MM-dd") // Adjust delivery date
},
message = "Shipping address updated successfully. Your estimated delivery date may have changed."
};
}
private Dictionary<string, Order> InitializeSampleOrders()
{
return new Dictionary<string, Order>
{
["ORD-2024-001"] = new Order
{
OrderNumber = "ORD-2024-001",
Status = "Processing",
TotalAmount = 299.99m,
CustomerId = "CUST-001",
CreatedAt = DateTime.Now.AddDays(-2),
EstimatedDelivery = DateTime.Now.AddDays(3),
TrackingNumber = "1Z999AA1234567890",
Items = new List<OrderItem>
{
new() { ProductId = "LAPTOP-001", ProductName = "TechCorp Laptop Pro", Quantity = 1, Status = "Processing", Price = 299.99m }
}
},
["ORD-2024-002"] = new Order
{
OrderNumber = "ORD-2024-002",
Status = "Shipped",
TotalAmount = 159.98m,
CustomerId = "CUST-002",
CreatedAt = DateTime.Now.AddDays(-5),
EstimatedDelivery = DateTime.Now.AddDays(1),
TrackingNumber = "1Z999AA1234567891",
Items = new List<OrderItem>
{
new() { ProductId = "MOUSE-001", ProductName = "Wireless Gaming Mouse", Quantity = 1, Status = "Shipped", Price = 79.99m },
new() { ProductId = "PAD-001", ProductName = "Gaming Mouse Pad", Quantity = 1, Status = "Shipped", Price = 79.99m }
}
},
["ORD-2024-003"] = new Order
{
OrderNumber = "ORD-2024-003",
Status = "Delivered",
TotalAmount = 499.99m,
CustomerId = "CUST-001",
CreatedAt = DateTime.Now.AddDays(-10),
EstimatedDelivery = DateTime.Now.AddDays(-3),
TrackingNumber = "1Z999AA1234567892",
Items = new List<OrderItem>
{
new() { ProductId = "MONITOR-001", ProductName = "4K Gaming Monitor", Quantity = 1, Status = "Delivered", Price = 499.99m }
}
}
};
}
private Dictionary<string, Customer> InitializeSampleCustomers()
{
return new Dictionary<string, Customer>
{
["CUST-001"] = new Customer
{
CustomerId = "CUST-001",
Name = "John Smith",
Email = "john.smith@email.com",
Phone = "+1-555-0123",
Tier = "Gold",
CreatedAt = DateTime.Now.AddMonths(-8)
},
["CUST-002"] = new Customer
{
CustomerId = "CUST-002",
Name = "Sarah Johnson",
Email = "sarah.johnson@email.com",
Phone = "+1-555-0124",
Tier = "Silver",
CreatedAt = DateTime.Now.AddMonths(-3)
},
["CUST-003"] = new Customer
{
CustomerId = "CUST-003",
Name = "Mike Davis",
Email = "mike.davis@email.com",
Phone = "+1-555-0125",
Tier = "Standard",
CreatedAt = DateTime.Now.AddMonths(-1)
}
};
}
}