Skip to content

Commit 16878b1

Browse files
authored
Merge pull request #1037 from yileicn/master
optimize HooKEmitter
2 parents cf4ea9c + 46ad042 commit 16878b1

3 files changed

Lines changed: 61 additions & 65 deletions

File tree

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
namespace BotSharp.Abstraction.Infrastructures;
22

3-
public class HookEmitOption
3+
public class HookEmitOption<T>
44
{
55
public bool OnlyOnce { get; set; }
6+
7+
/// <summary>
8+
/// Optional predicate to determine if the hook action should be executed for a specific hook instance.
9+
/// </summary>
10+
public Func<T, bool>? ShouldExecute { get; set; }
611
}

src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BotSharp.Core.Infrastructures;
44

55
public static class HookEmitter
66
{
7-
public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> action, HookEmitOption? option = null)
7+
public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> action, HookEmitOption<T>? option = null)
88
{
99
var logger = services.GetRequiredService<ILogger<T>>();
1010
var result = new HookEmittedResult();
@@ -15,12 +15,15 @@ public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> act
1515
{
1616
try
1717
{
18-
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
19-
action(hook);
20-
21-
if (option.OnlyOnce)
18+
if (option.ShouldExecute == null || option.ShouldExecute(hook))
2219
{
23-
break;
20+
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
21+
action(hook);
22+
23+
if (option.OnlyOnce)
24+
{
25+
break;
26+
}
2427
}
2528
}
2629
catch (Exception ex)
@@ -32,7 +35,7 @@ public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> act
3235
return result;
3336
}
3437

35-
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Func<T, Task> action, HookEmitOption? option = null)
38+
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Func<T, Task> action, HookEmitOption<T>? option = null)
3639
{
3740
var logger = services.GetRequiredService<ILogger<T>>();
3841
var result = new HookEmittedResult();
@@ -43,12 +46,15 @@ public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, F
4346
{
4447
try
4548
{
46-
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
47-
await action(hook);
48-
49-
if (option.OnlyOnce)
49+
if (option.ShouldExecute == null || option.ShouldExecute(hook))
5050
{
51-
break;
51+
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
52+
await action(hook);
53+
54+
if (option.OnlyOnce)
55+
{
56+
break;
57+
}
5258
}
5359
}
5460
catch (Exception ex)

src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Mvc;
88
using Twilio.Http;
99
using Task = System.Threading.Tasks.Task;
10+
using BotSharp.Abstraction.Infrastructures;
1011

1112
namespace BotSharp.Plugin.Twilio.Controllers;
1213

@@ -341,62 +342,46 @@ public async Task<TwiMLResult> TransferCall(ConversationalVoiceRequest request)
341342
public async Task<ActionResult> PhoneCallStatus(ConversationalVoiceRequest request)
342343
{
343344
var twilio = _services.GetRequiredService<TwilioService>();
344-
if (request.CallStatus == "completed")
345-
{
346-
if (twilio.MachineDetected(request))
347-
{
348-
// voicemail
349-
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
350-
async hook =>
351-
{
352-
if (hook.IsMatch(request)) await hook.OnVoicemailLeft(request);
353-
});
354-
}
355-
else
356-
{
357-
// phone call completed
358-
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
359-
async hook =>
360-
{
361-
if (hook.IsMatch(request)) await hook.OnUserDisconnected(request);
362-
});
363-
}
364-
}
365-
else if (request.CallStatus == "busy")
366-
{
367-
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
368-
async hook =>
369-
{
370-
if (hook.IsMatch(request)) await hook.OnCallBusyStatus(request);
371-
});
372-
}
373-
else if (request.CallStatus == "no-answer")
345+
346+
// Define the options with the predicate
347+
var emitOptions = new HookEmitOption<ITwilioCallStatusHook>
374348
{
375-
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
376-
async hook =>
377-
{
378-
if (hook.IsMatch(request)) await hook.OnCallNoAnswerStatus(request);
379-
});
380-
}
381-
else if (request.CallStatus == "canceled")
349+
ShouldExecute = hook => hook.IsMatch(request)
350+
};
351+
352+
switch (request.CallStatus)
382353
{
383-
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
384-
async hook =>
354+
case "completed":
355+
if (twilio.MachineDetected(request))
385356
{
386-
if (hook.IsMatch(request)) await hook.OnCallCanceledStatus(request);
387-
});
388-
}
389-
else if (request.CallStatus == "failed")
390-
{
391-
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
392-
async hook =>
357+
// voicemail
358+
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnVoicemailLeft(request), emitOptions);
359+
}
360+
else
393361
{
394-
if (hook.IsMatch(request)) await hook.OnCallFailedStatus(request);
395-
});
396-
}
397-
else
398-
{
399-
_logger.LogError($"Unknown call status: {request.CallStatus}, {request.CallSid}");
362+
// phone call completed
363+
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnUserDisconnected(request), emitOptions);
364+
}
365+
break;
366+
367+
case "busy":
368+
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallBusyStatus(request), emitOptions);
369+
break;
370+
371+
case "no-answer":
372+
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallNoAnswerStatus(request), emitOptions);
373+
break;
374+
375+
case "canceled":
376+
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallCanceledStatus(request), emitOptions);
377+
break;
378+
379+
case "failed":
380+
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallFailedStatus(request), emitOptions);
381+
break;
382+
default:
383+
_logger.LogError($"Unknown call status: {request.CallStatus}, {request.CallSid}");
384+
break;
400385
}
401386

402387
return Ok();

0 commit comments

Comments
 (0)