lex(msg) function

This commit is contained in:
yago 2015-03-14 17:23:38 +01:00
parent c58e507032
commit c189cfc93f

View File

@ -17,12 +17,15 @@ end
_values = read_file_values()
function fetch_value(chat, value_name)
if (_values[chat] == nil) then
-- Chat non exists
if _values[chat] == nil then
return nil
end
if (value_name == nil ) then
if value_name == nil then
return nil
end
local value = _values[chat][value_name]
return value
end
@ -57,18 +60,24 @@ function run(msg, matches)
return get_value(chat_id, matches[1])
end
function lex(msg, text)
function lex(msg)
local text = msg.text
local chat_id = tostring(msg.to.id)
local s, e = text:find("%$%a+")
if (s == nil) then
return nil
end
if s then
local var = text:sub(s + 1, e)
local value = fetch_value(chat_id, var)
if (value == nil) then
value = "(unknown value " .. var .. ")"
end
return text:sub(0, s - 1) .. value .. text:sub(e + 1)
msg.text = text:sub(0, s - 1) .. value .. text:sub(e + 1)
end
return msg
end
return {