2015-05-09 16:34:53 +02:00
|
|
|
do
|
|
|
|
|
|
|
|
local BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
|
|
|
|
|
|
|
|
local function get_weather(location)
|
|
|
|
print("Finde Wetter in ", location)
|
2015-07-26 22:00:55 +02:00
|
|
|
local location = string.gsub(location," ","+")
|
2015-05-09 16:34:53 +02:00
|
|
|
local url = BASE_URL
|
2015-11-12 17:42:03 +01:00
|
|
|
local apikey = cred_data.owm_apikey
|
|
|
|
local url = url..'?q='..location
|
|
|
|
local url = url..'&lang=de&units=metric&APPID='..apikey
|
2015-05-09 16:34:53 +02:00
|
|
|
|
|
|
|
local b, c, h = http.request(url)
|
|
|
|
if c ~= 200 then return nil end
|
|
|
|
|
|
|
|
local weather = json:decode(b)
|
|
|
|
local city = weather.name
|
2015-11-12 17:42:03 +01:00
|
|
|
if weather.sys.country == 'none' then
|
|
|
|
country = ''
|
|
|
|
else
|
|
|
|
country = ' ('..weather.sys.country..')'
|
|
|
|
end
|
2015-11-12 19:04:57 +01:00
|
|
|
local temperature = round(weather.main.temp, 1)
|
2015-11-12 17:42:03 +01:00
|
|
|
local temp = 'Wetter in '..city..country..':\n'..temperature..'°C'
|
|
|
|
local conditions = ' | '..weather.weather[1].description
|
2015-05-09 16:34:53 +02:00
|
|
|
if weather.weather[1].main == 'Clear' then
|
2015-11-12 17:42:03 +01:00
|
|
|
conditions = conditions..' ☀'
|
2015-05-09 16:34:53 +02:00
|
|
|
elseif weather.weather[1].main == 'Clouds' then
|
2015-11-12 17:42:03 +01:00
|
|
|
conditions = conditions..' ☁☁'
|
2015-05-09 16:34:53 +02:00
|
|
|
elseif weather.weather[1].main == 'Rain' then
|
2015-11-12 17:42:03 +01:00
|
|
|
conditions = conditions..' ☔'
|
2015-05-09 16:34:53 +02:00
|
|
|
elseif weather.weather[1].main == 'Thunderstorm' then
|
2015-11-12 17:42:03 +01:00
|
|
|
conditions = conditions..' ☔☔☔☔'
|
|
|
|
else
|
|
|
|
conditions = conditions..''
|
2015-05-09 16:34:53 +02:00
|
|
|
end
|
2015-11-12 17:42:03 +01:00
|
|
|
return temp..conditions
|
2015-05-09 16:34:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function run(msg, matches)
|
2015-11-12 17:42:03 +01:00
|
|
|
local user_id = msg.from.id
|
2015-05-09 16:34:53 +02:00
|
|
|
|
|
|
|
if matches[1] ~= '/wetter' then
|
|
|
|
city = matches[1]
|
2015-11-12 17:42:03 +01:00
|
|
|
else
|
|
|
|
local set_location = get_location(user_id)
|
|
|
|
if not set_location then
|
|
|
|
city = 'Berlin'
|
|
|
|
else
|
|
|
|
city = set_location
|
|
|
|
end
|
2015-05-09 16:34:53 +02:00
|
|
|
end
|
|
|
|
local text = get_weather(city)
|
|
|
|
if not text then
|
2015-11-12 17:42:03 +01:00
|
|
|
text = 'Konnte das Wetter von dieser Stadt nicht bekommen.'
|
2015-05-09 16:34:53 +02:00
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-11-12 17:42:03 +01:00
|
|
|
description = "Wetter für deinen oder einen gewählten Ort",
|
|
|
|
usage = {
|
|
|
|
"/wetter: Wetter für deinen Wohnort (!location set [Ort])",
|
|
|
|
"/wetter (Stadt): Wetter für diese Stadt"
|
|
|
|
},
|
|
|
|
patterns = {
|
|
|
|
"^/wetter$",
|
|
|
|
"^/wetter (.*)$"
|
|
|
|
},
|
|
|
|
run = run
|
2015-05-09 16:34:53 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 16:37:42 +02:00
|
|
|
end
|