Skip to content

Commit 76e14d8

Browse files
committed
Adds support for message_action
1 parent 129d2cc commit 76e14d8

8 files changed

Lines changed: 110 additions & 3 deletions

File tree

Samples/HelloWorld/Program.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
13
using Slackbot.Net.Endpoints.Hosting;
24
using Slackbot.Net.Endpoints.Authentication;
35
using Slackbot.Net.Endpoints.Abstractions;
46
using Slackbot.Net.Endpoints.Models.Events;
7+
using Slackbot.Net.Endpoints.Models.Interactive.MessageActions;
58

69
var builder = WebApplication.CreateBuilder(args);
710

@@ -11,7 +14,8 @@
1114

1215
// Setup event handlers
1316
builder.Services.AddSlackBotEvents()
14-
.AddAppMentionHandler<DoStuff>();
17+
.AddMessageActionsHandler<DoOtherStuff>()
18+
.AddAppMentionHandler<DoStuff>();
1519

1620

1721
var app = builder.Build();
@@ -27,4 +31,19 @@ public Task<EventHandledResponse> Handle(EventMetaData eventMetadata, AppMention
2731
Console.WriteLine("Doing stuff!");
2832
return Task.FromResult(new EventHandledResponse("yolo"));
2933
}
30-
}
34+
}
35+
36+
class DoOtherStuff : IHandleMessageActions
37+
{
38+
public async Task<EventHandledResponse> Handle(MessageActionInteraction @event)
39+
{
40+
var str = JsonSerializer.Serialize(@event);
41+
var httpClient = new HttpClient();
42+
var res = await httpClient.PostAsJsonAsync(@event.Response_Url, new
43+
{
44+
text = "Thx"
45+
});
46+
Console.WriteLine(str);
47+
return new EventHandledResponse("OK");
48+
}
49+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Slackbot.Net.Endpoints.Models.Interactive.MessageActions;
2+
3+
namespace Slackbot.Net.Endpoints.Abstractions;
4+
5+
public interface IHandleMessageActions
6+
{
7+
Task<EventHandledResponse> Handle(MessageActionInteraction blockActionEvent);
8+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ public interface ISlackbotHandlersBuilder
1212

1313
public ISlackbotHandlersBuilder AddAppHomeOpenedHandler<T>() where T : class, IHandleAppHomeOpened;
1414
public ISlackbotHandlersBuilder AddNoOpAppMentionHandler<T>() where T : class, INoOpAppMentions;
15+
16+
public ISlackbotHandlersBuilder AddMessageActionsHandler<T>() where T : class, IHandleMessageActions;
1517
}

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 AddNoOpAppMentionHandler<T>() where T : class, I
5353
_services.AddSingleton<INoOpAppMentions, T>();
5454
return this;
5555
}
56+
57+
public ISlackbotHandlersBuilder AddMessageActionsHandler<T>() where T : class, IHandleMessageActions
58+
{
59+
_services.AddSingleton<IHandleMessageActions, T>();
60+
return this;
61+
}
5662
}

source/src/Slackbot.Net.Endpoints/Middlewares/HttpItemsManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Slackbot.Net.Endpoints.Models.Events;
66
using Slackbot.Net.Endpoints.Models.Interactive;
77
using Slackbot.Net.Endpoints.Models.Interactive.BlockActions;
8+
using Slackbot.Net.Endpoints.Models.Interactive.MessageActions;
89
using Slackbot.Net.Endpoints.Models.Interactive.ViewSubmissions;
910

1011
namespace Slackbot.Net.Endpoints.Middlewares;
@@ -105,6 +106,8 @@ private static Interaction ToInteractiveType(JsonElement payloadJson, string raw
105106
return viewSubmission;
106107
case InteractionTypes.BlockActions:
107108
return JsonSerializer.Deserialize<BlockActionInteraction>(json, WebOptions);
109+
case InteractionTypes.MessageAction:
110+
return JsonSerializer.Deserialize<MessageActionInteraction>(json, WebOptions);
108111
default:
109112
var unknownSlackEvent = JsonSerializer.Deserialize<UnknownInteractiveMessage>(json, WebOptions);
110113
unknownSlackEvent.RawJson = raw;

source/src/Slackbot.Net.Endpoints/Middlewares/InteractiveEvents.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Slackbot.Net.Endpoints.Abstractions;
44
using Slackbot.Net.Endpoints.Models.Interactive;
55
using Slackbot.Net.Endpoints.Models.Interactive.BlockActions;
6+
using Slackbot.Net.Endpoints.Models.Interactive.MessageActions;
67
using Slackbot.Net.Endpoints.Models.Interactive.ViewSubmissions;
78

89
namespace Slackbot.Net.Endpoints.Middlewares;
@@ -13,14 +14,20 @@ internal class InteractiveEvents
1314
private readonly ILogger<InteractiveEvents> _logger;
1415
private readonly IEnumerable<IHandleViewSubmissions> _responseHandlers;
1516
private readonly IEnumerable<IHandleInteractiveBlockActions> _blockActionHandlers;
17+
private readonly IEnumerable<IHandleMessageActions> _messageActionHandlers;
1618
private readonly NoOpViewSubmissionHandler _noOp;
1719

18-
public InteractiveEvents(RequestDelegate next, ILogger<InteractiveEvents> logger, IEnumerable<IHandleViewSubmissions> responseHandlers, IEnumerable<IHandleInteractiveBlockActions> blockActionHandlers, ILoggerFactory loggerFactory)
20+
public InteractiveEvents(RequestDelegate next, ILogger<InteractiveEvents> logger,
21+
IEnumerable<IHandleViewSubmissions> responseHandlers,
22+
IEnumerable<IHandleInteractiveBlockActions> blockActionHandlers,
23+
IEnumerable<IHandleMessageActions> messageActionHandlers,
24+
ILoggerFactory loggerFactory)
1925
{
2026
_next = next;
2127
_logger = logger;
2228
_responseHandlers = responseHandlers;
2329
_blockActionHandlers = blockActionHandlers;
30+
_messageActionHandlers = messageActionHandlers;
2431
_noOp = new NoOpViewSubmissionHandler(loggerFactory.CreateLogger<NoOpViewSubmissionHandler>());
2532
}
2633

@@ -43,12 +50,40 @@ public async Task Invoke(HttpContext context)
4350
};
4451
await context.Response.WriteAsync(res.Response);
4552
break;
53+
case InteractionTypes.MessageAction:
54+
await HandleMessageAction(payload as MessageActionInteraction);
55+
break;
4656
default:
4757
await _noOp.Handle(payload);
4858
break;
4959
}
5060
}
5161

62+
private async Task HandleMessageAction(MessageActionInteraction messageAction)
63+
{
64+
var handler = _messageActionHandlers.FirstOrDefault();
65+
66+
if (handler == null)
67+
{
68+
_logger.LogError($"No handler registered for {nameof(MessageActionInteraction)} interactions");
69+
await _noOp.Handle(messageAction);
70+
}
71+
else
72+
{
73+
_logger.LogInformation($"Handling using {handler.GetType()}");
74+
try
75+
{
76+
_logger.LogInformation($"Handling using {handler.GetType()}");
77+
var response = await handler.Handle(messageAction);
78+
_logger.LogInformation(response.Response);
79+
}
80+
catch (Exception e)
81+
{
82+
_logger.LogError(e, e.Message);
83+
}
84+
}
85+
}
86+
5287
private async Task<EventHandledResponse> HandleBlockActions(BlockActionInteraction payload)
5388
{
5489
var handler = _blockActionHandlers.FirstOrDefault();

source/src/Slackbot.Net.Endpoints/Models/Interactive/InteractionTypes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ public class InteractionTypes
44
{
55
public const string ViewSubmission = "view_submission";
66
public const string BlockActions = "block_actions";
7+
public const string MessageAction = "message_action";
78
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Slackbot.Net.Endpoints.Models.Interactive.MessageActions;
2+
3+
public class MessageActionInteraction : Interaction
4+
{
5+
public string Callback_Id { get; set; }
6+
public string Response_Url { get; set; }
7+
8+
public Team Team { get; set; }
9+
public User User { get; set; }
10+
11+
public string Message_Ts { get; set; }
12+
13+
public Message Message { get; set; }
14+
}
15+
16+
public class Team
17+
{
18+
public string Id { get; set; }
19+
}
20+
21+
public class User
22+
{
23+
public string Id { get; set; }
24+
public string Username { get; set; }
25+
public string Name { get; set; }
26+
}
27+
28+
public class Message
29+
{
30+
public string Text { get; set; }
31+
public string User { get; set; }
32+
public string Ts { get; set; }
33+
}

0 commit comments

Comments
 (0)