Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Slackbot.Net.Endpoints.Models.Events;

namespace Slackbot.Net.Endpoints.Abstractions;

public interface IHandleTeamJoin
{
Task<EventHandledResponse> Handle(EventMetaData eventMetadata, TeamJoinEvent memberjoined);
}
1 change: 1 addition & 0 deletions source/src/Slackbot.Net.Endpoints/EventTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public static class EventTypes
public const string AppMention = "app_mention";
public const string MemberJoinedChannel = "member_joined_channel";
public const string AppHomeOpened = "app_home_opened";
public const string TeamJoin = "team_join";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static IApplicationBuilder UseSlackbot(this IApplicationBuilder app, bool
app.MapWhen(MemberJoinedEvents.ShouldRun, b => b.UseMiddleware<MemberJoinedEvents>());
app.MapWhen(AppHomeOpenedEvents.ShouldRun, b => b.UseMiddleware<AppHomeOpenedEvents>());
app.MapWhen(InteractiveEvents.ShouldRun, b => b.UseMiddleware<InteractiveEvents>());
app.MapWhen(TeamJoinEvents.ShouldRun, b => b.UseMiddleware<TeamJoinEvents>());

return app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public ISlackbotHandlersBuilder AddInteractiveBlockActionsHandler<T>()
public ISlackbotHandlersBuilder AddNoOpAppMentionHandler<T>() where T : class, INoOpAppMentions;

public ISlackbotHandlersBuilder AddMessageActionsHandler<T>() where T : class, IHandleMessageActions;
public ISlackbotHandlersBuilder AddTeamJoinHandler<T>() where T : class, IHandleTeamJoin;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ public ISlackbotHandlersBuilder AddMessageActionsHandler<T>() where T : class, I
services.AddSingleton<IHandleMessageActions, T>();
return this;
}

public ISlackbotHandlersBuilder AddTeamJoinHandler<T>() where T : class, IHandleTeamJoin
{
services.AddSingleton<IHandleTeamJoin, T>();
return this;
}
}
46 changes: 46 additions & 0 deletions source/src/Slackbot.Net.Endpoints/Middlewares/TeamJoinEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Slackbot.Net.Endpoints.Abstractions;
using Slackbot.Net.Endpoints.Models.Events;

namespace Slackbot.Net.Endpoints.Middlewares;

internal class TeamJoinEvents(
ILogger<TeamJoinEvents> logger,
IEnumerable<IHandleTeamJoin> responseHandlers
)
{
public async Task Invoke(HttpContext context)
{
var teamJoinEvent = (TeamJoinEvent)context.Items[HttpItemKeys.SlackEventKey];
var metadata = (EventMetaData)context.Items[HttpItemKeys.EventMetadataKey];
var handler = responseHandlers.FirstOrDefault();

if (handler == null)
{
logger.LogError("No handler registered for IHandleTeamJoinEvents");
}
else
{
logger.LogInformation("Handling using {HandlerType}", handler.GetType());
try
{
logger.LogInformation("Handling using {HandlerType}", handler.GetType());
var response = await handler.Handle(metadata, teamJoinEvent);
logger.LogInformation("Handler response: {Response}", response.Response);
}
catch (Exception e)
{
logger.LogError(e, e.Message);
}
}

context.Response.StatusCode = 200;
}

public static bool ShouldRun(HttpContext ctx)
{
return ctx.Items.ContainsKey(HttpItemKeys.EventTypeKey)
&& ctx.Items[HttpItemKeys.EventTypeKey].ToString() == EventTypes.TeamJoin;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Slackbot.Net.Endpoints.Models.Events;

public class TeamJoinEvent : SlackEvent
{
public string User { get; set; }
}
22 changes: 17 additions & 5 deletions source/src/Slackbot.Net.Shared/BlockElements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class ContextBlock : IBlock

public class InputBlock : IBlock
{
public string type { get; set;} = BlockTypes.Input;
public string block_id { get; set; }
public string type { get; set; } = BlockTypes.Input;
public IElement element { get; set; }
public Text label { get; set; }
public bool dispatch_action { get; set; }
Expand Down Expand Up @@ -175,16 +176,26 @@ public class OverflowElement : IElement

public class DatePickerElement : IElement
{
public string type { get; set;} = ElementTypes.DatePicker;
public string type { get; set; } = ElementTypes.DatePicker;
public string action_id { get; set; }
public Text placeholder { get; set; }
public string initial_date { get; set; }
public Confirm confirm { get; set; }
}

public class RadioButtonsElement : IElement
{
public string type { get; set; } = ElementTypes.RadioButtons;
public string action_id { get; set; }
public Option[] options { get; set; }
public string initial_option { get; set; }
public Confirm confirm { get; set; }
public bool focus_on_load { get; set; }
}

public class PlainTextElement : IElement
{
public string type { get; set;} = ElementTypes.PlainTextInput;
public string type { get; set; } = ElementTypes.PlainTextInput;
public string action_id { get; set; }
public Text placeholder { get; set; }
}
Expand Down Expand Up @@ -223,9 +234,10 @@ public static class ElementTypes
public const string Overflow = "overflow";
public const string DatePicker = "datepicker";
public const string PlainTextInput = "plain_text_input";
public const string RadioButtons = "radio_buttons";
}

public interface IHaveType { string type { get; set; } }

public interface IElement : IHaveType { }
public interface IBlock : IHaveType { }
public interface IBlock : IHaveType { }