Schedule messages to be posted in a group after a delay.
Admin-only. Stores scheduled messages in Redis with a cron job to send them.
]]
localplugin={}
plugin.name='schedule'
plugin.category='utility'
plugin.description='Schedule messages to be posted in a group at a later time'
plugin.commands={'schedule','sched'}
plugin.help='/schedule <duration> <message> - Schedule a message.\n/schedule list - List pending messages.\n/schedule cancel <id> - Cancel a scheduled message.'
plugin.group_only=true
plugin.admin_only=true
localtools=require('telegram-bot-lua.tools')
localMAX_PER_CHAT=10
localMAX_DURATION=7*24*3600-- 7 days
localMAX_MESSAGE_LENGTH=4000
localCRON_SEND_LIMIT=10
-- Parse a duration string like "2h30m", "1d", "45m", "90s"
localfunctionparse_duration(str)
ifnotstrorstr==''then
returnnil
end
localtotal=0
localfound=false
locald=str:match('(%d+)%s*d')
ifdthen
total=total+tonumber(d)*86400
found=true
end
localh=str:match('(%d+)%s*h')
ifhthen
total=total+tonumber(h)*3600
found=true
end
localm=str:match('(%d+)%s*m')
ifmthen
total=total+tonumber(m)*60
found=true
end
locals=str:match('(%d+)%s*s')
ifsthen
total=total+tonumber(s)
found=true
end
ifnotfoundortotal<=0then
returnnil
end
returntotal
end
-- Format seconds into a human-readable string
localfunctionformat_duration(seconds)
ifseconds<60then
returnseconds..' second'..(seconds==1and''or's')
end
localparts={}
localdays=math.floor(seconds/86400)
seconds=seconds%86400
localhours=math.floor(seconds/3600)
seconds=seconds%3600
localmins=math.floor(seconds/60)
localsecs=seconds%60
ifdays>0then
table.insert(parts,days..'d')
end
ifhours>0then
table.insert(parts,hours..'h')
end
ifmins>0then
table.insert(parts,mins..'m')
end
ifsecs>0anddays==0then
table.insert(parts,secs..'s')
end
returntable.concat(parts,' ')
end
-- Get the index key for a chat's scheduled messages
localfunctionindex_key(chat_id)
return'schedule:index:'..tostring(chat_id)
end
-- Get the hash key for a specific scheduled message