Splitting commands in plugins

This commit is contained in:
David Muñoz
2014-11-04 16:09:08 +01:00
parent bc6df5d683
commit f8161ca1cf
12 changed files with 354 additions and 240 deletions

29
plugins/9gag.lua Normal file
View File

@ -0,0 +1,29 @@
function get_9GAG()
b = http.request("http://api-9gag.herokuapp.com/")
local gag = json:decode(b)
math.randomseed(os.time())
i = math.random(#gag) -- random max json table size (# is an operator o.O)
local link_image = gag[i].src
local title = gag[i].title
if link_image:sub(0,2) == '//' then
link_image = msg.text:sub(3,-1)
end
return link_image, title
end
function run(msg, matches)
local receiver = get_receiver(msg)
url, title = get_9GAG()
file_path = download_to_file(url)
send_photo(receiver, file_path, ok_cb, false)
return title
end
return {
description = "send random url image from 9gag",
usage = "9gag",
regexp = "^9gag$",
run = run
}

12
plugins/echo.lua Normal file
View File

@ -0,0 +1,12 @@
function run(msg, matches)
return matches[1]
end
return {
description = "echoes the msg",
usage = "echo [whatever]",
regexp = "^echo (.*)$",
run = run
}

27
plugins/eur.lua Normal file
View File

@ -0,0 +1,27 @@
function getEURUSD(usd)
b = http.request("http://webrates.truefx.com/rates/connect.html?c=EUR/USD&f=csv&s=n")
local rates = b:split(", ")
local symbol = rates[1]
local timestamp = rates[2]
local sell = rates[3]..rates[4]
local buy = rates[5]..rates[6]
text = symbol..'\n'..'Buy: '..buy..'\n'..'Sell: '..sell
if usd then
eur = tonumber(usd) / tonumber(buy)
text = text.."\n "..usd.."USD = "..eur.."EUR"
end
return text
end
function run(msg, matches)
return getEURUSD(matches[1])
end
return {
description = "EURUSD market value",
usage = "eur [USD]",
regexp = "^eur (%d+[%d%.]*)$",
run = run
}

21
plugins/fortunes_uc3m.lua Normal file
View File

@ -0,0 +1,21 @@
function get_fortunes_uc3m()
math.randomseed(os.time())
local i = math.random(0,178) -- max 178
local web = "http://www.gul.es/fortunes/f"..i
b, c, h = http.request(web)
return b
end
function run(msg, matches)
return get_fortunes_uc3m()
end
return {
description = "Fortunes from Universidad Carlos III",
usage = "uc3m",
regexp = "^uc3m$",
run = run
}

12
plugins/hello.lua Normal file
View File

@ -0,0 +1,12 @@
function run(msg, matches)
return "Hello," .. matches[1]
end
return {
description = "Says hello to someone",
usage = "say hello to [name]",
regexp = "^say hello to (.*)$",
run = run
}

16
plugins/help.lua Normal file
View File

@ -0,0 +1,16 @@
function run(msg, matches)
local ret = ""
for k, dict in pairs(plugins) do
ret = ret .. dict.usage .. " -> " .. dict.description .. "\n"
end
return ret
end
return {
description = "Lists all available commands",
usage = "help",
regexp = "^help$",
run = run
}

33
plugins/images.lua Normal file
View File

@ -0,0 +1,33 @@
function getGoogleImage(text)
text = URL.escape(text)
for i = 1, 5, 1 do -- Try 5 times
local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q="
b = http.request(api..text)
local google = json:decode(b)
if (google.responseStatus == 200) then -- OK
math.randomseed(os.time())
i = math.random(#google.responseData.results) -- Random image from results
return google.responseData.results[i].url
end
end
end
function run(msg, matches)
local receiver = get_receiver(msg)
local text = msg.text:sub(6,-1)
local url = getGoogleImage(text)
local file_path = download_to_file(url)
print(file_path)
send_photo(receiver, file_path, ok_cb, false)
return nil
end
return {
description = "search image with Google API and sends it",
usage = "img [topic]",
regexp = "^img (.*)$",
run = run
}

14
plugins/ping.lua Normal file
View File

@ -0,0 +1,14 @@
function run(msg, matches)
local receiver = get_receiver(msg)
print('receiver: '..receiver)
return "pong"
end
return {
description = "bot sends pong",
usage = "ping",
regexp = "^ping$",
run = run
}

46
plugins/rae.lua Normal file
View File

@ -0,0 +1,46 @@
function getDulcinea( text )
-- Powered by https://github.com/javierhonduco/dulcinea
local api = "http://dulcinea.herokuapp.com/api/?query="
b = http.request(api..text)
dulcinea = json:decode(b)
if dulcinea.status == "error" then
return "Error: " .. dulcinea.message
end
while dulcinea.type == "multiple" do
text = dulcinea.response[1].id
b = http.request(api..text)
dulcinea = json:decode(b)
end
vardump(dulcinea)
local text = ""
local responses = #dulcinea.response
if (responses > 5) then
responses = 5
end
for i = 1, responses, 1 do
text = text .. dulcinea.response[i].word .. "\n"
local meanings = #dulcinea.response[i].meanings
if (meanings > 5) then
meanings = 5
end
for j = 1, meanings, 1 do
local meaning = dulcinea.response[i].meanings[j].meaning
text = text .. meaning .. "\n\n"
end
end
return text
end
function run(msg, matches)
return getDulcinea(matches[1])
end
return {
description = "Spanish dictionary",
usage = "rae [word]",
regexp = "^rae (.*)$",
run = run
}

16
plugins/version.lua Normal file
View File

@ -0,0 +1,16 @@
function run(msg, matches)
return 'James Bot '.. VERSION .. [[
Licencia GNU v2, código disponible en http://git.io/6jdjGg
Al Bot le gusta la gente solidaria.
Puedes hacer una donación a la ONG que decidas y ayudar a otras personas.]]
end
return {
description = "Shows the bot version",
usage = "version",
regexp = "^version$",
run = run
}

39
plugins/weather.lua Normal file
View File

@ -0,0 +1,39 @@
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]",
regexp = "^weather(.*)$",
run = run
}