这次介绍一个跟Slack 紧密相关的Gem。
前些日子简单的接触了一下,实现了一个简单的功能,但是没有太深入的研究过,本来这次应该先介绍Exception Notification ,然后再介绍这个Slack的Gem。由于时间仓促,下个星期先研究一下Exception Notification再做介绍,这次就先介绍这个了。
功能简介
简单说,这个Gem提供了向Slack的channel发送消息的功能。举个简单的栗子:
1
2
3
4
5
6
require 'slack-notifier'
notifier = Slack : :Notifier . new "WEBHOOK_URL"
notifier . ping "Hello World"
# => if your webhook is setup, will message "Hello World"
# => to the default channel you set in slack
链接(Links)
Slack所要一种确定的链接格式,所以slack-notifier会在post之前试图转化成html或markdown格式的链接。例如:
1
2
3
message = "Hello world, [check](http://example.com) it <a href='http://example.com'>out</a>"
Slack : :Notifier :: LinkFormatter . format ( message )
# => "Hello world, <http://example.com|check> it <http://example.com|out>"
额外的参数
除了这些常规的设置,还可以有一些额外的参数设置,详细的设置可以参照Slack webhook文档 。这里简单举一个栗子。
设置icon:
1
2
3
notifier . ping "feeling spooky" , icon_emoji : ":ghost:"
# or
notifier . ping "feeling chimpy" , icon_url : "http://static.mailchimp.com/web/favicon.png"
代码块设置方式如下:
1
2
3
4
5
6
a_ok_note = {
fallback : "Everything looks peachy" ,
text : "Everything looks peachy" ,
color : "good"
}
notifier . ping "with an attachment" , attachments : [ a_ok_note ]
自定义HTTP客户端
很容易,但内容比较灵活,详情可参见原文档