Add slack notification

This commit is contained in:
benji 2015-04-04 01:44:28 +02:00
parent 9ec9f043ed
commit be9c967db2
7 changed files with 54 additions and 1 deletions

View file

@ -90,3 +90,6 @@ gem 'coveralls', require: false
# Default avatar for users
gem 'identicon'
# Slack
gem 'tarumi'

View file

@ -121,6 +121,9 @@ GEM
activesupport (>= 4.1.0)
hashie (3.4.0)
hike (1.2.3)
httparty (0.13.3)
json (~> 1.8)
multi_xml (>= 0.5.2)
i18n (0.7.0)
identicon (0.0.3)
chunky_png
@ -247,6 +250,9 @@ GEM
colorize (>= 0.7.0)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
tarumi (0.0.1)
activesupport
httparty
term-ansicolor (1.3.0)
tins (~> 1.0)
thor (0.19.1)
@ -303,6 +309,7 @@ DEPENDENCIES
sdoc (~> 0.4.0)
spring
sqlite3
tarumi
turbolinks
tzinfo-data
uglifier (>= 1.3.0)

View file

@ -1,5 +1,6 @@
class OrdersController < ApplicationController
include ActionView::Helpers::NumberHelper
include ApplicationHelper
load_and_authorize_resource
@ -17,7 +18,9 @@ class OrdersController < ApplicationController
@order = @user.orders.build order_params
if @order.save
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
message = "#{@order.to_sentence} ordered. Enjoy it!"
flash[:success] = message
slack_notification(@user, message)
redirect_to root_path
else
@order.g_order_items Product.all

View file

@ -17,4 +17,15 @@ module ApplicationHelper
options[:builder] = FormattedFormBuilder
form_for(record, options, &block)
end
def slack_notification(user, message)
require 'net/http'
require 'json'
postData = Net::HTTP.post_form(URI.parse('https://slack.com/api/users.list'), {'token'=>'xoxp-2484654576-2817526333-4116062828-04487a'})
slackmember = JSON.parse(postData.body)["members"].select{ |m| m["profile"]["email"] == user.uid + "@zeus.ugent.be" }.first
if slackmember
Webhook.new(channel: "@" + slackmember["name"], username: "Tab").ping(message)
end
end
end

24
app/models/webhook.rb Normal file
View file

@ -0,0 +1,24 @@
class Webhook
DEFAULT_USERNAME = "Tab"
attr_accessor :hook
def initialize(attributes = {})
options = {
channel: (attributes[:channel]),
username: (attributes[:username] || DEFAULT_USERNAME),
icon_url: attributes[:icon_url],
icon_emoji: attributes[:icon_emoji]
}
self.hook = Tarumi::Bot.new(
Tab002::ZEUS_TEAM,
Tab002::ZEUS_TOKEN,
options
)
end
def ping(text)
self.hook.ping(text)
end
end

View file

@ -7,6 +7,9 @@ require 'rails/all'
Bundler.require(*Rails.groups)
module Tab002
ZEUS_TEAM = 'zeuswpi'
ZEUS_TOKEN = Rails.application.secrets.zeus_token
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers

View file

@ -14,6 +14,7 @@ development:
secret_key_base: 5d40610321e19e4f71ee2ba8af4f426fe15096c405da3800c6b33bed6779f2d11f55a0edc455974b19a01fd71f6cd508dba980305dbc55ff82521a2d12f891d8
omniauth_client_id: tomtest
omniauth_client_secret: blargh
zeus_token: ""
test:
secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81
@ -24,3 +25,4 @@ production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
omniauth_client_id: ""
omniauth_client_secret: ""
zeus_token: ""