slash v2.0.4 Slash.Builder View Source
Slash.Builder
is responsible for building the actual plug that can be used in a
Plug pipeline.
The main macro provided when using the module is command/2
, which allows you to declare
commands for your Slash plug. Additionally the before/1
macro configures a function to be
executed prior to matching a command. These functions can be used to authenticate users, verify
authorized channels, or similiar.
Usage
Additional options can be passed when using the module. Accepted options are:
name
- name of the Slack router, the only place this is currently used is in help text generation.formatter
- a customSlash.Formatter
implementation you should like to use in place of the defaultSlash.Formatter.Dasherized
.
Configuration
In order to verify the Slack signature using Slash.Signature
, the following will need to be
configured for each router that was built with Slack.Builder
.
config :slash, Bot.SlackRouter,
signing_key: "secret key from slack"
Help generation
Slash automatically builds out a help subcommand which can be invoked with /bot help
, and
will display basic commands available by default. If you want to customize this functionality
you can use the @help
module attribute.
defmodule Bot.SlackRouter do
use Slash.Builder
@help "Sends you a hearty hello!"
command :greet, fn _command ->
"Hello!"
end
end
Examples
Basic
defmodule Bot.SlackRouter do
use Slash.Builder, name: "Custom Bot", formatter: MyCustomFormatter
command :greet, fn _command ->
"Greetings!"
end
end
Before Handler
defmodule Bot.SlackRouter do
use Slash.Builder
before :verify_user
command :greet, fn %{args: args} ->
case args do
[name] ->
"Hello #{name}!"
_ ->
"Please pass name to greet"
end
end
def verify_user(%{user_id: user_id} = command) do
case Accounts.find_user_by_slack_id(user_id) do
nil ->
{:error, "User not authorized"}
user ->
{:ok, put_data(command, :user, user)}
end
end
end
Link to this section Summary
Types
Valid return values for a before handler function. See Slash.Builder.before/1
for more information
Valid return types from command handler functions. See Slash.Builder.command/2
for more information
Functions
Defines a function to be executed before the command is routed to the appropriate handler function
Defines a command for the Slack router, the first argument is always a Slash.Command
struct
Handle a command block return value
Verify the request according to the Slack documentation
Link to this section Types
before_response()
View Source
before_response() :: {:ok, Slash.Command.t()} | {:error, String.t()}
before_response() :: {:ok, Slash.Command.t()} | {:error, String.t()}
Valid return values for a before handler function. See Slash.Builder.before/1
for more information.
command_response() View Source
Valid return types from command handler functions. See Slash.Builder.command/2
for more information.
Link to this section Functions
before(function_name) View Source (macro)
Defines a function to be executed before the command is routed to the appropriate handler function.
The function_name
should be a reference to the name of the function on the current module.
Values returned from a before function should match the before_response/0
type.
command(name, func)
View Source
(macro)
command(atom(), (Slash.Command.t() -> command_response())) :: Macro.t()
command(atom(), (Slash.Command.t() -> command_response())) :: Macro.t()
Defines a command for the Slack router, the first argument is always a Slash.Command
struct.
The name
argument should be the command name you would like to define, this should be an
internal name, for example greet_user
. This will then ran through
SlackCommand.Formatter.Dasherized
by default, creating the Slack command greet-user
.
The func
argument will be your function which is invoked on command route match, this
function will always receive the %Slash.Command{}
struct as an argument.
TODO: This needs to verify the arity of func
.
handle_command(module, conn, command)
View Source
handle_command(module(), Plug.Conn.t(), Slash.Command.t()) :: Plug.Conn.t()
handle_command(module(), Plug.Conn.t(), Slash.Command.t()) :: Plug.Conn.t()
Handle a command block return value.
verify_request(module, conn)
View Source
verify_request(module(), Plug.Conn.t()) :: boolean()
verify_request(module(), Plug.Conn.t()) :: boolean()
Verify the request according to the Slack documentation.
See the Slack documentation for additional details.