[PYTHON] Slack notification of weather information on OpenWhisk

Use OpenWhisk to notify Slack of weather information.

snip_20161031002150.png

The method of acquiring weather information is described in the following article.

This time, I will describe the flow of notifying Slack of the data acquired by Weather Company.

Use Slack's package catalog

The Bluemix Open Whisk screen has also been redesigned. Is the cute.

snip_20161223024154.png

When using Slack, it is the same as Weather Company. From the edit screen of the action, select "View Catalog" in the upper right.

snip_20161025222009.png

Select "SLACK".

snip_20161025222036.png

The Slack package is displayed.

snip_20161025222114.png

Let's take a look at the sample input. In order to notify Slack, it seems that you should enter the following parameters in JSON.

{
    "channel": "myChannel",
    "text": "Hello OpenWhisk!",
    "url": "https://hooks.slack.com/services/XYZ/ABCDEFG/12345678",
    "username": "openwhisk"
}

The contents are the URL, user name, and channel name of Slack's Webhook. And the text to notify. Select NEW BIDING on the left.

snip_20161223222258.png

Enter the url and channel according to the slack you want to notify, and enter any user name, and save the configuration. Now all you have to do is create the text to notify you.

Process the information obtained by Weather Company for Slack notification

In the previous article, the data acquired by The Weather Company is as follows.

{
  "metadata": {
    "latitude": 35.68,
    "status_code": 200,
    "longitude": 139.76,

...(abridgement)...

  },
  "observation": {
    "dewPt": 1,
    "blunt_phrase": null,
    "pressure_tend": 2,
    "pressure_desc": "Falling",

...(abridgement)...

    "wx_phrase": null,
    "terse_phrase": null,
    "uv_index": 3
  }
}

Get the parameters you need and write a little code in Slack to stuff them into the text parameters. Select Create Action.

snip_20161022171511.png

Actions are currently available in Swift, Node.js, Python and Docker. This time, I chose Python.

snip_20161029203312.png

Coding is as usual ** I won't do my best. ** **

weather_to_slack.py


import sys

def main(dict):
    slack = {}
    
    latitude = dict["metadata"]["latitude"]
    longitude = dict["metadata"]["longitude"]
    pressure = dict["observation"]["pressure"]
    temp = dict["observation"]["temp"]
    rh = dict["observation"]["rh"]
    
    message = "temperature:" + str(temp) + "\n" + "Humidity:" + str(rh) + "\n" + "Atomosheric pressure:" + str(pressure)
    
    slack["text"] = message
    return slack

Since the parameters received from WeatherCompany are passed in dictionary type, extract only the parameters you want and pack them in the text of slack. I wanted to display the text in Japanese, so I tried and errored it, but it didn't work and I had no choice but to write it in English. Is it possible to transfer Japanese JSON?

Execute by specifying the processing flow in the sequence

The action is ready. Create a "sequence" that combines each action and try to execute it. From the action you just created, select "Link to Sequence" at the bottom right.

snip_20161223225348.png

Select the SLACK you set earlier.

snip_20161223225422.png

For the BIND on the left, select the BIND you set this time. Click Add to Sequence.

snip_20161223225637.png

From the Weather_to_slack I made this time, I have a flow to pass it to the Slack package! Next, add the Weather Company from which the data was acquired. Select "Extend" at the bottom right.

snip_20161223225705.png

Select "WEATHER".

snip_20161223225732.png

For WEATHER, select the previously set BIND for the BIND on the left and click "Add to Sequence".

snip_20161223225755.png

I was connected! !!

snip_20161223225832.png

..., but the order of action execution is Korejanai. Sokojanai.

But it's okay. You can change the order of execution of actions by clicking the arrow on the upper right of the action of Weather.

  1. Get weather information with Weather
  2. Use the created application to stuff the weather information into the text of slack.
  3. Throw in slack.

Change the flow of actions in.

snip_20161223225906.png

It's in the right order! Click Save Action Sequence at the bottom right and give the sequence a name.

snip_20161223225958.png

It has been saved! Click the sequence you created and click "Run this sequence"

snip_20161223230045.png

Weather information will be notified to Slack.

snip_20161031002150.png

in conclusion

Since this verification, the Watson package catalog has been expanded. I will definitely try using TextToSpeech and SpeechToText as well.

Recommended Posts

Slack notification of weather information on OpenWhisk
Lunch invitation bot on Slack
Slack notification of weather information on OpenWhisk
Acquisition of weather information (DarkSky)
Post images of Papillon regularly on Python + AWS Lambda + Slack
Set information such as length on the edge of NetworkX