[PYTHON] Ask NAO (Pepper) to tell you the weather

After practicing the Python box with Choregraphe, from Livedoor's Weather Web Service I would like NAO (Pepper) to talk about the forecast for the present / tomorrow / the day after tomorrow.

Python box concept

I borrowed the wisdom of my ancestors! Please see here for details. Python Box Concept (Pepper Tech Fes Technical Session)

behavior

スクリーンショット 2016-03-26 22.25.29.png The relationship between the boxes is like this.

Box name Setting
Set Language Language: Japanese
Speech Reco. Word list:Today's Tenki;Tomorrow's weather;The weather the day after tomorrow;The end
Switch Case Speech Reco.The character string specified in the Word list of""Enclose in (double quotation marks) and set in each line
Say Double-click the box and set Language (Japanese) and any text
Python Script Write weather get from weather web service here
Say Text Two boxes, one for the acquired weather forecast and one for the end

Python Script

Preparation


In the default Python Script, all inputs and outputs are van, so Right-click the box> select Edit Box and set the input onStart and output onStopped Type to string.

script


I think it would be easier to write, but for the time being, the code that worked. .. ..

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self)

    def onLoad(self):
        pass

    def onUnload(self):
        pass

    def onInput_onStart(self, question):
        import urllib2, json
        url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=030010"
        r = urllib2.urlopen(url)
        root = json.loads(r.read())

        if question == "Today's Tenki":
            result = "The current tenki is" + root['forecasts'][0]['telop'].encode('utf8') + "is."
            self.onStopped(result)

        elif question == "Tomorrow's weather":
            result = "Tomorrow's weather" + root['forecasts'][1]['telop'].encode('utf8') + "is."
            self.onStopped(result)

        elif question == "The weather the day after tomorrow":
            result = "The weather the day after tomorrow" + root['forecasts'][2]['telop'].encode('utf8') + "is."
            self.onStopped(result)

        else:
            self.onStopped(question)

    def onInput_onStop(self):
        self.onUnload()

The code for city = sets Morioka. * For other area codes, click here.

Summary

The feeling that it is not smart is amazing.

Recommended Posts

Ask NAO (Pepper) to tell you the weather
Ask the bot to tell you the weather (precipitation information) using the weather information API (YOLP) provided by Yahoo ~ slack bot development with python ④ ~
Relive the dream you missed! (However, it is limited to Pepper)
I'm new to Python. Could you please tell me where the error is?
When you want to update the chrome driver.
Add a function to tell the weather of today to slack bot (made by python)
Don't ask "Are you sure you want to continue connecting"
The programming language you want to be able to use
I wrote you to watch the signal with Go