Created
October 1, 2014 13:51
-
-
Save quanon/499289909b66193a6bbe to your computer and use it in GitHub Desktop.
Lodge に記事 or コメントの投稿があったら Slack で通知する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lodge: | |
per_size: 30 | |
right_list_size: 10 | |
slack: | |
defaults: &defaults | |
url: https://slack.com | |
channel: #channel | |
# トークンは Slack API のページで発行できます。 | |
token: my_token | |
username: 'Lodge に住む海未ちゃん' | |
icon_url: http://i.minus.com/j39fQ8bf9uA28.png | |
messages: | |
article: | | |
%{article_username} さんが「%{article_title}」を Lodge に投稿したみたいですよ。 | |
是非チェックしてくださいね! | |
http://mana-lodge.ediea.net/articles/%{article_id} | |
comment: | | |
%{article_username} さんの投稿した「%{article_title}」に、 | |
%{comment_username} さんがコメントしたみたいですよ。 | |
ちょっと見てみましょう! | |
http://mana-lodge.ediea.net/articles/%{article_id} | |
'[email protected]': | |
<<: *defaults | |
username: 'Lodge に住む凛ちゃん' | |
icon_url: http://i.minus.com/jbzg8KazKl4i7M.png | |
messages: | |
article: | | |
%{article_username} さんが「%{article_title}」を Lodge に投稿したみたいにゃ! | |
今すぐチェックするにゃー!! | |
http://mana-lodge.ediea.net/articles/%{article_id} | |
comment: | | |
%{article_username} さんの投稿した「%{article_title}」に、 | |
%{comment_username} さんがコメントしたみたいにゃ! | |
今すぐチェックするにゃー!! | |
http://mana-lodge.ediea.net/articles/%{article_id} | |
'[email protected]': | |
<<: *defaults | |
username: 'Lodge に住む伝説のメイドさん' | |
icon_url: http://i.minus.com/jmyOp2aPOk6pA.png | |
messages: | |
article: | | |
%{article_username} サンが「%{article_title}」を Lodge にトーコーしたみたいデース! | |
ヨキニハカラエミナノシュー | |
http://mana-lodge.ediea.net/articles/%{article_id} | |
comment: | | |
%{article_username} サンのトーコーした「%{article_title}」に、 | |
%{comment_username} さんがコメントしたみたいデース! | |
ヨキニハカラエミナノシュー | |
http://mana-lodge.ediea.net/articles/%{article_id} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
class Slack | |
extend Forwardable | |
def_delegators :@settings, :url, :channel, :token, :username, :icon_url | |
def self.notice(record) | |
new(record).notice | |
end | |
def initialize(record) | |
@record = record | |
@user = @record.user | |
@settings = | |
(Settings.slack[@user.try(:email)] || Settings.slack.defaults) | |
end | |
def notice | |
slack.post_message | |
end | |
protected | |
def post_message | |
post('/api/chat.postMessage', { | |
token: token, | |
channel: channel, | |
username: username, | |
text: message, | |
icon_url: icon_url | |
}) | |
end | |
private | |
def slack | |
case @record | |
when Article | |
ArticleSlack.new(@record) | |
when Comment | |
CommentSlack.new(@record) | |
else | |
raise ArgumentError | |
end | |
end | |
def connection | |
@connection ||= Faraday.new(url: url) do |faraday| | |
faraday.request(:url_encoded) | |
faraday.adapter(Faraday.default_adapter) | |
end | |
end | |
def post(url, params=nil) | |
connection.post do |request| | |
request.body = params | |
request.url(url) | |
end | |
end | |
def message | |
raise NotImplementedError | |
end | |
end | |
class ArticleSlack < Slack | |
private | |
def message | |
article = @record | |
@settings.messages.article % { | |
article_username: article.user.name, | |
article_title: article.title, | |
article_id: article.id | |
} | |
end | |
end | |
class CommentSlack < Slack | |
private | |
def message | |
comment = @record | |
article = @record.article | |
@settings.messages.comment % { | |
comment_username: comment.user.name, | |
article_username: article.user.name, | |
article_title: article.title, | |
article_id: article.id | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lodge はイントラネット限定でも使える、ナレッジ/ノウハウ情報共有サービス。Rails4製。