[PYTHON] Make a rain notification bot for Hangouts Chat at explosive speed

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.

What I made

Here is what I sent in the test. Actually, it is set to be sent at 17:00 at the same time. IMG_0068.jpg Hangouts Chat should be able to use Markdown-like notation. (Although highlighting is not good in the image ...)

What was used

POST to Hangouts Chat

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.

Python source code

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)

cron settings

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.

Impressions

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

Make a rain notification bot for Hangouts Chat at explosive speed
Make a LINE BOT (chat)
[TPU] [Transformers] Make BERT at explosive speed
Python template for log analysis at explosive speed
Make a bot for Skype on EC2 (CentOS)
[For play] Let's make Yubaba a LINE Bot (Python)
Build a web API server at explosive speed using hug
Easily build a GCP environment for Kaggle at high speed
I tried to create a bot for PES event notification
Let's make a Discord Bot.
Make a Tweet box for Pepper
How to make a slack bot
Create a Python development environment locally at the fastest speed (for beginners)
Make a Discord Bot that you can search for and paste images