Voorbeelden
Gebruiker vraagt weer in een stad
from responses.responsemessage import ResponseMessage
from responses.responsedata import ResponseData
from responses.responsetype import ResponseType
from responses.response import Response
from responses.quickreply import Quickreply
import requests
class WeatherInfo(Response):
baseUri = "http://api.openweathermap.org/data/2.5/weather"
appId = "<JOUW APPID>"
lang = "nl"
units = "metric"
def getResponse(self, sentence, context, options={}):
cities = sentence.getEntitiesWithLabel("stad")
city = cities[0]
cityName = city.value
parameters = {
"q": cityName,
"appid": self.appId,
"lang": self.lang,
"units": self.units
}
result = requests.get(self.baseUri, params=parameters)
if result.status_code == 200:
weatherInfo = result.json()
description = weatherInfo["weather"][0]["description"]
message = "Het is momenteel {} in {}.".format(
description,
cityName.title()
)
else:
message = "Ik kan het weer in {} niet ophalen...".format(
cityName.title()
)
return ResponseMessage(
data=[
ResponseData(
message=message,
type=ResponseType.text
)
]
)De gebruiker geeft aan waar hij woont.
De gebruiker geeft zijn leeftijd aan.
Last updated
Was this helpful?