Giter Site home page Giter Site logo

slash's Introduction

Slash

CircleCI Documentation

A simple Slack slash command builder for Plug.

Documentation

Documentation can be found at https://hexdocs.pm/slash.

If you're not sure what a Slack slash command is, see the Slack documentation.

Installation

Add slash to your list of dependencies in mix.exs:

def deps do
  [{:slash, "~> 2.0.0"}]
end

Usage

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

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

Usage with Plug Router

1. Define your Slack Router plug using Slash.Builder.
defmodule Bot.SlackRouter do
  use Slash.Builder

  command :async_greet, fn command ->
    async(command, fn ->
      Process.sleep(5_000)

      "Async response!"
    end)
  end

  command :greet, fn %{args: args} ->
    case args do
      [name] ->
        "Greetings #{name}!"

      _ ->
        "Please pass a name to greet!"
    end
  end
end
2. Define your Plug Router
defmodule Bot.Router do
  use Plug.Router

  # NOTE: The custom body_reader option here is critical.
  plug Plug.Parsers,
    parsers: [:urlencoded],
    body_reader: {Slash.BodyReader, :read_body, []}

  plug :match
  plug :dispatch

  forward "/slack", to: Bot.SlackRouter

  match _ do
    send_resp(conn, 404, "")
  end
end

Usage with Phoenix

1. Define your Slash Router plug using Slash.Builder like above.
2. Add a new scope or use an existing scope in your Phoenix.Router
scope "/" do
  pipe_through [:api]

  forward "/slack", ShieldSlack.Router
end
3. Override the body reader in your Phoenix.Endpoint
plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json],
  pass: ["*/*"],
  body_reader: {Slash.BodyReader, :read_body, []}

Configuration

Assuming you've setup the above Bot.SlackRouter module in your mix project. To configure the Slack signature key, configure your builder module using the following:

config :slash, Bot.SlackRouter,
  signing_key: "secret key from slack"

Slack Help Generation

Another feature of this library is the ability to automatically generate help documentation and the help command itself. To generate per-command help text, simply decorate your command functions with a @help module parameter.

Here is an example:

  @help """
  Generates a sweet gif

  Example: '/gif make'
  """
  command :make, fn _command  do
    # some code
  end

Testing

See the Slash.Test module for more information in testing Slash.Builder plugs.

License (MIT)

Copyright (c) 2017-2019 Matt McFarland

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

slash's People

Contributors

chaseconey avatar vanetix avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.