提交 3a79e26e authored 作者: Michael S Collins's avatar Michael S Collins

Mikey's cheesey little wakeup call script

上级 3c25a380
<include>
<!-- Wake-up call extensions -->
<extension name="Set wake-up call">
<condition field="destination_number" expression="^(\*55)$">
<action application="log" data="INFO Wake-up call set"/>
<action application="lua" data="wakeup-call.lua"/>
</condition>
</extension>
<extension name="Cancel wake-up call">
<condition field="destination_number" expression="^(#55)$">
<action application="answer"/>
<action application="log" data="INFO Wake-up call cancel [ ${caller_id_number} ]"/>
<action application="set" data="wuc_time=${db(select/wuc/${caller_id_number})}"/>
<action application="transfer" data="wuc_${caller_id_number} XML default"/>
<action application="hangup"/>
</condition>
</extension>
<extension name="Deliver wake-up call">
<condition field="destination_number" expression="^(wake_up_sleepy_head)$">
<action application="answer"/>
<action application="playback" data="phrase:This_is_your_wakeup_call"/>
<action application="sleep" data="4000"/>
<action application="playback" data="phrase:This_is_your_wakeup_call"/>
<action application="hangup"/>
</condition>
</extension>
<extension name="Cancel if he have one" continue="true">
<condition field="destination_number" expression="^wuc_(\d+)$">
<action application="log" data="INFO wuc_user is ${wuc_time}"/>
</condition>
<condition field="${wuc_time}" expression="(\d+)">
<action application="set" data="data=${sched_del(wuc${caller_id_number})}"/>
<action application="set" data="data=${db(delete/wuc/${caller_id_number})}"/>
<action application="playback" data="phrase:Wakeup_call_has_been_cancelled"/>
<anti-action application="playback" data="phrase:No_wakeup_call"/>
</condition>
</extension>
<extension name="vm_forwarding">
<condition field="destination_number" expression="^${sip_auth_username}$">
<action application="info"/>
<action application="answer"/>
<action application="sleep" data="1000"/>
<action application="bridge" data="loopback/app=voicemail:default ${domain_name} ${"/>
</condition>
</extension>
</include>
LuaDate v2
Lua Date and Time module for Lua 5.x.
Features:
* Date and Time string parsing.
* Time addition and subtraction.
* Time span calculation.
* Support ISO 8601 Dates.
* Local time support.
* Lua module (not binary).
* Formats Date and Time like strftime.
\ No newline at end of file
差异被折叠。
差异被折叠。
--[[---------------------
This script makes an html calendar
Syntax: mkcalendar.lua year1 year2 year3 .. yearn > file
arg:
year1 .. yearn - the year(s) of the calendar to generate
file - the name of the file to write the generated text calendar
--]]---------------------
require"date"
function makemonth(y,m)
local t = {}
local d = date(y,m,1)
t.name = d:fmt("%B")
t.year = y
-- get back to the nearest sunday
d:adddays(-(d:getweekday()-1))
repeat
local tt = {}
table.insert(t,tt)
repeat -- insert the week days
table.insert(tt, d:getday())
until d:adddays(1):getweekday() == 1
until d:getmonth() ~= m
return t
end
local htm_foot = '\n</html>'
local htm_head = [[
<style>
th {background:black; color: silver; vertical-align: middle;}
td {vertical-align: middle; text-align:center;}
td.sun {color: red;}
td.sat {color: blue;}
</style>
<html>
]]
local htm_yearhead = '\n<table align="left">'
local htm_monhead = '\n<tr><th colspan = "7">%s, %s</th></tr><tr><td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thu</td><td>fri</td><td>sat</td></tr>'
local htm_monweek = '\n<tr><td class="sun">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td class="sat">%s</td></tr>'
local htm_yearfoot = '\n</table>'
function makecalendar(y, iox)
iox:write(htm_yearhead)
for i = 1, 12 do
local tm = makemonth(y, i)
iox:write(string.format(htm_monhead, tm.name, tm.year))
for k, v in ipairs(tm) do
iox:write(string.format(htm_monweek, v[1], v[2], v[3], v[4], v[5], v[6], v[7]))
end
end
iox:write(htm_yearfoot)
end
io.stdout:write(htm_head)
for k, v in ipairs(arg) do
local y = tonumber(v)
if y then makecalendar(y, io.stdout) end
end
io.stdout:write(htm_foot)
--[[---------------------
This script makes an iso year-week-day calendar
Syntax: mkisocal.lua year1 year2 year3 .. yearn > file
arg:
year1 .. yearn - the year(s) of the calendar to generate
file - the name of the file to write the generated text calendar
--]]---------------------
require"date"
local htm_foot = [[</body></html>]]
local htm_head = [[<html><head><style>body{color:#000000;background-color:#FFFFFF;font-family:sans-serif;}th{background:#000000;color:#CCCCCC;vertical-align:middle;}td{vertical-align:top;text-align:center;font-weight:bold;}.s{color:#999999;font-size:60%;}</style></head><body>]]
local htm_yearhead = [[<table align="center" width="100%" border="1"><tr><th>Year</th><th>Week</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th></tr>]]
local htm_yearfoot = [[</table>]]
function makecalendar(year, iow)
local d = date():setisoyear(year,1,1)
iow(htm_yearhead)
iow("<!--".. d .. "-->\n")
while d:getisoyear() == year do
iow(d:fmt("<tr><td>%G</td><td>%V<br/><small class='s'>%Y-%j</small></td>"))
repeat iow(d:fmt("<td>%u<br/><small class='s'>%b %d %Y</small></td>"))
until d:adddays(1):getisoweekday() == 1
iow("</tr>\n")
end
iow(htm_yearfoot)
end
local out = io.write
out(htm_head)
for k, v in ipairs(arg) do
local d = tonumber(v);
if d then makecalendar(d, out) end
end
out(htm_foot)
--
-- wakeup-call.lua
--
-- Simple wakeup call script
-- Reads caller id, checks for existing wakeup call
-- If wuc exists then confirm/delete, else do new wuc
--
-- Get the date library
require "date"
api = freeswitch.API()
session:answer()
session:setAutoHangup(false)
-- Sound files and phrases
get_time = "phrase:For_one_time_wakeup_call"
invalid = "ivr/ivr-that_was_an_invalid_entry.wav"
already = "phrase:Already_have_wakeup_call"
cancelled = "phrase:Wakeup_call_has_been_cancelled"
confirmed = "phrase:Wakeup_call_has_been_confirmed"
-- Dates/times/fun stuff
now_epoch = api:execute("strepoch")
now_iso = api:execute("strftime")
today = api:execute("strftime","%Y-%m-%d")
today_obj = date(today)
tom_obj = today_obj:adddays(1)
tomorrow = tom_obj:fmt("%Y-%m-%d")
freeswitch.consoleLog("INFO","Today is: " .. today .. "\n")
freeswitch.consoleLog("INFO","Tomorrow is: " .. tomorrow .. "\n")
--freeswitch.consoleLog("INFO","OS Time is " .. osdate .. "\n")
-- Check for existing wuc
caller = session:getVariable("caller_id_number")
my_wuc = api:execute("db","select/wuc/" .. caller)
while (session:ready()) do
if ( my_wuc == '' ) then
wakeup_dialstring = "originate {ignore_early_media=true,origination_caller_id_number=*55}user/" .. caller .. " wake_up_sleepy_head"
freeswitch.consoleLog("INFO","Setting wakeup dialstring to:\n" .. wakeup_dialstring .. "\n\n")
-- No wuc, ask caller to create one
freeswitch.consoleLog("INFO", "No wakeup call for " .. caller .. ", let's create one\n")
digits = session:playAndGetDigits(4,4,3,9000,"#",get_time,invalid,"([012][0-9]|2[0123])[012345][0-9]")
if ( session:ready() ) then
-- Caller entered a valid time, let's proceed
freeswitch.consoleLog("WARNING", "Digits entered: " .. digits .. "\n\n")
-- digits has the military time HHMM
-- Compare to time now to decide if we need today or tomorrow
-- Calculate now in minutes since 00:00
now_hr = api:execute("strftime","%H")
now_min = api:execute("strftime","%M")
now_mod = (now_hr * 60) + now_min
freeswitch.consoleLog("INFO","Now hr/min/mod [" .. now_hr .. "/" .. now_min .. "/" .. now_mod .. "]\n")
-- Calculate wuc time in minutes since midnight
wuc_hr = string.sub(digits, 1, 2)
wuc_min = string.sub(digits, 3)
freeswitch.consoleLog("INFO","wuc_hr: " .. wuc_hr .." wuc_min: " .. wuc_min .. "\n")
wuc_mod = (wuc_hr * 60) + wuc_min
freeswitch.consoleLog("INFO","Wuc hr min mod [" .. wuc_hr .. "/" .. wuc_min .. "/" .. wuc_mod .. "]\n")
-- Compare now vs. wuc time and act accordingly
if ( wuc_mod < now_mod ) then
-- Tomorrow
-- Create date/time string
wuc_time = tomorrow .. " " .. wuc_hr .. ":" .. wuc_min .. ":00"
wuc_epoch = api:execute("strepoch",wuc_time)
freeswitch.consoleLog("INFO","Setting wuc for tomorrow: " .. wuc_time .. " [" .. wuc_epoch .. "]\n")
freeswitch.consoleLog("INFO", "Set sched_api for tomorrow...\n")
else
-- Later today
wuc_time = today .. " " .. wuc_hr .. ":" .. wuc_min .. ":00"
wuc_epoch = api:execute("strepoch",wuc_time)
-- TODO: ask user if they want the wuc tomorrow instead of today
freeswitch.consoleLog("INFO","Setting wuc for today: " .. wuc_time .. " [" .. wuc_epoch .. "]\n")
freeswitch.consoleLog("INFO", "Set sched_api for today...\n")
end
sched_cmd = wuc_epoch .. " wuc" .. caller .. " " .. wakeup_dialstring
freeswitch.consoleLog("ERR","sched_api args:\n" .. sched_cmd .. "\n")
res = api:execute("sched_api",sched_cmd)
freeswitch.consoleLog("INFO","sched_api result: " .. res)
res = api:execute("sched_api",wuc_epoch .. " wuc" .. caller .. " db delete/wuc/" .. caller)
freeswitch.consoleLog("INFO","sched_api result: " .. res)
res = api:execute("db","insert/wuc/" .. caller .. "/" .. wuc_epoch)
freeswitch.consoleLog("INFO","db insert result: " .. res)
-- Confirm the wakeup call
my_confirm = confirmed .. ":" .. wuc_epoch
session:execute("playback",my_confirm)
else
-- Caller hung up without entering anyting
freeswitch.consoleLog("WARNING","Caller hung up without entering a wakeup time\n")
session:hangup()
end
else
-- Caller already has wuc, ask to cancel
freeswitch.consoleLog("INFO", caller .. " has a wakeup call, ask to confirm or delete...\n")
wuc_epoch = api:execute("db","select/wuc/" .. caller)
freeswitch.consoleLog("INFO","wuc time for " .. caller .. " is " .. wuc_epoch .. "\n")
already = already .. ":" .. wuc_epoch
freeswitch.consoleLog("INFO","Calling playAndGetDigits with " .. already .. "\n")
digits = session:playAndGetDigits(1,1,3,9000,"#",already .. ":" .. wuc_epoch,invalid,"[0-9*#]")
if ( digits == '1' ) then
api:execute("sched_del","wuc" .. caller)
api:execute("db","delete/wuc/" .. caller)
session:execute("playback", cancelled)
else
session:execute("playback", confirmed)
end
end -- if (my_wuc == '')
session:hangup()
end -- while(session:ready())
session:hangup()
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论