slack_command v1.0.0 SlackCommand.Router behaviour View Source

Provides a primary macro defcommand to enable building a Plug that can route inbound requests from Slack with a familiar interface (functions).

Since SlackCommand.Router is just a builder for a Plug, it can be integrated into either a Plug.Router or a Phoenix application.

Example Usage

defmodule SlackBot.SlackRouter do
  use SlackCommand.Router

  defcommand hello(_, name) do
    "Greetings #{name}!"
  end
end

defmodule SlackBot.Router do
  use Plug.Router

  plug :match
  plug Plug.Parsers, parsers: [:json],
                     pass:  ["application/json"],
                     json_decoder: Jason
  plug :dispatch

  forward "/slack", to: SlackBot.SlackRouter
end

Link to this section Summary

Functions

Defines a Slack command by decomposing the function head. The first argument is the %Plug.Conn{} struct and is always required

Handle the %Plug.Conn{} with router, which is expected to be a module that has called use SlackCommand.Router

Callbacks

Authenticates the connection, the default implementation just verifies that the received token in the payload matches the configured value

Process the command given from slack

Link to this section Functions

Link to this macro defcommand(head_ast, list) View Source (macro)

Defines a Slack command by decomposing the function head. The first argument is the %Plug.Conn{} struct and is always required.

For example:

defcommand ping(_) do
  "pong"
end

Which will then translate to then slash command in Slack /bot ping.

Link to this function handle(router, conn) View Source
handle(SlackCommand.Router, Plug.Conn.t()) :: Plug.Conn.t()

Handle the %Plug.Conn{} with router, which is expected to be a module that has called use SlackCommand.Router

Matches on the root route when path_info == []

Link to this section Callbacks

Link to this callback authenticated?(arg0) View Source
authenticated?(String.t()) :: boolean()

Authenticates the connection, the default implementation just verifies that the received token in the payload matches the configured value.

Link to this callback do_command(arg0, arg1, list) View Source
do_command(String.t(), Plug.Conn.t(), [String.t()]) ::
  SlackCommand.Message.t()
  | {:ok, SlackCommand.Message.t()}
  | {:ok, String.t() | {:error, String.t()}}

Process the command given from slack.