ChatopsRpc
Docs: https://hexdocs.pm/chatops_rpc.
An elixir implementation of the ChatopsRPC protocol. This repo provides both server side and client side implementations of the protocol for use with Plug and Fawkes respectively.
Servers
Define your chatops:
defmodule MyApp.Chatops do
use ChatopsRPC.Builder
namespace :my_app
@help """
echo <text> - Echo some text back to you
"""
command :echo, ~r/echo (?<text>.*)?/, fn %{params: args} ->
"#{args["text"]}"
end
endAdd the chatops plug to your router or phoenix endpoint:
defmodule ChatopsRPC.TestRouter do
use Plug.Router
forward "/_chatops", to: ChatopsRPC.Plug, handler: MyApp.Chatops
endYou’ll also need to add the chatops body parser to your plug parsers:
plug Plug.Parsers,
parsers: [:urlencoded, :json],
pass: ["text/*"],
json_decoder: Jason,
body_reader: {ChatopsRPC.Plug.BodyReader, :read_body, []}This step is required because chatops rpc needs the full body in order to the client signature.
Finally, you’ll need to add the server to your application’s supervision tree:
defmodule MyApp.Application do
def start(_, _) do
public_key = System.get_env("CHATOPS_PUBLIC_KEY")
children = [
{ChatopsRPC.Server, [base_url: "YOUR APPS URL", public_key: public_key]},
]
end
endThe server is used to verify client signatures, store nonces, and other stateful tasks.
Clients
Add the chatops handler to your fawkes bot:
opts = [
name: TestBot,
bot_alias: ".",
adapter: {Fawkes.Adapter.Slack, [token: config.slack_token]},
brain: {Fawkes.Brain.Redis, []},
handlers: [
{ChatopsRPC.Handler, []},
]
]
Fawkes.start_link(opts)Add the client module to your bots supervision tree:
defmodule MyBot.Application do
@moduledoc false
use Application
def start(_type, _args) do
opts = [
name: TestBot,
bot_alias: ".",
adapter: {Fawkes.Adapter.Slack, [token: config.slack_token]},
brain: {Fawkes.Brain.Redis, []},
handlers: [
{ChatopsRPC.Handler, []},
]
]
children = [
{ChatopsRPC.Client, private_key: File.read!("private_key")},
{Fawkes, opts},
]
opts = [strategy: :one_for_one, name: TestBot.Supervisor]
Supervisor.start_link(children, opts)
end
endYour bot should now be able to interact with rpc commands and send rpcs to servers.
Should I use this?
This implementation is missing some features from the canonical implementation as well as some tests around key pieces of functionality. But the rudimentary functionality seems to be working correctly and should be safe to use in production.
Installation
If available in Hex, the package can be installed
by adding chatops_rpc to your list of dependencies in mix.exs:
def deps do
[
{:chatops_rpc, "~> 0.1.0"}
]
endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/chatops_rpc.