fix conflict
This commit is contained in:
commit
46ad042d32
|
|
@ -1,6 +1,11 @@
|
|||
namespace BotSharp.Abstraction.Infrastructures;
|
||||
|
||||
public class HookEmitOption
|
||||
public class HookEmitOption<T>
|
||||
{
|
||||
public bool OnlyOnce { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Optional predicate to determine if the hook action should be executed for a specific hook instance.
|
||||
/// </summary>
|
||||
public Func<T, bool>? ShouldExecute { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace BotSharp.Core.Infrastructures;
|
|||
|
||||
public static class HookEmitter
|
||||
{
|
||||
public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> action, HookEmitOption? option = null)
|
||||
public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> action, HookEmitOption<T>? option = null)
|
||||
{
|
||||
var logger = services.GetRequiredService<ILogger<T>>();
|
||||
var result = new HookEmittedResult();
|
||||
|
|
@ -14,6 +14,8 @@ public static class HookEmitter
|
|||
foreach (var hook in hooks)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (option.ShouldExecute == null || option.ShouldExecute(hook))
|
||||
{
|
||||
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
|
||||
action(hook);
|
||||
|
|
@ -23,6 +25,7 @@ public static class HookEmitter
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex.ToString());
|
||||
|
|
@ -32,7 +35,7 @@ public static class HookEmitter
|
|||
return result;
|
||||
}
|
||||
|
||||
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Func<T, Task> action, HookEmitOption? option = null)
|
||||
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Func<T, Task> action, HookEmitOption<T>? option = null)
|
||||
{
|
||||
var logger = services.GetRequiredService<ILogger<T>>();
|
||||
var result = new HookEmittedResult();
|
||||
|
|
@ -42,6 +45,8 @@ public static class HookEmitter
|
|||
foreach (var hook in hooks)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (option.ShouldExecute == null || option.ShouldExecute(hook))
|
||||
{
|
||||
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
|
||||
await action(hook);
|
||||
|
|
@ -51,6 +56,7 @@ public static class HookEmitter
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex.ToString());
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Http;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Twilio.Http;
|
||||
using Task = System.Threading.Tasks.Task;
|
||||
using BotSharp.Abstraction.Infrastructures;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.Controllers;
|
||||
|
||||
|
|
@ -341,62 +342,46 @@ public class TwilioVoiceController : TwilioController
|
|||
public async Task<ActionResult> PhoneCallStatus(ConversationalVoiceRequest request)
|
||||
{
|
||||
var twilio = _services.GetRequiredService<TwilioService>();
|
||||
if (request.CallStatus == "completed")
|
||||
|
||||
// Define the options with the predicate
|
||||
var emitOptions = new HookEmitOption<ITwilioCallStatusHook>
|
||||
{
|
||||
ShouldExecute = hook => hook.IsMatch(request)
|
||||
};
|
||||
|
||||
switch (request.CallStatus)
|
||||
{
|
||||
case "completed":
|
||||
if (twilio.MachineDetected(request))
|
||||
{
|
||||
// voicemail
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
|
||||
async hook =>
|
||||
{
|
||||
if (hook.IsMatch(request)) await hook.OnVoicemailLeft(request);
|
||||
});
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnVoicemailLeft(request), emitOptions);
|
||||
}
|
||||
else
|
||||
{
|
||||
// phone call completed
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
|
||||
async hook =>
|
||||
{
|
||||
if (hook.IsMatch(request)) await hook.OnUserDisconnected(request);
|
||||
});
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnUserDisconnected(request), emitOptions);
|
||||
}
|
||||
}
|
||||
else if (request.CallStatus == "busy")
|
||||
{
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
|
||||
async hook =>
|
||||
{
|
||||
if (hook.IsMatch(request)) await hook.OnCallBusyStatus(request);
|
||||
});
|
||||
}
|
||||
else if (request.CallStatus == "no-answer")
|
||||
{
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
|
||||
async hook =>
|
||||
{
|
||||
if (hook.IsMatch(request)) await hook.OnCallNoAnswerStatus(request);
|
||||
});
|
||||
}
|
||||
else if (request.CallStatus == "canceled")
|
||||
{
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
|
||||
async hook =>
|
||||
{
|
||||
if (hook.IsMatch(request)) await hook.OnCallCanceledStatus(request);
|
||||
});
|
||||
}
|
||||
else if (request.CallStatus == "failed")
|
||||
{
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
|
||||
async hook =>
|
||||
{
|
||||
if (hook.IsMatch(request)) await hook.OnCallFailedStatus(request);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
|
||||
case "busy":
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallBusyStatus(request), emitOptions);
|
||||
break;
|
||||
|
||||
case "no-answer":
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallNoAnswerStatus(request), emitOptions);
|
||||
break;
|
||||
|
||||
case "canceled":
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallCanceledStatus(request), emitOptions);
|
||||
break;
|
||||
|
||||
case "failed":
|
||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, hook => hook.OnCallFailedStatus(request), emitOptions);
|
||||
break;
|
||||
default:
|
||||
_logger.LogError($"Unknown call status: {request.CallStatus}, {request.CallSid}");
|
||||
break;
|
||||
}
|
||||
|
||||
return Ok();
|
||||
|
|
|
|||
Loading…
Reference in a new issue