// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using System.Text;
using Migrations.OldActions;
using Squidex.Domain.Apps.Core.Rules.Deprecated;
using Squidex.Flows;
using Squidex.Flows.Steps.Utils;
using Squidex.Infrastructure.Reflection;
using Squidex.Infrastructure.Validation;
namespace Squidex.Extensions.Actions.Slack;
[FlowStep(
Title = "Slack",
IconImage = "",
IconColor = "#5c3a58",
Display = "Send to Slack",
Description = "Create a status update to a slack channel.",
ReadMore = "https://slack.com")]
#pragma warning disable CS0618 // Type or member is obsolete
public sealed record SlackFlowStep : FlowStep, IConvertibleToAction
#pragma warning restore CS0618 // Type or member is obsolete
{
[AbsoluteUrl]
[LocalizedRequired]
[Display(Name = "Webhook Url", Description = "The slack webhook url.")]
[Editor(FlowStepEditor.Text)]
public Uri WebhookUrl { get; set; }
[LocalizedRequired]
[Display(Name = "Text", Description = "The text that is sent as message to slack.")]
[Editor(FlowStepEditor.TextArea)]
[Expression]
public string Text { get; set; }
public override async ValueTask ExecuteAsync(FlowExecutionContext executionContext,
CancellationToken ct)
{
var body = new { text = Text };
var jsonRequest = executionContext.SerializeJson(body);
var request = new HttpRequestMessage(HttpMethod.Post, WebhookUrl)
{
Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"),
};
if (executionContext.IsSimulation)
{
executionContext.LogSkipSimulation(
HttpDumpFormatter.BuildDump(request, null, null));
return Next();
}
var httpClient =
executionContext.Resolve()
.CreateClient("SlackAction");
var (_, dump) = await httpClient.SendAsync(executionContext, request, jsonRequest, ct);
executionContext.Log("Notification sent", dump);
return Next();
}
#pragma warning disable CS0618 // Type or member is obsolete
public RuleAction ToAction()
{
return SimpleMapper.Map(this, new SlackAction());
}
#pragma warning restore CS0618 // Type or member is obsolete
}