forked from yagop/telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d27c6d5
commit 891047b
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
local filename='data/remind.lua' | ||
local cronned = load_from_file(filename) | ||
|
||
local function save_cron(msg, text,date) | ||
local origin = get_receiver(msg) | ||
if not cronned[date] then | ||
cronned[date] = {} | ||
end | ||
local arr = { origin, text } ; | ||
table.insert(cronned[date], arr) | ||
serialize_to_file(cronned, filename) | ||
return 'Saved!' | ||
end | ||
|
||
local function delete_cron(date) | ||
for k,v in pairs(cronned) do | ||
if k == date then | ||
cronned[k]=nil | ||
end | ||
end | ||
serialize_to_file(cronned, filename) | ||
end | ||
|
||
local function cron() | ||
for date, values in pairs(cronned) do | ||
if date < os.time() then --time's up | ||
send_msg(values[1][1], "Time's up:"..values[1][2], ok_cb, false) | ||
delete_cron(date) --TODO: Maybe check for something else? Like user | ||
end | ||
|
||
end | ||
end | ||
|
||
local function actually_run(msg, delay,text) | ||
if (not delay or not text) then | ||
return "Usage: !remind [delay: 2h3m1s] text" | ||
end | ||
save_cron(msg, text,delay) | ||
return "I'll remind you on " .. os.date("%x at %H:%M:%S",delay) .. " about '" .. text .. "'" | ||
end | ||
|
||
local function run(msg, matches) | ||
local sum = 0 | ||
for i = 1, #matches-1 do | ||
local b,_ = string.gsub(matches[i],"[a-zA-Z]","") | ||
if string.find(matches[i], "s") then | ||
sum=sum+b | ||
end | ||
if string.find(matches[i], "m") then | ||
sum=sum+b*60 | ||
end | ||
if string.find(matches[i], "h") then | ||
sum=sum+b*3600 | ||
end | ||
end | ||
|
||
local date=sum+os.time() | ||
local text = matches[#matches] | ||
|
||
local text = actually_run(msg, date, text) | ||
return text | ||
end | ||
|
||
return { | ||
description = "remind plugin", | ||
usage = { | ||
"!remind [delay: 2hms] text", | ||
"!remind [delay: 2h3m] text", | ||
"!remind [delay: 2h3m1s] text" | ||
}, | ||
patterns = { | ||
"^!remind ([0-9]+[hmsdHMSD]) (.+)$", | ||
"^!remind ([0-9]+[hmsdHMSD])([0-9]+[hmsdHMSD]) (.+)$", | ||
"^!remind ([0-9]+[hmsdHMSD])([0-9]+[hmsdHMSD])([0-9]+[hmsdHMSD]) (.+)$" | ||
}, | ||
run = run, | ||
cron = cron | ||
} |