2015-05-09 16:34:53 +02:00
|
|
|
do
|
|
|
|
|
2016-02-16 18:53:15 +01:00
|
|
|
require("./plugins/time")
|
2015-05-09 16:34:53 +02:00
|
|
|
|
2016-02-16 18:53:15 +01:00
|
|
|
local BASE_URL = "https://api.forecast.io/forecast"
|
|
|
|
local apikey = cred_data.forecastio_apikey
|
|
|
|
local google_apikey = cred_data.google_apikey
|
2015-05-09 16:34:53 +02:00
|
|
|
|
2016-02-16 18:53:15 +01:00
|
|
|
local function get_city_name(lat, lng)
|
|
|
|
local city = redis:hget('telegram:cache:weather:pretty_names', lat..','..lng)
|
|
|
|
if city then return city end
|
|
|
|
local url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='..lat..','..lng..'&result_type=political&language=de&key='..google_apikey
|
|
|
|
local res, code = https.request(url)
|
|
|
|
if code ~= 200 then return 'Unbekannte Stadt' end
|
|
|
|
local data = json:decode(res).results[1]
|
|
|
|
local city = data.formatted_address
|
|
|
|
print('Setting '..lat..','..lng..' in redis hash telegram:cache:weather:pretty_names to "'..city..'"')
|
|
|
|
redis:hset('telegram:cache:weather:pretty_names', lat..','..lng, city)
|
|
|
|
return city
|
|
|
|
end
|
2015-05-09 16:34:53 +02:00
|
|
|
|
2016-02-16 18:53:15 +01:00
|
|
|
local function get_weather(lat, lng)
|
|
|
|
print('Finde Wetter in '..lat..', '..lng)
|
|
|
|
local text = redis:get('telegram:cache:weather:'..lat..','..lng)
|
|
|
|
if text then print('...aus dem Cache..') return text end
|
|
|
|
|
|
|
|
local url = BASE_URL..'/'..apikey..'/'..lat..','..lng..'?lang=de&units=si&exclude=minutely,hourly,daily,alerts,flags'
|
|
|
|
|
|
|
|
local response_body = {}
|
|
|
|
local request_constructor = {
|
|
|
|
url = url,
|
|
|
|
method = "GET",
|
|
|
|
sink = ltn12.sink.table(response_body)
|
|
|
|
}
|
|
|
|
local ok, response_code, response_headers, response_status_line = https.request(request_constructor)
|
|
|
|
if not ok then return nil end
|
|
|
|
local data = json:decode(table.concat(response_body))
|
|
|
|
local ttl = string.sub(response_headers["cache-control"], 9)
|
|
|
|
|
|
|
|
|
|
|
|
local weather = data.currently
|
|
|
|
local city = get_city_name(lat, lng)
|
|
|
|
local temperature = round(weather.temperature, 1)
|
|
|
|
local feelslike = round(weather.apparentTemperature, 1)
|
|
|
|
local temp = 'Wetter in '..city..':\n'..temperature..' °C'
|
|
|
|
local conditions = ' | '..weather.summary
|
|
|
|
if weather.icon == 'clear-day' then
|
|
|
|
conditions = conditions..' ☀️'
|
|
|
|
elseif weather.icon == 'clear-night' then
|
|
|
|
conditions = conditions..' 🌙'
|
|
|
|
elseif weather.icon == 'rain' then
|
|
|
|
conditions = conditions..' ☔️'
|
|
|
|
elseif weather.icon == 'snow' then
|
|
|
|
conditions = conditions..' ❄️'
|
|
|
|
elseif weather.icon == 'sleet' then
|
|
|
|
conditions = conditions..' 🌨'
|
|
|
|
elseif weather.icon == 'wind' then
|
|
|
|
conditions = conditions..' 💨'
|
|
|
|
elseif weather.icon == 'fog' then
|
2016-01-30 21:03:21 +01:00
|
|
|
conditions = conditions..' 🌫'
|
2016-02-16 18:53:15 +01:00
|
|
|
elseif weather.icon == 'cloudy' then
|
|
|
|
conditions = conditions..' ☁️☁️'
|
|
|
|
elseif weather.icon == 'partly-cloudy-day' then
|
|
|
|
conditions = conditions..' 🌤'
|
|
|
|
elseif weather.icon == 'partly-cloudy-night' then
|
|
|
|
conditions = conditions..' 🌙☁️'
|
2015-11-12 17:42:03 +01:00
|
|
|
else
|
|
|
|
conditions = conditions..''
|
2015-05-09 16:34:53 +02:00
|
|
|
end
|
2016-02-16 18:53:15 +01:00
|
|
|
local windspeed = ' | 💨 '..round(weather.windSpeed, 1)..' m/s'
|
|
|
|
|
|
|
|
local text = temp..conditions..windspeed
|
|
|
|
|
2016-06-01 20:46:40 +02:00
|
|
|
if temperature ~= feelslike then
|
2016-02-16 18:53:15 +01:00
|
|
|
text = text..'\n(gefühlt: '..feelslike..' °C)'
|
|
|
|
end
|
|
|
|
|
|
|
|
cache_data('weather', lat..','..lng, text, tonumber(ttl), 'key')
|
|
|
|
return text
|
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
|
|
|
|
2016-02-16 18:53:15 +01:00
|
|
|
if matches[1] ~= '/wetter' and matches[1] ~= '/w' then
|
2015-05-09 16:34:53 +02:00
|
|
|
city = matches[1]
|
2015-11-12 17:42:03 +01:00
|
|
|
else
|
|
|
|
local set_location = get_location(user_id)
|
|
|
|
if not set_location then
|
2016-02-16 18:53:15 +01:00
|
|
|
city = 'Berlin, Deutschland'
|
2015-11-12 17:42:03 +01:00
|
|
|
else
|
|
|
|
city = set_location
|
|
|
|
end
|
2015-05-09 16:34:53 +02:00
|
|
|
end
|
2016-02-16 18:53:15 +01:00
|
|
|
|
|
|
|
local lat = redis:hget('telegram:cache:weather:'..string.lower(city), 'lat')
|
|
|
|
local lng = redis:hget('telegram:cache:weather:'..string.lower(city), 'lng')
|
|
|
|
if not lat and not lng then
|
|
|
|
print('Koordinaten nicht eingespeichert, frage Google...')
|
|
|
|
lat,lng = get_latlong(city)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not lat and not lng then
|
|
|
|
return 'Den Ort "'..city..'" gibt es nicht!'
|
|
|
|
end
|
|
|
|
|
|
|
|
redis:hset('telegram:cache:weather:'..string.lower(city), 'lat', lat)
|
|
|
|
redis:hset('telegram:cache:weather:'..string.lower(city), 'lng', lng)
|
|
|
|
|
|
|
|
local text = get_weather(lat, lng)
|
2015-05-09 16:34:53 +02:00
|
|
|
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 = {
|
2016-06-01 20:46:40 +02:00
|
|
|
"/wetter: Wetter für deinen Wohnort (!location set [Ort])",
|
2015-11-12 17:42:03 +01:00
|
|
|
"/wetter (Stadt): Wetter für diese Stadt"
|
|
|
|
},
|
|
|
|
patterns = {
|
|
|
|
"^/wetter$",
|
2016-02-16 18:53:15 +01:00
|
|
|
"^/wetter (.*)$",
|
|
|
|
"^/w$",
|
|
|
|
"^/w (.*)$"
|
2015-11-12 17:42:03 +01:00
|
|
|
},
|
|
|
|
run = run
|
2015-05-09 16:34:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-01 20:46:40 +02:00
|
|
|
end
|