Skip to content

Instantly share code, notes, and snippets.

@gomasy
Last active March 6, 2020 01:21
Show Gist options
  • Save gomasy/f2360e308b611a08a36b22531a5d1623 to your computer and use it in GitHub Desktop.
Save gomasy/f2360e308b611a08a36b22531a5d1623 to your computer and use it in GitHub Desktop.
ありがとう Slack
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