2016-04-26 07:40:31 +02:00
|
|
|
local remind = {}
|
2016-04-18 05:28:55 +02:00
|
|
|
|
2016-06-19 22:39:47 +02:00
|
|
|
remind.command = 'remind <Länge> <Nachricht>'
|
2016-04-18 05:28:55 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function remind:init(config)
|
2016-10-04 20:16:20 +02:00
|
|
|
self.database.remind = self.database.remind or {}
|
2016-04-26 07:40:31 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
remind.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('remind', true).table
|
2016-06-19 22:39:47 +02:00
|
|
|
remind.doc = [[*
|
2016-08-14 16:30:06 +02:00
|
|
|
]]..config.cmd_pat..[[remind* _<Länge>_ _<Nachricht>_
|
|
|
|
Erinnert dich in der angegeben Länge in Minuten an eine Nachricht.
|
|
|
|
Die maximale Länge einer Erinnerung beträgt %s Buchstaben, die maximale Zeit beträgt %s Minuten, die maximale Anzahl an Erinnerung für eine Gruppe ist %s und für private Chats %s.]]
|
2016-10-04 20:16:20 +02:00
|
|
|
remind.doc = remind.doc:format(
|
2016-10-04 16:07:15 +02:00
|
|
|
config.remind.max_length,
|
|
|
|
config.remind.max_duration,
|
|
|
|
config.remind.max_reminders_group,
|
|
|
|
config.remind.max_reminders_private
|
|
|
|
)
|
2016-04-26 07:40:31 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
function remind:action(msg, config)
|
2016-08-02 01:01:06 +02:00
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
if not input then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, remind.doc, true)
|
2016-08-02 01:01:06 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-08-14 16:30:06 +02:00
|
|
|
local duration = tonumber(utilities.get_word(input, 1))
|
|
|
|
if not duration then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, remind.doc, true)
|
2016-08-02 01:01:06 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if duration < 1 then
|
|
|
|
duration = 1
|
2016-08-14 16:30:06 +02:00
|
|
|
elseif duration > config.remind.max_duration then
|
|
|
|
duration = config.remind.max_duration
|
2016-08-02 01:01:06 +02:00
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-30 23:45:44 +02:00
|
|
|
local message
|
|
|
|
if msg.reply_to_message and #msg.reply_to_message.text > 0 then
|
|
|
|
message = msg.reply_to_message.text
|
|
|
|
elseif utilities.input(input) then
|
|
|
|
message = utilities.input(input)
|
|
|
|
else
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, remind.doc, true)
|
2016-08-02 01:01:06 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-14 16:30:06 +02:00
|
|
|
if #message > config.remind.max_length then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Die maximale Länge einer Erinnerung ist ' .. config.remind.max_length .. '.')
|
2016-08-02 01:01:06 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-14 16:30:06 +02:00
|
|
|
|
|
|
|
local chat_id_str = tostring(msg.chat.id)
|
|
|
|
local output
|
2016-10-04 20:16:20 +02:00
|
|
|
self.database.remind[chat_id_str] = self.database.remind[chat_id_str] or {}
|
|
|
|
if msg.chat.type == 'private' and utilities.table_size(self.database.remind[chat_id_str]) >= config.remind.max_reminders_private then
|
2016-08-14 16:30:06 +02:00
|
|
|
output = 'Sorry, du kannst keine Erinnerungen mehr hinzufügen.'
|
2016-10-04 20:16:20 +02:00
|
|
|
elseif msg.chat.type ~= 'private' and utilities.table_size(self.database.remind[chat_id_str]) >= config.remind.max_reminders_group then
|
2016-08-14 16:30:06 +02:00
|
|
|
output = 'Sorry, diese Gruppe kann keine Erinnerungen mehr hinzufügen.'
|
|
|
|
else
|
|
|
|
-- Put together the reminder with the expiration, message, and message to reply to.
|
|
|
|
local timestamp = os.time() + duration * 60
|
|
|
|
local reminder = {
|
|
|
|
time = timestamp,
|
|
|
|
message = message
|
|
|
|
}
|
2016-10-04 20:16:20 +02:00
|
|
|
table.insert(self.database.remind[chat_id_str], reminder)
|
2016-08-14 16:30:06 +02:00
|
|
|
local human_readable_time = convert_timestamp(timestamp, '%H:%M:%S')
|
|
|
|
output = 'Ich werde dich um *'..human_readable_time..' Uhr* erinnern.'
|
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, true)
|
2016-04-18 05:28:55 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
function remind:cron(config)
|
2016-04-18 05:28:55 +02:00
|
|
|
local time = os.time()
|
|
|
|
-- Iterate over the group entries in the reminders database.
|
2016-10-04 20:16:20 +02:00
|
|
|
for chat_id, group in pairs(self.database.remind) do
|
2016-04-18 05:28:55 +02:00
|
|
|
-- Iterate over each reminder.
|
2016-08-14 16:30:06 +02:00
|
|
|
for k, reminder in pairs(group) do
|
2016-04-18 05:28:55 +02:00
|
|
|
-- If the reminder is past-due, send it and nullify it.
|
|
|
|
-- Otherwise, add it to the replacement table.
|
|
|
|
if time > reminder.time then
|
2016-10-04 20:16:20 +02:00
|
|
|
local output = '<b>ERINNERUNG:</b>\n"'..utilities.html_escape(reminder.message)..'"'
|
2016-10-04 20:29:13 +02:00
|
|
|
local res = utilities.send_message(chat_id, output, true, nil, 'HTML')
|
2016-08-14 16:30:06 +02:00
|
|
|
-- If the message fails to send, save it for later (if enabled in config).
|
|
|
|
if res or not config.remind.persist then
|
|
|
|
group[k] = nil
|
2016-04-18 05:28:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-05 13:14:22 +02:00
|
|
|
return remind
|