Commit 8e015c5c authored by Cool Fire's avatar Cool Fire

Add some support for slack/mattermost compatible webhooks

parent 1505a883
Pipeline #2044 failed with stages
in 1 minute and 47 seconds
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'sinatra'
......@@ -19,4 +19,4 @@ DEPENDENCIES
sinatra
BUNDLED WITH
1.14.6
2.1.4
require File.expand_path '../nanoapi_app.rb', __FILE__
# frozen_string_literal: true
require File.expand_path 'nanoapi_app.rb', __dir__
run Sinatra::Application
---
key: foobar
webhook_keys:
- foobar_key
- barfoo_key
# frozen_string_literal: true
require 'json'
require 'sinatra'
require 'socket'
......@@ -9,6 +10,11 @@ def check_key(key)
halt 401 if key != config['key']
end
def check_webhook_key(key)
config = YAML.load_file 'config.yaml'
halt 401 unless config['webhook_keys'].include? key
end
def forward_message(message)
sock = TCPSocket.open 'localhost', 2000
sock.puts message
......@@ -30,3 +36,21 @@ post '/v1/message' do
'Message accepted'
end
post '/v1/webhook/:webhook_key' do
check_webhook_key params[:webhook_key]
halt 400 unless params.key? :payload
payload = JSON.parse(params[:payload])
halt 400 unless payload.key? 'text'
payload['channel'] = '#shells' unless payload.key? 'channel'
payload['username'] = 'webhook' unless payload.key? 'username'
forward_message({
dest: payload['channel'],
message: "#{payload['username']}: #{params['text']}"
}.to_json)
'OK'
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment