-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #26
- Loading branch information
Showing
18 changed files
with
294 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SlackNet.Interaction; | ||
|
||
namespace SlackNet.AspNetCore | ||
{ | ||
abstract class ResolvedSlashCommandHandler : ISlashCommandHandler | ||
{ | ||
protected ResolvedSlashCommandHandler(string command) => Command = command; | ||
|
||
public string Command { get; } | ||
|
||
public abstract Task<SlashCommandResponse> Handle(SlashCommand command); | ||
} | ||
|
||
class ResolvedSlashCommandHandler<T> : ResolvedSlashCommandHandler | ||
where T : ISlashCommandHandler | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public ResolvedSlashCommandHandler(IServiceProvider serviceProvider, string command) | ||
: base(command) | ||
=> _serviceProvider = serviceProvider; | ||
|
||
public override async Task<SlashCommandResponse> Handle(SlashCommand command) | ||
{ | ||
using (var scope = _serviceProvider.CreateScope()) | ||
{ | ||
var handler = scope.ServiceProvider.GetRequiredService<T>(); | ||
return await handler.Handle(command).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using SlackNet.Interaction; | ||
|
||
namespace SlackNet.AspNetCore | ||
{ | ||
class SlackSlashCommandsService : ISlackSlashCommands | ||
{ | ||
private readonly SlackSlashCommands _commands = new SlackSlashCommands(); | ||
|
||
public SlackSlashCommandsService(IEnumerable<ResolvedSlashCommandHandler> handlers) | ||
{ | ||
foreach (var handler in handlers) | ||
SetHandler(handler.Command, handler); | ||
} | ||
|
||
public Task<SlashCommandResponse> Handle(SlashCommand command) => _commands.Handle(command); | ||
public void SetHandler(string command, ISlashCommandHandler handler) => _commands.SetHandler(command, handler); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Threading.Tasks; | ||
using SlackNet.Interaction; | ||
using SlackNet.WebApi; | ||
|
||
namespace SlackNet.EventsExample | ||
{ | ||
public class EchoCommand : ISlashCommandHandler | ||
{ | ||
public async Task<SlashCommandResponse> Handle(SlashCommand command) | ||
{ | ||
return new SlashCommandResponse | ||
{ | ||
Message = new Message | ||
{ | ||
Text = command.Text | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace SlackNet.Interaction | ||
{ | ||
public interface ISlashCommandHandler | ||
{ | ||
Task<SlashCommandResponse> Handle(SlashCommand command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Collections.Generic; | ||
using SlackNet.Blocks; | ||
using SlackNet.WebApi; | ||
|
||
namespace SlackNet.Interaction | ||
{ | ||
public class MessageResponseWrapper : IReadOnlyMessage | ||
{ | ||
private readonly IMessageResponse _response; | ||
public MessageResponseWrapper(IMessageResponse response) => _response = response; | ||
|
||
public string Channel => _response.Message.Channel; | ||
public string Text => _response.Message.Text; | ||
public ParseMode Parse => _response.Message.Parse; | ||
public bool LinkNames => _response.Message.LinkNames; | ||
public IList<Attachment> Attachments => _response.Message.Attachments; | ||
public IList<Block> Blocks => _response.Message.Blocks; | ||
public bool UnfurlLinks => _response.Message.UnfurlLinks; | ||
public bool UnfurlMedia => _response.Message.UnfurlMedia; | ||
public string Username => _response.Message.Username; | ||
public bool AsUser => _response.Message.AsUser; | ||
public string IconUrl => _response.Message.IconUrl; | ||
public string IconEmoji => _response.Message.IconEmoji; | ||
public string ThreadTs => _response.Message.ThreadTs; | ||
public bool ReplyBroadcast => _response.Message.ReplyBroadcast; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,12 @@ | ||
using System.Collections.Generic; | ||
using SlackNet.Blocks; | ||
using SlackNet.WebApi; | ||
|
||
namespace SlackNet.Interaction | ||
namespace SlackNet.Interaction | ||
{ | ||
public class MessageUpdateResponse : IReadOnlyMessage | ||
public class MessageUpdateResponse : MessageResponseWrapper | ||
{ | ||
private readonly MessageResponse _response; | ||
public MessageUpdateResponse(MessageResponse response) => _response = response; | ||
private readonly MessageResponse _updateResponse; | ||
public MessageUpdateResponse(MessageResponse response) : base(response) => _updateResponse = response; | ||
|
||
public ResponseType ResponseType => _response.ResponseType; | ||
public bool ReplaceOriginal => _response.ReplaceOriginal; | ||
public bool DeleteOriginal => _response.DeleteOriginal; | ||
public string Channel => _response.Message.Channel; | ||
public string Text => _response.Message.Text; | ||
public ParseMode Parse => _response.Message.Parse; | ||
public bool LinkNames => _response.Message.LinkNames; | ||
public IList<Attachment> Attachments => _response.Message.Attachments; | ||
public IList<Block> Blocks => _response.Message.Blocks; | ||
public bool UnfurlLinks => _response.Message.UnfurlLinks; | ||
public bool UnfurlMedia => _response.Message.UnfurlMedia; | ||
public string Username => _response.Message.Username; | ||
public bool AsUser => _response.Message.AsUser; | ||
public string IconUrl => _response.Message.IconUrl; | ||
public string IconEmoji => _response.Message.IconEmoji; | ||
public string ThreadTs => _response.Message.ThreadTs; | ||
public bool ReplyBroadcast => _response.Message.ReplyBroadcast; | ||
public ResponseType ResponseType => _updateResponse.ResponseType; | ||
public bool ReplaceOriginal => _updateResponse.ReplaceOriginal; | ||
public bool DeleteOriginal => _updateResponse.DeleteOriginal; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace SlackNet.Interaction | ||
{ | ||
public interface ISlackSlashCommands | ||
{ | ||
Task<SlashCommandResponse> Handle(SlashCommand command); | ||
void SetHandler(string command, ISlashCommandHandler handler); | ||
} | ||
|
||
public class SlackSlashCommands : ISlackSlashCommands | ||
{ | ||
private readonly Dictionary<string, ISlashCommandHandler> _handlers = new Dictionary<string, ISlashCommandHandler>(); | ||
|
||
public Task<SlashCommandResponse> Handle(SlashCommand command) => | ||
_handlers.TryGetValue(command.Command, out var handler) | ||
? handler.Handle(command) | ||
: Task.FromResult((SlashCommandResponse)null); | ||
|
||
public void SetHandler(string command, ISlashCommandHandler handler) | ||
{ | ||
if (!command.StartsWith("/")) | ||
throw new ArgumentException("Command must start with '/'.", nameof(command)); | ||
|
||
_handlers[command] = handler; | ||
} | ||
} | ||
} |
Oops, something went wrong.