This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/weather.lua

73 lines
1.8 KiB
Lua
Raw Normal View History

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," ","+")
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
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
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
if weather.weather[1].main == 'Clear' then
2015-11-12 17:42:03 +01:00
conditions = conditions..''
elseif weather.weather[1].main == 'Clouds' then
2015-11-12 17:42:03 +01:00
conditions = conditions..' ☁☁'
elseif weather.weather[1].main == 'Rain' then
2015-11-12 17:42:03 +01:00
conditions = conditions..''
elseif weather.weather[1].main == 'Thunderstorm' then
2015-11-12 17:42:03 +01:00
conditions = conditions..' ☔☔☔☔'
else
conditions = conditions..''
end
2015-11-12 17:42:03 +01:00
return temp..conditions
end
local function run(msg, matches)
2015-11-12 17:42:03 +01:00
local user_id = msg.from.id
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
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.'
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
}
end