Last active
March 6, 2020 01:21
-
-
Save gomasy/f2360e308b611a08a36b22531a5d1623 to your computer and use it in GitHub Desktop.
ありがとう 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
require "digest/md5" | |
require "net/https" | |
require "json" | |
require "mail" | |
require "time" | |
require "uri" | |
def parse_envelope(data) | |
if data.multipart? | |
data.parts.map do |pt| | |
if pt.content_type == "text/plain" | |
return pt.decoded | |
end | |
end | |
return data.parts[0].decoded | |
else | |
return data.decoded | |
end | |
end | |
slack_url = URI.parse("") # slack webhook url | |
header = { | |
"channel" => "@gomasy", | |
"username" => "E-mail Alert", | |
"icon_emoji" => ":envelope_with_arrow:", | |
"text" => "メールを受信しました。", | |
} | |
begin | |
mail = Mail.new(STDIN.read) | |
body = JSON.generate(header.merge({ | |
"attachments" => [ | |
"author_name" => mail.from[0], | |
"author_icon" => %(https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(mail.from[0])}?d=mp&s=32), | |
"fields" => [ | |
{ | |
"title" => mail.subject, | |
"value" => parse_envelope(mail), | |
}, | |
], | |
"footer" => mail.to[0], | |
"ts" => Time.parse(mail.date.to_s).to_i, | |
], | |
})) | |
rescue | |
body = JSON.generate(header.merge({ | |
"attachments" => [ | |
{ | |
"text" => "Can not decode an entire message.", | |
"color" => "danger", | |
}, | |
], | |
})) | |
end | |
Net::HTTP.post(slack_url, body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment