If you mutter to the bot, "Tell me if it's raining," I'll let you know the precipitation information as follows.
By the way, the articles so far are as follows Automatically create JIRA tickets with slack bot ~ slack bot development with python ① ~ JIRA ticket with slack bot → Automation of Slack notification ~ slack bot development with python② ~ Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~
・ Hit the following API to get the precipitation intensity and use that information to notify slack. Weather Information API With this
Pre-registration (free) is required to hit the API provided by yahoo For details on how to register, please refer to the following people who have summarized it wonderfully. Get weather information using Yahoo! Open Local Platform (YOLP) and let Raspberry Pi talk with AquesTalkPi
I will add it to my_mention.py this time as well. However, as the amount of code has increased, I will change to the method of calling the code in the scripts directory from mymention.py.
slackbot #A directory that organizes programs. Any name is fine
├─ run.py #Start the bot by running this program
├─ slackbot_settings.py #File to write settings related to bot
└─ plugins #Add bot functionality to this directory
├─ __init__.py #A file to indicate the module. Sky is fine
└─ my_mention.py #Features each file. Any name is fine
└─ files_dir #Store the contents acquired by curl in this directory
├─ switch.html_1 #Content acquired with curl
└─ switch.html_2 #Content acquired with curl
└─ switch.html_3..4..5..6..
└─ scripts #script storage directory
├─ confirm_weather.py #★ Script to get this weather information
├─__init__.py
The contents to be added to my_mention.py are as follows
# coding: utf-8
from slackbot.bot import respond_to # @botname:Decoder that reacts with
from plugins.scripts.confirm_weather import ConfirmWeather
@respond_to('(.rain*)')
def confirm_weather(message, something):
weather_class = ConfirmWeather()
weather_class.return_rainfall(message)
The contents of confirm_weather.py to be placed in the actual script directory are as follows
1 import urllib.request
2 import json
3
4
5 class ConfirmWeather():
6
7 def __init__(self):
8
9 #yahoo API url
10 base_url = 'https://map.yahooapis.jp/weather/V1/place?'
11 #Latitude and longitude of Ginza
12 coordinates_ginza = '139.768976,35.672859'
13 #KEY for using yahoo API(Pre-registration required)
14 crient_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
15 self.url = '%scoordinates=%s&appid=%s&output=json' % (base_url, coordinates_ginza, crient_id)
16
17 def return_rainfall(self, message):
18 #Load the contents of the target URL using the urllib library
19 response = urllib.request.urlopen(self.url).read()
20
21 #The read data is a json file
22 data_json = json.loads(response.decode('utf-8'))
23 # weaher_The following information is stored in a list in info
24 # [{'Rainfall': 0.0, 'Type': 'observation', 'Date': '201707291350'}・ ・ ・ ・]
25 weather_info = data_json['Feature'][0]['Property']['WeatherList']['Weather']
26 base_minutes = 10
27
28 #Rotate the loop while getting the index with the enumerate method
29 for index, var in enumerate(weather_info):
30 #Return message from precipitation intensity_rain_Get from level function
31 info = self.retrun_rain_level(var['Rainfall'])
32 if index == 0:
33 before_words = "now,"
34 if var['Rainfall'] == 0.0:
35 after_words = "Not"
36 else:
37 after_words = "ing"
38
39 else:
40 before_words = '%s minutes later' % (base_minutes)
41 if var['Rainfall'] == 0.0:
42 after_words = "Will not"
43 else:
44 after_words = "Will"
45 base_minutes += 10
46
47 message.send(before_words + info + after_words)
48
49
50 def retrun_rain_level(self, rainfall):
51 if (rainfall == 0.0):
52 rain_level = "It rains"
53 elif (rainfall < 20.0):
54 rain_level = "Slightly heavy rain, Zaza and rain"
55 elif (rainfall < 30.0):
56 rain_level = "It's pouring, and it rains wet even with an umbrella"
57 elif (rainfall < 50.0):
58 rain_level = "Do you have an umbrella?It rains like a bucket turned over"
59 elif (rainfall < 80.0):
60 rain_level = "It rains very hard like a waterfall"
61 elif (rainfall >= 80.0):
62 rain_level = "Go home soon!!Heavy rain with a feeling of oppression that makes you suffocate"
63 return rain_level
11 # Latitude and longitude of Ginza Can be obtained from here http://www.geocoding.jp/
22 data_json = json.loads(response.decode('utf-8'))
You can get json in the following format by hitting api
{'ResultInfo': {'Start': 1, 'Count': 1, 'Description': '', 'Status': 200, 'Latency': 0.003202, 'Copyright': '(C) Yahoo Japan Corporation.', 'Total': 1},
'Feature': [{'Name': 'point(Actually, the latitude and longitude are here.)July 30, 2017 Weather information for 60 minutes from 00:15',
'Property': {'WeatherList': {'Weather': [{'Rainfall': 0.0, 'Type': 'observation', 'Date': '201707300015'}, {'Rainfall': 1.55, 'Type': 'forecast', 'Date': '201707300025'}, {'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707300035'}, {'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707300045'}, {'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707300055'}, {'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707300105'}, {'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707300115'}]}, 'WeatherAreaCode': 4410}, 'Id': '201707300015_139.76898_35.672859', 'Geometry': {'Type': 'point', 'Coordinates': 'Actually, the latitude and longitude are here.'}}]}
All you need is Rainfall This API will tell you from the current observed value to the predicted value one hour later.
25 weather_info = data_json['Feature'][0]['Property']['WeatherList']
If only the above information can be obtained, it is ok, so specify the key and retrieve it. If you print this, you can get only the information you want as follows
[{'Rainfall': 0.0, 'Type': 'observation', 'Date': '201707291350'},
{'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707291400'},
{'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707291410'},
{'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707291420'},
{'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707291430'},
{'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707291440'},
{'Rainfall': 0.0, 'Type': 'forecast', 'Date': '201707291450'}]
Based on this information, we will turn the for statement
28 Turn the loop while getting the index with the # enumerate method 29 for index, var in enumerate(weather_info): Turn the for statement while getting the index. This is because the first item is the current measured value, so it is necessary to make the word at the end of the word the present progressive tense.
50 def retrun_rain_level(self, rainfall):
Take the value of Rainfall as an argument and define the message based on the measured value The intensity of rain and how it rains are taken from the following website of the Japan Meteorological Agency. http://www.jma.go.jp/jma/kishou/know/yougo_hp/amehyo.html
If you do this, you will get a result like the beginning
that's all
Automatically create JIRA tickets with slack bot ~ slack bot development with python ① ~ JIRA ticket with slack bot → Automation of Slack notification ~ slack bot development with python② ~ Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~ Get the bot to tell you the weather (precipitation information) using the weather information API (YOLP) provided by Yahoo ~ slack bot development ④ ~ with python
Recommended Posts