Skip to content

Instantly share code, notes, and snippets.

@phumpal
Last active June 23, 2020 03:28
Show Gist options
  • Save phumpal/4522a07dc5948ab91d3cdbc3634cf808 to your computer and use it in GitHub Desktop.
Save phumpal/4522a07dc5948ab91d3cdbc3634cf808 to your computer and use it in GitHub Desktop.
A simple Sinatara app to proxy Airbrake webhooks to Google Hangout Chats

Setup

Uses the hangouts-chat gem

brew install jq
rvm install ruby-2.5.7
rvm gemset create hangouts && rvm gemset use hangouts
bundle install

Testing

Open two terminal sessions. In the first run ruby app.rb -b 0.0.0.0

In the second POST webhook.json to the Sinatra endpoint

curl -s -XPOST -H "Content-Type: application/json" \
  -d @webhook.json \
  "http://localhost:4567/event" | jq
#!/usr/bin/env ruby
require 'hangouts_chat'
require 'sinatra'
sender = HangoutsChat::Sender.new 'https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=SPACE_KEY&token=SPACE_TOKEN%3D'
post '/event' do
status 201
request.body.rewind
request_payload = JSON.parse(request.body.read)
header = { title: "#{request_payload['error']['error_message']}",
subtitle: "#{request_payload['error']['error_class']}" }
sections = [{ widgets: [{ keyValue: { topLabel: 'File', content: "#{request_payload['error']['file']}:#{request_payload['error']['line_number']}" } },
{ "buttons": [ { "textButton": {
"text": "Open in Airbrake",
"onClick": {
"openLink": {
"url": "#{request_payload['airbrake_error_url']}"
}
}
} } ] } ]
}]
sender.card(header, sections)
end
source 'https://rubygems.org'
gem 'hangouts-chat'
gem 'sinatra'
GEM
remote: https://rubygems.org/
specs:
hangouts-chat (0.0.6)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
rack (2.2.2)
rack-protection (2.0.8.1)
rack
ruby2_keywords (0.0.2)
sinatra (2.0.8.1)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.8.1)
tilt (~> 2.0)
tilt (2.0.10)
PLATFORMS
ruby
DEPENDENCIES
hangouts-chat
sinatra
BUNDLED WITH
1.17.3
upstream sinatra_app {
server 0.0.0.0:4567
}
server {
listen 443 ssl;
server_name myserver.name;
ssl_certificate /etc/ssl/myserver.name.crt;
ssl_certificate_key /etc/ssl/myserver.name.key;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.2;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
location /event {
proxy_pass http://sinatra_app;
}
}
{
"error":{
"id":37463546,
"error_message":"KitchenException: You are all out of bacon!",
"error_class":"KitchenException",
"file":"[PROJECT_ROOT]/app/controllers/bacon_controller.rb",
"line_number":35,
"project":{
"id":1111,
"name":"Baconator"
},
"last_notice":{
"id":4505303522,
"request_method":null,
"request_url":"http://airbrake.io:445/bacon/cook",
"backtrace":[
"[PROJECT_ROOT]/app/controllers/bacon_controller.rb:35:in `cook'",
"[PROJECT_ROOT]/app/middleware/kitchen.rb:19:in `oven'",
"[PROJECT_ROOT]/app/middleware/kitchen.rb:33:in `chef'",
"[PROJECT_ROOT]/app/middleware/salumi.rb:23:in `store'"
]
},
"environment":"avocado",
"first_occurred_at":"2012-02-23T22:03:03Z",
"last_occurred_at":"2012-03-21T08:37:15Z",
"times_occurred":118
},
"airbrake_error_url": "https://airbrake.io/airbrake-error-url"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment