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
2014-11-04 22:06:59 +01:00

40 lines
1.2 KiB
Lua

function get_weather(location)
print("Finding weather in ", location)
b, c, h = http.request("http://api.openweathermap.org/data/2.5/weather?q=" .. location .. "&units=metric")
weather = json:decode(b)
print("Weather returns", weather)
local city = weather.name
local country = weather.sys.country
temp = 'The temperature in ' .. city .. ' (' .. country .. ')'
temp = temp .. ' is ' .. weather.main.temp .. '°C'
conditions = 'Current conditions are: ' .. weather.weather[1].description
if weather.weather[1].main == 'Clear' then
conditions = conditions .. ''
elseif weather.weather[1].main == 'Clouds' then
conditions = conditions .. ' ☁☁'
elseif weather.weather[1].main == 'Rain' then
conditions = conditions .. ''
elseif weather.weather[1].main == 'Thunderstorm' then
conditions = conditions .. ' ☔☔☔☔'
end
return temp .. '\n' .. conditions
end
function run(msg, matches)
if string.len(matches[1]) > 2 then
city = matches[1]
else
city = "Madrid,ES"
end
return get_weather(city)
end
return {
description = "weather in that city (Madrid is default)",
usage = "!weather (city)",
patterns = {"^!weather(.*)$"},
run = run
}