multiple patterns
This commit is contained in:
parent
6ef55411a1
commit
7d8abde1d1
30
bot/bot.lua
30
bot/bot.lua
@ -1,7 +1,7 @@
|
||||
http = require("socket.http")
|
||||
URL = require("socket.url")
|
||||
json = (loadfile "./bot/JSON.lua")()
|
||||
lrexlib = require("rex_pcre")
|
||||
-- lrexlib = require("rex_pcre")
|
||||
|
||||
VERSION = 'v0.6'
|
||||
|
||||
@ -123,21 +123,23 @@
|
||||
-- Where magic happens
|
||||
function do_action(msg)
|
||||
local receiver = get_receiver(msg)
|
||||
local text = msg.text:sub(2) -- removes the '!'
|
||||
print("Received msg", msg.text)
|
||||
--local text = msg.text:sub(2) -- removes the '!'
|
||||
local text = msg.text
|
||||
print("Received msg", text)
|
||||
for name, desc in pairs(plugins) do
|
||||
regexp = desc.regexp
|
||||
runmethod = desc.run
|
||||
print("Trying", text, "against", regexp)
|
||||
matches = { string.match(text, regexp) }
|
||||
print("Trying module", name)
|
||||
for k, pattern in pairs(desc.patterns) do
|
||||
print("Trying", text, "against", pattern)
|
||||
matches = { string.match(text, pattern) }
|
||||
if matches[1] then
|
||||
print(" matches!")
|
||||
result = runmethod(msg, matches)
|
||||
print(" should return", result)
|
||||
if (result) then
|
||||
send_msg(receiver, result, ok_cb, false)
|
||||
end
|
||||
print(" matches!")
|
||||
result = desc.run(msg, matches)
|
||||
print(" should return", result)
|
||||
if (result) then
|
||||
send_msg(receiver, result, ok_cb, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -189,7 +191,7 @@
|
||||
-- end
|
||||
|
||||
function string.starts(String,Start)
|
||||
return string.sub(String,1,string.len(Start))==Start
|
||||
return string.sub(String,1,string.len(Start))==Start
|
||||
end
|
||||
|
||||
function load_config()
|
||||
|
@ -21,9 +21,9 @@ function run(msg, matches)
|
||||
end
|
||||
|
||||
return {
|
||||
description = "send random url image from 9gag",
|
||||
usage = "9gag",
|
||||
regexp = "^9gag$",
|
||||
description = "send random image from 9gag",
|
||||
usage = "!9gag",
|
||||
patterns = {"^!9gag$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -5,8 +5,8 @@ end
|
||||
|
||||
return {
|
||||
description = "echoes the msg",
|
||||
usage = "echo [whatever]",
|
||||
regexp = "^echo (.*)$",
|
||||
usage = "!echo [whatever]",
|
||||
patterns = {"^!echo (.*)$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -20,8 +20,8 @@ end
|
||||
|
||||
return {
|
||||
description = "EURUSD market value",
|
||||
usage = "eur [USD]",
|
||||
regexp = "^eur (%d+[%d%.]*)$",
|
||||
usage = "!eur [USD]",
|
||||
patterns = {"^!eur (%d+[%d%.]*)$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -14,8 +14,8 @@ end
|
||||
|
||||
return {
|
||||
description = "Fortunes from Universidad Carlos III",
|
||||
usage = "uc3m",
|
||||
regexp = "^uc3m$",
|
||||
usage = "!uc3m",
|
||||
patterns = {"^!uc3m$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ end
|
||||
return {
|
||||
description = "Says hello to someone",
|
||||
usage = "say hello to [name]",
|
||||
regexp = "^say hello to (.*)$",
|
||||
patterns = {"^say hello to (.*)$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -9,8 +9,8 @@ end
|
||||
|
||||
return {
|
||||
description = "Lists all available commands",
|
||||
usage = "help",
|
||||
regexp = "^help$",
|
||||
usage = "!help",
|
||||
patterns = {"^!help$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -26,8 +26,8 @@ end
|
||||
|
||||
return {
|
||||
description = "search image with Google API and sends it",
|
||||
usage = "img [topic]",
|
||||
regexp = "^img (.*)$",
|
||||
usage = "!img [topic]",
|
||||
patterns = {"^!img (.*)$"},
|
||||
run = run
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ end
|
||||
|
||||
return {
|
||||
description = "bot sends pong",
|
||||
usage = "ping",
|
||||
regexp = "^ping$",
|
||||
usage = "!ping",
|
||||
patterns = {"^!ping$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -39,8 +39,8 @@ end
|
||||
|
||||
return {
|
||||
description = "Spanish dictionary",
|
||||
usage = "rae [word]",
|
||||
regexp = "^rae (.*)$",
|
||||
usage = "!rae [word]",
|
||||
patterns = {"^!rae (.*)$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -9,8 +9,8 @@ end
|
||||
|
||||
return {
|
||||
description = "Shows the bot version",
|
||||
usage = "version",
|
||||
regexp = "^version$",
|
||||
usage = "!version",
|
||||
patterns = {"^!version$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
@ -32,8 +32,8 @@ end
|
||||
|
||||
return {
|
||||
description = "weather in that city (Madrid is default)",
|
||||
usage = "weather [city]",
|
||||
regexp = "^weather(.*)$",
|
||||
usage = "!weather (city)",
|
||||
patterns = {"^!weather(.*)$"},
|
||||
run = run
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user