-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslack-weather-notifier.rb
56 lines (45 loc) · 1.37 KB
/
slack-weather-notifier.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'json'
require 'open-uri'
require 'slack/incoming/webhooks'
def post_to_slack text, attachments: attachments
slack = Slack::Incoming::Webhooks.new ENV['WEBHOOK_URL']
slack.post text, attachments: attachments
end
def temperature weather, maxmin
temperature = weather['temperature'][maxmin]
if temperature.nil?
"---"
else
"#{temperature['celsius']}℃"
end
end
def post_weather_for weather, link: link
min = temperature weather, 'min'
max = temperature weather, 'max'
title = "#{weather['dateLabel']}の天気 『#{weather['telop']}』"
text = "最低気温 #{min}\n最高気温 #{max}\n#{weather['date']}"
attachments = [{
title: title,
title_link: link,
text: text,
image_url: weather['image']['url'],
color: "#7CD197"
}]
post_to_slack "", attachments: attachments
end
uri = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=400010'
res = JSON.load(open(uri).read)
title = res['title']
provider = res['copyright']['provider'].first['name']
description = res['description']['text'].delete("\n")
link = res['link']
today = res['forecasts'][0]
tomorrow = res['forecasts'][1]
attachments = [{
title: title,
title_link: link,
text: provider
}]
post_to_slack description, attachments: attachments
post_weather_for today, link: link
post_weather_for tomorrow, link: link