ChatopsRPC v0.2.0 ChatopsRPC View Source
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
end
Add the chatops plug to your router or phoenix endpoint:
defmodule ChatopsRPC.TestRouter do
use Plug.Router
forward "/_chatops", to: ChatopsRPC.Plug, handler: MyApp.Chatops
end
You'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
end
The 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
end
Your bot should now be able to interact with rpc commands and send rpcs to servers.