Read @ murs313's Make a Slack rain notification bot at explosive speed [Me ●●] I wanted to make the same thing right away, but we couldn't use Slack.
That's why I made a ** Hangouts Chat version ** rain notification bot. It's boring to make it too much with GAS, so I implemented it with Python and cron.
Here is what I sent in the test. Actually, it is set to be sent at 17:00 at the same time. Hangouts Chat should be able to use Markdown-like notation. (Although highlighting is not good in the image ...)
Hangouts Chat can use incoming webhooks. I referred to @ iitenkida7's [Super Easy] Posting a message easily from the API using the incoming webhooks of Hangouts Chat.
weather.py
import requests
weather_url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=130010' # Tokyo
resp = requests.get(weather_url)
data = resp.json()
#It's partially blurred, but you can copy it.
webhook_url = "https://chat.googleapis.com/v1/spaces/HOGEHOGE/messages?key={YOUR_KEY}&token={YOUR_TOKEN}"
tomorrow_weather = data['forecasts'][1]
telop = tomorrow_weather['telop']
max_temp = tomorrow_weather['temperature']['max']
min_temp = tomorrow_weather['temperature']['min']
image = tomorrow_weather['image']['url']
text = ''
text += 'Tomorrow's weather'
text += '*' + telop + '*'
text += 'is.\n'
#Sometimes max and min cannot be obtained for some reason
if max_temp is not None:
text += 'Highest temperature'
text += max_temp.get('celsius')
text += '℃'
if min_temp is not None:
text += '/Lowest Temperature'
text += min_temp.get('celsius')
text += '℃\n'
text += image
content = {"text": text}
response = requests.post(webhook_url, json=content)
You can refer to here for cron settings.
To set up cron, hit `` `crontab -e```.
$ crontab -e
Then Vim will open the configuration file, so enter the time to execute and the command to execute. This time, it is set to be placed directly under the home directory.
0 17 * * * python3 ~/weather.py
When you save the file and close Vim, if `crontab: installing new crontab`
is displayed, the setting is complete.
To check the operation, open `` `crontab -e``` again and open it.
*/1 * * * * python3 ~/weather.py
If you write like, the command will be executed every minute.
The links I used as a reference were easy to understand, so I was able to set them without any clogging. Now I don't forget to bring my PC home the day before the rainy day.
...... Well, the other day we heard that remote control is prohibited.
Recommended Posts