Skip to content

Commit ca7284f

Browse files
authored
Murdahl/add new relevant events (#14)
* Add Team Join event https://api.slack.com/events/team_join Update TeamJoinEvents.cs * Add Id to Block element input for input retention * Add RadioButtonElement https://api.slack.com/reference/block-kit/block-elements#radio
1 parent eba594b commit ca7284f

8 files changed

Lines changed: 86 additions & 5 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Slackbot.Net.Endpoints.Models.Events;
2+
3+
namespace Slackbot.Net.Endpoints.Abstractions;
4+
5+
public interface IHandleTeamJoin
6+
{
7+
Task<EventHandledResponse> Handle(EventMetaData eventMetadata, TeamJoinEvent memberjoined);
8+
}

source/src/Slackbot.Net.Endpoints/EventTypes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public static class EventTypes
77
public const string AppMention = "app_mention";
88
public const string MemberJoinedChannel = "member_joined_channel";
99
public const string AppHomeOpened = "app_home_opened";
10+
public const string TeamJoin = "team_join";
1011
}

source/src/Slackbot.Net.Endpoints/Hosting/IAppBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static IApplicationBuilder UseSlackbot(this IApplicationBuilder app, bool
1919
app.MapWhen(MemberJoinedEvents.ShouldRun, b => b.UseMiddleware<MemberJoinedEvents>());
2020
app.MapWhen(AppHomeOpenedEvents.ShouldRun, b => b.UseMiddleware<AppHomeOpenedEvents>());
2121
app.MapWhen(InteractiveEvents.ShouldRun, b => b.UseMiddleware<InteractiveEvents>());
22+
app.MapWhen(TeamJoinEvents.ShouldRun, b => b.UseMiddleware<TeamJoinEvents>());
2223

2324
return app;
2425
}

source/src/Slackbot.Net.Endpoints/Hosting/ISlackbotHandlersBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ public ISlackbotHandlersBuilder AddInteractiveBlockActionsHandler<T>()
1616
public ISlackbotHandlersBuilder AddNoOpAppMentionHandler<T>() where T : class, INoOpAppMentions;
1717

1818
public ISlackbotHandlersBuilder AddMessageActionsHandler<T>() where T : class, IHandleMessageActions;
19+
public ISlackbotHandlersBuilder AddTeamJoinHandler<T>() where T : class, IHandleTeamJoin;
1920
}

source/src/Slackbot.Net.Endpoints/Hosting/SlackBotHandlersBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ public ISlackbotHandlersBuilder AddMessageActionsHandler<T>() where T : class, I
5353
services.AddSingleton<IHandleMessageActions, T>();
5454
return this;
5555
}
56+
57+
public ISlackbotHandlersBuilder AddTeamJoinHandler<T>() where T : class, IHandleTeamJoin
58+
{
59+
services.AddSingleton<IHandleTeamJoin, T>();
60+
return this;
61+
}
5662
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.Extensions.Logging;
3+
using Slackbot.Net.Endpoints.Abstractions;
4+
using Slackbot.Net.Endpoints.Models.Events;
5+
6+
namespace Slackbot.Net.Endpoints.Middlewares;
7+
8+
internal class TeamJoinEvents(
9+
ILogger<TeamJoinEvents> logger,
10+
IEnumerable<IHandleTeamJoin> responseHandlers
11+
)
12+
{
13+
public async Task Invoke(HttpContext context)
14+
{
15+
var teamJoinEvent = (TeamJoinEvent)context.Items[HttpItemKeys.SlackEventKey];
16+
var metadata = (EventMetaData)context.Items[HttpItemKeys.EventMetadataKey];
17+
var handler = responseHandlers.FirstOrDefault();
18+
19+
if (handler == null)
20+
{
21+
logger.LogError("No handler registered for IHandleTeamJoinEvents");
22+
}
23+
else
24+
{
25+
logger.LogInformation("Handling using {HandlerType}", handler.GetType());
26+
try
27+
{
28+
logger.LogInformation("Handling using {HandlerType}", handler.GetType());
29+
var response = await handler.Handle(metadata, teamJoinEvent);
30+
logger.LogInformation("Handler response: {Response}", response.Response);
31+
}
32+
catch (Exception e)
33+
{
34+
logger.LogError(e, e.Message);
35+
}
36+
}
37+
38+
context.Response.StatusCode = 200;
39+
}
40+
41+
public static bool ShouldRun(HttpContext ctx)
42+
{
43+
return ctx.Items.ContainsKey(HttpItemKeys.EventTypeKey)
44+
&& ctx.Items[HttpItemKeys.EventTypeKey].ToString() == EventTypes.TeamJoin;
45+
}
46+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Slackbot.Net.Endpoints.Models.Events;
2+
3+
public class TeamJoinEvent : SlackEvent
4+
{
5+
public string User { get; set; }
6+
}

source/src/Slackbot.Net.Shared/BlockElements.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class ContextBlock : IBlock
3838

3939
public class InputBlock : IBlock
4040
{
41-
public string type { get; set;} = BlockTypes.Input;
41+
public string block_id { get; set; }
42+
public string type { get; set; } = BlockTypes.Input;
4243
public IElement element { get; set; }
4344
public Text label { get; set; }
4445
public bool dispatch_action { get; set; }
@@ -175,16 +176,26 @@ public class OverflowElement : IElement
175176

176177
public class DatePickerElement : IElement
177178
{
178-
public string type { get; set;} = ElementTypes.DatePicker;
179+
public string type { get; set; } = ElementTypes.DatePicker;
179180
public string action_id { get; set; }
180181
public Text placeholder { get; set; }
181182
public string initial_date { get; set; }
182183
public Confirm confirm { get; set; }
183184
}
184185

186+
public class RadioButtonsElement : IElement
187+
{
188+
public string type { get; set; } = ElementTypes.RadioButtons;
189+
public string action_id { get; set; }
190+
public Option[] options { get; set; }
191+
public string initial_option { get; set; }
192+
public Confirm confirm { get; set; }
193+
public bool focus_on_load { get; set; }
194+
}
195+
185196
public class PlainTextElement : IElement
186197
{
187-
public string type { get; set;} = ElementTypes.PlainTextInput;
198+
public string type { get; set; } = ElementTypes.PlainTextInput;
188199
public string action_id { get; set; }
189200
public Text placeholder { get; set; }
190201
}
@@ -223,9 +234,10 @@ public static class ElementTypes
223234
public const string Overflow = "overflow";
224235
public const string DatePicker = "datepicker";
225236
public const string PlainTextInput = "plain_text_input";
237+
public const string RadioButtons = "radio_buttons";
226238
}
227-
239+
228240
public interface IHaveType { string type { get; set; } }
229241

230242
public interface IElement : IHaveType { }
231-
public interface IBlock : IHaveType { }
243+
public interface IBlock : IHaveType { }

0 commit comments

Comments
 (0)