Minor cleanup and code polishing

This commit is contained in:
Sipke Schoorstra 2023-08-02 22:54:51 +02:00
parent fa9e3f13a0
commit 1c8ce5211d
10 changed files with 28 additions and 7 deletions

View file

@ -92,6 +92,7 @@ public abstract class AnswerCallBase : Activity<CallAnsweredPayload>, IBookmarks
/// <summary>
/// The call control ID to answer. Leave blank when the workflow is driven by an incoming call and you wish to pick up that one.
/// </summary>
[Input(DisplayName = "Call Control ID", Description = "The call control ID of the call to answer.", Category = "Advanced")]
public Input<string?>? CallControlId { get; set; }
/// <inheritdoc />

View file

@ -3,13 +3,22 @@ using Elsa.Telnyx.Models;
namespace Elsa.Telnyx.Events;
/// <summary>
/// Triggered when a Telnyx webhook is received.
/// </summary>
public class TelnyxWebhookReceived : INotification
{
/// <summary>
/// Initializes a new instance of the <see cref="TelnyxWebhookReceived"/> class.
/// </summary>
public TelnyxWebhookReceived(TelnyxWebhook webhook)
{
Webhook = webhook;
}
/// <summary>
/// Gets the webhook.
/// </summary>
public TelnyxWebhook Webhook { get; }
}

View file

@ -30,6 +30,7 @@ public static class DependencyInjectionExtensions
Action<IHttpClientBuilder>? configureHttpClientBuilder = default)
{
// Telnyx options.
configure ??= options => options.ApiUrl = new Uri("https://api.telnyx.com");
services.Configure(configure);
// Services.

View file

@ -1,4 +1,5 @@
using Elsa.Telnyx.Contracts;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
@ -10,6 +11,7 @@ namespace Elsa.Extensions;
/// <summary>
/// Provides extensions on <see cref="IEndpointRouteBuilder"/>
/// </summary>
[PublicAPI]
public static class EndpointsExtensions
{
/// <summary>

View file

@ -8,6 +8,7 @@ using Elsa.Telnyx.Payloads.Abstract;
using Elsa.Telnyx.Payloads.Call;
using Elsa.Workflows.Core.Helpers;
using Elsa.Workflows.Runtime.Contracts;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
namespace Elsa.Telnyx.Handlers;
@ -15,6 +16,7 @@ namespace Elsa.Telnyx.Handlers;
/// <summary>
/// Triggers all workflows starting with or blocked on a <see cref="IncomingCall"/> activity.
/// </summary>
[PublicAPI]
internal class TriggerBridgeCallActivities : INotificationHandler<TelnyxWebhookReceived>
{
private readonly IWorkflowRuntime _workflowRuntime;
@ -34,7 +36,7 @@ internal class TriggerBridgeCallActivities : INotificationHandler<TelnyxWebhookR
if (payload is not CallBridgedPayload callBridgedPayload)
return;
var correlationId = ((Payload)webhook.Data.Payload).GetCorrelationId();;
var correlationId = ((Payload)webhook.Data.Payload).GetCorrelationId();
var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<BridgeCalls>();
var input = new Dictionary<string, object>().AddInput(callBridgedPayload);
var callBridgedBookmarkPayload = new CallBridgedBookmarkPayload(callBridgedPayload.CallControlId);

View file

@ -8,6 +8,7 @@ using Elsa.Telnyx.Payloads.Abstract;
using Elsa.Telnyx.Payloads.Call;
using Elsa.Workflows.Core.Helpers;
using Elsa.Workflows.Runtime.Contracts;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
namespace Elsa.Telnyx.Handlers;
@ -15,6 +16,7 @@ namespace Elsa.Telnyx.Handlers;
/// <summary>
/// Triggers all workflows starting with or blocked on a <see cref="IncomingCall"/> activity.
/// </summary>
[PublicAPI]
internal class TriggerIncomingCallActivities : INotificationHandler<TelnyxWebhookReceived>
{
private readonly IWorkflowRuntime _workflowRuntime;
@ -37,7 +39,7 @@ internal class TriggerIncomingCallActivities : INotificationHandler<TelnyxWebhoo
if (callInitiatedPayload.Direction != "incoming")
return;
var correlationId = ((Payload)webhook.Data.Payload).GetCorrelationId();;
var correlationId = ((Payload)webhook.Data.Payload).GetCorrelationId();
var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<IncomingCall>();
var input = new Dictionary<string, object>().AddInput(webhook);

View file

@ -8,6 +8,7 @@ using Elsa.Telnyx.Events;
using Elsa.Telnyx.Extensions;
using Elsa.Telnyx.Payloads.Abstract;
using Elsa.Workflows.Runtime.Contracts;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
namespace Elsa.Telnyx.Handlers;
@ -15,6 +16,7 @@ namespace Elsa.Telnyx.Handlers;
/// <summary>
/// Triggers all workflows starting with or blocked on a <see cref="WebhookEvent"/> activity.
/// </summary>
[PublicAPI]
internal class TriggerWebhookActivities : INotificationHandler<TelnyxWebhookReceived>
{
private readonly IWorkflowRuntime _workflowRuntime;

View file

@ -8,12 +8,14 @@ using Elsa.Telnyx.Payloads.Abstract;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
using Elsa.Workflows.Runtime.Contracts;
using JetBrains.Annotations;
namespace Elsa.Telnyx.Handlers;
/// <summary>
/// Resumes all workflows blocked on activities that are waiting for a given webhook.
/// </summary>
[PublicAPI]
internal class TriggerWebhookDrivenActivities : INotificationHandler<TelnyxWebhookReceived>
{
private readonly IWorkflowRuntime _workflowRuntime;

View file

@ -8,7 +8,7 @@ internal static class PayloadSerializer
{
public static Payload Deserialize(string eventType, JsonElement dataModel, JsonSerializerOptions? options = null)
{
var payloadType = WebhookPayloadTypes.PayloadTypeDictionary.ContainsKey(eventType) ? WebhookPayloadTypes.PayloadTypeDictionary[eventType] : typeof(UnsupportedPayload);
var payloadType = WebhookPayloadTypes.PayloadTypeDictionary.TryGetValue(eventType, out var value) ? value : typeof(UnsupportedPayload);
return (Payload)dataModel.Deserialize(payloadType, options)!;
}
}

View file

@ -16,7 +16,7 @@ namespace Elsa.Telnyx.Services;
internal class WebhookHandler : IWebhookHandler
{
private static readonly JsonSerializerOptions SerializerSettings;
private readonly INotificationSender _mediator;
private readonly INotificationSender _notificationSender;
private readonly ILogger<WebhookHandler> _logger;
static WebhookHandler()
@ -24,9 +24,9 @@ internal class WebhookHandler : IWebhookHandler
SerializerSettings = CreateSerializerSettings();
}
public WebhookHandler(INotificationSender mediator, ILogger<WebhookHandler> logger)
public WebhookHandler(INotificationSender notificationSender, ILogger<WebhookHandler> logger)
{
_mediator = mediator;
_notificationSender = notificationSender;
_logger = logger;
}
@ -39,7 +39,7 @@ internal class WebhookHandler : IWebhookHandler
using var loggingScope = _logger.BeginScope(new Dictionary<string, object> { ["CorrelationId"] = correlationId });
_logger.LogDebug("Telnyx webhook payload received: {@Webhook}", webhook);
await _mediator.SendAsync(new TelnyxWebhookReceived(webhook), NotificationStrategy.Background, cancellationToken);
await _notificationSender.SendAsync(new TelnyxWebhookReceived(webhook), NotificationStrategy.Background, cancellationToken);
}
private static async Task<string> ReadRequestBodyAsync(HttpContext httpContext)