2016-04-11 06:04:47 +02:00
|
|
|
local weather = {}
|
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function weather:init(config)
|
2016-06-15 01:16:27 +02:00
|
|
|
if not cred_data.forecastio_apikey then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Fehlender Key: forecastio_apikey.')
|
|
|
|
print('weather.lua wird nicht aktiviert.')
|
2016-06-15 01:16:27 +02:00
|
|
|
return
|
|
|
|
elseif not cred_data.google_apikey then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Fehlender Key: google_apikey.')
|
|
|
|
print('weather.lua wird nicht aktiviert.')
|
2016-04-11 06:04:47 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-06-15 01:16:27 +02:00
|
|
|
weather.triggers = {
|
|
|
|
"^/wetter$",
|
|
|
|
"^/wetter (.*)$",
|
|
|
|
"^/w$",
|
|
|
|
"^/w (.*)$"
|
|
|
|
}
|
2016-08-15 23:14:28 +02:00
|
|
|
weather.inline_triggers = {
|
|
|
|
"^w (.+)$",
|
|
|
|
"^w$"
|
|
|
|
}
|
2016-06-15 01:16:27 +02:00
|
|
|
weather.doc = [[*
|
2016-08-17 21:28:55 +02:00
|
|
|
]]..config.cmd_pat..[[w*: Wetter für deinen Wohnort _(/location set [Ort])_
|
|
|
|
*]]..config.cmd_pat..[[w* _<Ort>_: Wetter für diesen Ort
|
2016-06-15 01:16:27 +02:00
|
|
|
]]
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
weather.command = 'w [Ort]'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-09-20 16:56:37 +02:00
|
|
|
local BASE_URL = "https://api.darksky.net/forecast"
|
2016-06-15 01:16:27 +02:00
|
|
|
local apikey = cred_data.forecastio_apikey
|
|
|
|
local google_apikey = cred_data.google_apikey
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-15 01:16:27 +02:00
|
|
|
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
|
2016-08-01 21:07:27 +02:00
|
|
|
local res, code = https.request(url)
|
2016-06-15 01:16:27 +02:00
|
|
|
if code ~= 200 then return 'Unbekannte Stadt' end
|
2016-08-01 21:07:27 +02:00
|
|
|
local data = json.decode(res).results[1]
|
2016-06-15 01:16:27 +02:00
|
|
|
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-11-25 03:22:04 +01:00
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
function get_city_coordinates(city, config)
|
|
|
|
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...')
|
|
|
|
coords = utilities.get_coords(city, config)
|
|
|
|
lat = coords.lat
|
|
|
|
lng = coords.lon
|
|
|
|
end
|
|
|
|
|
|
|
|
if not lat and not lng then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
redis:hset('telegram:cache:weather:'..string.lower(city), 'lat', lat)
|
|
|
|
redis:hset('telegram:cache:weather:'..string.lower(city), 'lng', lng)
|
|
|
|
return lat, lng
|
|
|
|
end
|
|
|
|
|
|
|
|
function weather:get_weather(lat, lng, is_inline)
|
2016-06-15 01:16:27 +02:00
|
|
|
print('Finde Wetter in '..lat..', '..lng)
|
2016-08-15 23:14:28 +02:00
|
|
|
local hash = 'telegram:cache:weather:'..lat..','..lng
|
|
|
|
|
|
|
|
local text = redis:hget(hash, 'text')
|
|
|
|
if text then
|
|
|
|
print('...aus dem Cache')
|
|
|
|
if is_inline then
|
|
|
|
local ttl = redis:ttl(hash)
|
|
|
|
local city = redis:hget(hash, 'city')
|
|
|
|
local temperature = redis:hget(hash, 'temperature')
|
|
|
|
local weather_icon = redis:hget(hash, 'weather_icon')
|
|
|
|
local condition = redis:hget(hash, 'condition')
|
|
|
|
return city, condition..' bei '..temperature..' °C', weather_icon, text, ttl
|
|
|
|
else
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-15 01:16:27 +02:00
|
|
|
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)
|
|
|
|
}
|
2016-08-15 23:14:28 +02:00
|
|
|
local ok, response_code, response_headers = https.request(request_constructor)
|
2016-06-15 01:16:27 +02:00
|
|
|
if not ok then return nil end
|
2016-08-01 21:07:27 +02:00
|
|
|
local data = json.decode(table.concat(response_body))
|
2016-08-15 23:14:28 +02:00
|
|
|
local ttl = tonumber(string.sub(response_headers["cache-control"], 9))
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-15 01:16:27 +02:00
|
|
|
|
|
|
|
local weather = data.currently
|
|
|
|
local city = get_city_name(lat, lng)
|
|
|
|
local temperature = string.gsub(round(weather.temperature, 1), "%.", ",")
|
|
|
|
local feelslike = string.gsub(round(weather.apparentTemperature, 1), "%.", ",")
|
|
|
|
local temp = '*Wetter in '..city..':*\n'..temperature..' °C'
|
2016-08-15 23:14:28 +02:00
|
|
|
local weather_summary = weather.summary
|
|
|
|
local conditions = ' | '..weather_summary
|
|
|
|
local weather_icon = weather.icon
|
|
|
|
if weather_icon == 'clear-day' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' ☀️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'clear-night' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' 🌙'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'rain' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' ☔️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'snow' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' ❄️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'sleet' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' 🌨'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'wind' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' 💨'
|
|
|
|
elseif weather.icon == 'fog' then
|
|
|
|
conditions = conditions..' 🌫'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'cloudy' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' ☁️☁️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'partly-cloudy-day' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' 🌤'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_icon == 'partly-cloudy-night' then
|
2016-06-15 01:16:27 +02:00
|
|
|
conditions = conditions..' 🌙☁️'
|
|
|
|
else
|
|
|
|
conditions = conditions..''
|
|
|
|
end
|
|
|
|
local windspeed = ' | 💨 '..string.gsub(round(weather.windSpeed, 1), "%.", ",")..' m/s'
|
|
|
|
|
|
|
|
local text = temp..conditions..windspeed
|
|
|
|
|
|
|
|
if temperature ~= feelslike then
|
|
|
|
text = text..'\n(gefühlt: '..feelslike..' °C)'
|
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
print('Caching data...')
|
|
|
|
redis:hset(hash, 'city', city)
|
|
|
|
redis:hset(hash, 'temperature', temperature)
|
|
|
|
redis:hset(hash, 'weather_icon', weather_icon)
|
|
|
|
redis:hset(hash, 'condition', weather_summary)
|
|
|
|
redis:hset(hash, 'text', text)
|
|
|
|
redis:expire(hash, ttl)
|
|
|
|
|
|
|
|
if is_inline then
|
|
|
|
return city, weather_summary..' bei '..temperature..' °C', weather_icon, text, ttl
|
|
|
|
else
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function weather:inline_callback(inline_query, config, matches)
|
|
|
|
local user_id = inline_query.from.id
|
|
|
|
if matches[1] ~= 'w' then
|
|
|
|
city = matches[1]
|
|
|
|
is_personal = false
|
|
|
|
else
|
|
|
|
local set_location = get_location(user_id)
|
|
|
|
is_personal = true
|
|
|
|
if not set_location then
|
|
|
|
city = 'Berlin, Deutschland'
|
|
|
|
else
|
|
|
|
city = set_location
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local lat, lng = get_city_coordinates(city, config)
|
2016-08-24 15:38:29 +02:00
|
|
|
if not lat and not lng then abort_inline_query(inline_query) return end
|
2016-08-15 23:14:28 +02:00
|
|
|
|
|
|
|
local title, description, icon, text, ttl = weather:get_weather(lat, lng, true)
|
2016-08-24 15:38:29 +02:00
|
|
|
if not title and not description and not icon and not text and not ttl then abort_inline_query(inline_query) return end
|
2016-08-15 23:14:28 +02:00
|
|
|
|
|
|
|
local text = text:gsub('\n', '\\n')
|
|
|
|
local thumb_url = 'https://anditest.perseus.uberspace.de/inlineQuerys/weather/'
|
|
|
|
if icon == 'clear-day' or icon == 'partly-cloudy-day' then
|
|
|
|
thumb_url = thumb_url..'day.jpg'
|
2016-08-17 21:28:55 +02:00
|
|
|
elseif icon == 'clear-night' then
|
2016-08-15 23:14:28 +02:00
|
|
|
thumb_url = thumb_url..'night.jpg'
|
2016-08-17 21:28:55 +02:00
|
|
|
elseif icon == 'partly-cloudy-night' then
|
|
|
|
thumb_url = thumb_url..'cloudy-night.jpg'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif icon == 'rain' then
|
|
|
|
thumb_url = thumb_url..'rain.jpg'
|
|
|
|
elseif icon == 'snow' then
|
|
|
|
thumb_url = thumb_url..'snow.jpg'
|
|
|
|
else
|
|
|
|
thumb_url = thumb_url..'cloudy.jpg'
|
|
|
|
end
|
|
|
|
local results = '[{"type":"article","id":"19122006","title":"'..title..'","description":"'..description..'","thumb_url":"'..thumb_url..'","thumb_width":80,"thumb_height":80,"input_message_content":{"message_text":"'..text..'", "parse_mode":"Markdown"}}]'
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_inline_query(inline_query, results, ttl, is_personal)
|
2016-06-15 01:16:27 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-15 01:16:27 +02:00
|
|
|
function weather:action(msg, config, matches)
|
|
|
|
local user_id = msg.from.id
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-15 01:16:27 +02:00
|
|
|
if matches[1] ~= '/wetter' and matches[1] ~= '/w' then
|
|
|
|
city = matches[1]
|
|
|
|
else
|
|
|
|
local set_location = get_location(user_id)
|
|
|
|
if not set_location then
|
|
|
|
city = 'Berlin, Deutschland'
|
|
|
|
else
|
|
|
|
city = set_location
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
local lat, lng = get_city_coordinates(city, config)
|
2016-06-15 01:16:27 +02:00
|
|
|
if not lat and not lng then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, '*Diesen Ort gibt es nicht!*', true)
|
2016-06-15 01:16:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local text = weather:get_weather(lat, lng)
|
|
|
|
if not text then
|
|
|
|
text = 'Konnte das Wetter von dieser Stadt nicht bekommen.'
|
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, text, true)
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
return weather
|