2016-06-15 01:16:27 +02:00
|
|
|
local forecast = {}
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
require("./miku/plugins/weather")
|
|
|
|
|
2016-06-15 01:16:27 +02:00
|
|
|
function forecast:init(config)
|
|
|
|
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-06-15 01:16:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
forecast.triggers = {
|
|
|
|
"^(/f)$",
|
|
|
|
"^(/f) (.*)$",
|
|
|
|
"^(/fh)$",
|
|
|
|
"^(/fh) (.*)$",
|
|
|
|
"^(/forecast)$",
|
|
|
|
"^(/forecast) (.*)$",
|
|
|
|
"^(/forecasth)$",
|
|
|
|
"^(/forecasth) (.*)$"
|
|
|
|
}
|
2016-08-15 23:14:28 +02:00
|
|
|
forecast.inline_triggers = {
|
|
|
|
"^(f) (.+)$",
|
|
|
|
"^(fh) (.+)$",
|
|
|
|
"^(fh)$",
|
|
|
|
"^(f)$"
|
|
|
|
}
|
2016-06-15 01:16:27 +02:00
|
|
|
forecast.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[f*: Wettervorhersage für deinen Wohnort _(/location set <Ort>)_
|
|
|
|
*]]..config.cmd_pat..[[f* _<Ort>_: Wettervorhersage für diesen Ort
|
|
|
|
*]]..config.cmd_pat..[[fh*: 24-Stunden-Wettervorhersage für deine Stadt _(/location set [Ort]_
|
|
|
|
*]]..config.cmd_pat..[[fh* _<Ort>_: 24-Stunden-Wettervorhersage für diesen Ort
|
|
|
|
]]
|
|
|
|
end
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
forecast.command = 'f [Ort]'
|
2016-06-15 01:16:27 +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
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
function forecast:get_condition_symbol(weather_data)
|
|
|
|
if weather_data.icon == 'clear-day' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '☀️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'clear-night' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '🌙'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'rain' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '☔️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'snow' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '❄️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'sleet' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '🌨'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'wind' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '💨'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'fog' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '🌫'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'cloudy' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '☁️☁️'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'partly-cloudy-day' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '🌤'
|
2016-08-15 23:14:28 +02:00
|
|
|
elseif weather_data.icon == 'partly-cloudy-night' then
|
2016-06-15 01:16:27 +02:00
|
|
|
return '🌙☁️'
|
|
|
|
else
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_temp(weather, n, hourly)
|
2016-08-15 23:14:28 +02:00
|
|
|
local weather_data = weather.data[n]
|
2016-06-15 01:16:27 +02:00
|
|
|
if hourly then
|
2016-08-15 23:14:28 +02:00
|
|
|
local temperature = string.gsub(round(weather_data.temperature, 1), "%.", ",")
|
|
|
|
local condition = weather_data.summary
|
|
|
|
return temperature..'°C | '..forecast:get_condition_symbol(weather_data)..' '..condition
|
2016-06-15 01:16:27 +02:00
|
|
|
else
|
2016-08-15 23:14:28 +02:00
|
|
|
local day = string.gsub(round(weather_data.temperatureMax, 1), "%.", ",")
|
|
|
|
local night = string.gsub(round(weather_data.temperatureMin, 1), "%.", ",")
|
|
|
|
local condition = weather_data.summary
|
|
|
|
return '☀️ '..day..'°C | 🌙 '..night..'°C | '..forecast:get_condition_symbol(weather_data)..' '..condition
|
2016-06-15 01:16:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
function forecast:get_forecast(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:forecast:'..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 summary = redis:hget(hash, 'summary')
|
|
|
|
return city, summary, text, ttl
|
|
|
|
else
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
2016-06-15 01:16:27 +02:00
|
|
|
|
|
|
|
local url = BASE_URL..'/'..apikey..'/'..lat..','..lng..'?lang=de&units=si&exclude=currently,minutely,hourly,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-15 23:14:28 +02:00
|
|
|
local weather = json.decode(table.concat(response_body)).daily
|
|
|
|
local ttl = tonumber(string.sub(response_headers["cache-control"], 9))
|
2016-06-15 01:16:27 +02:00
|
|
|
local city = get_city_name(lat, lng)
|
2016-08-15 23:14:28 +02:00
|
|
|
local weather_summary = weather.summary
|
2016-06-15 01:16:27 +02:00
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
local header = '*Vorhersage für '..city..':*\n_'..weather_summary..'_\n'
|
2016-06-15 01:16:27 +02:00
|
|
|
|
|
|
|
local text = '*Heute:* '..get_temp(weather, 1)
|
|
|
|
local text = text..'\n*Morgen:* '..get_temp(weather, 2)
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
local weather_data = weather.data
|
|
|
|
for day in pairs(weather_data) do
|
2016-06-15 01:16:27 +02:00
|
|
|
if day > 2 then
|
2016-08-15 23:14:28 +02:00
|
|
|
text = text..'\n*'..convert_timestamp(weather_data[day].time, '%a, %d.%m')..'*: '..get_temp(weather, day)
|
2016-06-15 01:16:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
local text = text:gsub("Mon", "Mo")
|
|
|
|
local text = text:gsub("Tue", "Di")
|
|
|
|
local text = text:gsub("Wed", "Mi")
|
|
|
|
local text = text:gsub("Thu", "Do")
|
|
|
|
local text = text:gsub("Fri", "Fr")
|
|
|
|
local text = text:gsub("Sat", "Sa")
|
|
|
|
local text = text:gsub("Sun", "So")
|
2016-06-15 01:16:27 +02:00
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
print('Caching data...')
|
|
|
|
redis:hset(hash, 'city', city)
|
|
|
|
redis:hset(hash, 'summary', weather_summary)
|
|
|
|
redis:hset(hash, 'text', header..text)
|
|
|
|
redis:expire(hash, ttl)
|
2016-06-15 01:16:27 +02:00
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
if is_inline then
|
|
|
|
return city, weather_summary, header..text, ttl
|
|
|
|
else
|
|
|
|
return header..text
|
|
|
|
end
|
2016-06-15 01:16:27 +02:00
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
function forecast:get_forecast_hourly(lat, lng, is_inline)
|
2016-06-15 01:16:27 +02:00
|
|
|
print('Finde stündliches Wetter in '..lat..', '..lng)
|
2016-08-15 23:14:28 +02:00
|
|
|
local hash = 'telegram:cache:forecast:'..lat..','..lng..':hourly'
|
|
|
|
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 summary = redis:hget(hash, 'summary')
|
|
|
|
return city, summary, text, ttl
|
|
|
|
else
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
2016-06-15 01:16:27 +02:00
|
|
|
|
|
|
|
local url = BASE_URL..'/'..apikey..'/'..lat..','..lng..'?lang=de&units=si&exclude=currently,minutely,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-15 23:14:28 +02:00
|
|
|
local weather = json.decode(table.concat(response_body)).hourly
|
|
|
|
local ttl = tonumber(string.sub(response_headers["cache-control"], 9))
|
2016-06-15 01:16:27 +02:00
|
|
|
local city = get_city_name(lat, lng)
|
2016-08-15 23:14:28 +02:00
|
|
|
local weather_summary = weather.summary
|
2016-06-15 01:16:27 +02:00
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
local header = '*24-Stunden-Vorhersage für '..city..':*\n_'..weather_summary..'_'
|
2016-06-15 01:16:27 +02:00
|
|
|
local text = ""
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
local weather_data = weather.data
|
|
|
|
for hour in pairs(weather_data) do
|
2016-06-15 01:16:27 +02:00
|
|
|
if hour < 26 then
|
2016-08-15 23:14:28 +02:00
|
|
|
text = text..'\n*'..convert_timestamp(weather_data[hour].time, '%H:%M Uhr')..'* | '..get_temp(weather, hour, true)
|
2016-06-15 01:16:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
print('Caching data...')
|
|
|
|
redis:hset(hash, 'city', city)
|
|
|
|
redis:hset(hash, 'summary', weather_summary)
|
|
|
|
redis:hset(hash, 'text', header..text)
|
|
|
|
redis:expire(hash, ttl)
|
|
|
|
|
|
|
|
if is_inline then
|
|
|
|
return city, weather_summary, header..text, ttl
|
|
|
|
else
|
|
|
|
return header..text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function forecast:inline_callback(inline_query, config, matches)
|
|
|
|
local user_id = inline_query.from.id
|
|
|
|
if matches[2] then
|
|
|
|
city = matches[2]
|
|
|
|
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
|
2016-06-15 01:16:27 +02:00
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
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
|
|
|
if matches[1] == 'f' then
|
|
|
|
title, description, text, ttl = forecast:get_forecast(lat, lng, true)
|
|
|
|
else
|
|
|
|
title, description, text, ttl = forecast:get_forecast_hourly(lat, lng, true)
|
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
if not title and not description 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 results = '[{"type":"article","id":"28062013","title":"'..title..'","description":"'..description..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/weather/cloudy.jpg","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
|
|
|
|
|
|
|
|
function forecast:action(msg, config, matches)
|
|
|
|
local user_id = msg.from.id
|
|
|
|
|
|
|
|
if matches[2] then
|
|
|
|
city = matches[2]
|
|
|
|
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
|
|
|
|
|
|
|
|
if matches[1] == '/forecasth' or matches[1] == '/fh' then
|
|
|
|
text = forecast:get_forecast_hourly(lat, lng)
|
|
|
|
else
|
|
|
|
text = forecast:get_forecast(lat, lng)
|
|
|
|
end
|
|
|
|
if not text then
|
|
|
|
text = '*Konnte die Wettervorhersage für diese Stadt nicht bekommen.*'
|
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, text, true)
|
2016-06-15 01:16:27 +02:00
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
return forecast
|