Use OpenWhisk to notify Slack of weather information.
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.
The Bluemix Open Whisk screen has also been redesigned. Is the cute.
When using Slack, it is the same as Weather Company. From the edit screen of the action, select "View Catalog" in the upper right.
Select "SLACK".
The Slack package is displayed.
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.
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.
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.
Actions are currently available in Swift, Node.js, Python and Docker. This time, I chose Python.
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?
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.
Select the SLACK you set earlier.
For the BIND on the left, select the BIND you set this time. Click Add to Sequence.
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.
Select "WEATHER".
For WEATHER, select the previously set BIND for the BIND on the left and click "Add to Sequence".
I was connected! !!
..., 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.
Change the flow of actions in.
It's in the right order! Click Save Action Sequence at the bottom right and give the sequence a name.
It has been saved! Click the sequence you created and click "Run this sequence"
Weather information will be notified to Slack.
Since this verification, the Watson package catalog has been expanded. I will definitely try using TextToSpeech and SpeechToText as well.
Recommended Posts