| Jun | JUL | Aug |
| 10 | ||
| 2020 | 2021 | 2022 |
COLLECTED BY
Collection: github.com
class ChatopsController < ApplicationController include ::Chatops::Controller # The default chatops RPC prefix. Clients may replace this. chatops_namespace :echo chatop :echo, /(?<text>.*)?/, "<text> - Echo some text back" do jsonrpc_success "Echoing back to you: #{jsonrpc_params[:text]}" end endSome routing boilerplate is required in
config/routes.rb:
"chatops#list"
end
">Rails.application.routes.draw do # Replace the controller: argument with your controller's name post "/_chatops/:chatop", controller: "chatops", action: :execute_chatop get "/_chatops" => "chatops#list" endIt's easy to test:
class MyControllerTestCase < ActionController::TestCase include Chatops::Controller::TestCaseHelpers before do chatops_prefix "echo" chatops_auth! end def test_it_works chat "echo foo bar baz" assert_equal "foo bar baz", chatop_response end endBefore you deploy, add the RPC authentication tokens to your app's environment, below. You're all done. Try
.echo foo, and you should see your client respond with
Echoing back to you: foo.
A hubot client implementation is available at
https://github.com/hubot-scripts/hubot-chatops-rpc
.<namespace> in chat. The namespace is a
default chatops RPC prefix and may be overridden by a client.
chatops_namespace :foo
chatop :echo, /(?<text>.*)?/, "<text> - Echo some text back" do jsonrpc_success "Echoing back to you: #{jsonrpc_params[:text]}" endIn this example, we've created a chatop called
echo. The next argument is a
regular expression with named
captures.
In this example, only one capture group is available, text.
The next line is a string, which is a single line of help that will be displayed
in chat for .echo.
The DSL takes a block, which is the code that will run when the chat robot sees
this regex. Arguments will be available in the params hash. params[:user]
and params[:room_id] are special, and will be set by the client. user will
always be the login of the user typing the command, and room_id will be where
it was typed.
The optional mention_slug parameter will provide the name to use to refer to
the user when sending a message; this may or may not be the same thing as the
username, depending on the chat system being used. The optional message_id parameter will provide a reference to the message that invoked the rpc.
You can return jsonrpc_success with a string to return text to chat. If you
have an input validation or other handle-able error, you can use
jsonrpc_failure to send a helpful error message.
Chatops are regular old rails controller actions, and you can use niceties like
before_action and friends. before_action :echo, :load_user for the above
case would call load_user before running echo.
CHATOPS_AUTH_PUBLIC_KEY is the public key of your chatops client in PEM
format. This environment variable will be the contents of a .pub file,
newlines and all.
CHATOPS_AUTH_BASE_URL is the base URL of your server as the chatops client
sees it. This is specified as an environment variable since rails will trust
client headers about a forwarded hostname. For example, if your chatops client
has added the url https://example.com/_chatops, you'd set this to
https://example.com.
You can also optionally set CHATOPS_AUTH_ALT_PUBLIC_KEY to a second public key
which will be accepted. This is helpful when rolling keys.
script/bootstrap
script/test
See CONTRIBUTING.md for contribution instructions.
:action routing, which was deprecated in Rails 5.
post "/_chatops/:action", controller: "chatops"Becomes this:
post "/_chatops/:chatop", controller: "chatops" action: :execute_chatop
.help
●Prefixes make regex-clobbering impossible
To upgrade to version 2, upgrade to version 2.x of this gem. To migrate:
●Migrate your chatops to remove any prefixes you have:
chatop :foo, "help", /ci build whatever/, do "yay" endBecomes:
chatop :foo, "help", /build whatever/, do "yay" end●Update your tests:
chat "ci build foobar"Becomes:
chat "build foobar" # or chatops_prefix "ci" chat "ci build foobar"
CHATOPS_ALT_AUTH_TOKEN as a shared secret. This form
of authentication was deprecated and the public key form used above is now
used instead.