1-- 2-- FusionPBX 3-- Version: MPL 1.1 4-- 5-- The contents of this file are subject to the Mozilla Public License Version 6-- 1.1 (the "License"); you may not use this file except in compliance with 7-- the License. You may obtain a copy of the License at 8-- http://www.mozilla.org/MPL/ 9-- 10-- Software distributed under the License is distributed on an "AS IS" basis, 11-- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12-- for the specific language governing rights and limitations under the 13-- License. 14-- 15-- The Original Code is FusionPBX 16-- 17-- The Initial Developer of the Original Code is 18-- Mark J Crane <markjcrane@fusionpbx.com> 19-- Copyright (C) 2010 20-- the Initial Developer. All Rights Reserved. 21-- 22-- Contributor(s): 23-- Mark J Crane <markjcrane@fusionpbx.com> 24 25--get the argv values 26script_name = argv[0]; 27domain_name = argv[1]; 28wakeup_destination = argv[2]; 29wakeup_call_sound = argv[3]; 30 31--define the trim function 32require "resources.functions.trim"; 33 34--add is_numeric 35function is_numeric(text) 36 if type(text)~="string" and type(text)~="number" then return false end 37 return tonumber(text) and true or false 38end 39 40--set the default values for the variables 41pin_number = ""; 42max_tries = 3; 43digit_timeout = 3000; 44 45--check whether originate the wakeup call or to request details for it 46if (wakeup_destination) then 47 --begin the wakeup call 48 if ( session:ready() ) then 49 --prepare the api object 50 api = freeswitch.API(); 51 52 --set session settings 53 session:answer(); 54 session:setAutoHangup(false); 55 56 --sleep 57 session:sleep('500'); 58 59 --wakeup confirm press 1 to 3 60 min_digits = 1; 61 max_digits = 1; 62 max_tries = 3; 63 digit_timeout = 20000; 64 digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", wakeup_call_sound, "", "\\d+"); 65 66 --get the dialplan variables and set them as local variables 67 domain_name = session:getVariable("domain_name"); 68 sip_auto_answer = session:getVariable("sip_auto_answer"); 69 snooze_time = session:getVariable("snooze_time"); 70 wakeup_caller_id_name = session:getVariable("wakeup_caller_id_name"); 71 wakeup_caller_id_number = session:getVariable("wakeup_caller_id_number"); 72 73 --handle auto answer 74 if (sip_auto_answer == "true") then 75 auto_answer = "sip_auto_answer=true,sip_h_Alert-Info='Ring Answer'"; 76 else 77 auto_answer = "sip_auto_answer=false"; 78 end 79 80 --reschedule the call for snooze 81 if (digits == "2") then 82 freeswitch.consoleLog("NOTICE", "wakeup call: snooze selected - rescheduled the call\n"); 83 api = freeswitch.API(); 84 cmd_string = "sched_api +"..snooze_time.." wakeup-call-"..wakeup_destination.." originate {hangup_after_bridge=false,"..auto_answer..",snooze_time="..snooze_time..",origination_caller_id_name='"..wakeup_caller_id_name.."',origination_caller_id_number="..wakeup_caller_id_number..",wakeup_caller_id_name='".. wakeup_caller_id_name.."',wakeup_caller_id_number=".. wakeup_caller_id_number.."}user/"..wakeup_destination.."@"..domain_name.." &lua('wakeup.lua "..domain_name.." "..wakeup_destination.." "..wakeup_call_sound.."') "; 85 freeswitch.consoleLog("NOTICE", "wakeup: "..cmd_string.."\n"); 86 reply = api:executeString(cmd_string); 87 end 88 end 89else 90 --prompt for the wakeup call information 91 if (session:ready()) then 92 --session commands 93 session:answer(); 94 session:setAutoHangup(false); 95 96 --get the dialplan variables and set them as local variables 97 domain_name = session:getVariable("domain_name"); 98 time_zone_offset = session:getVariable("time_zone_offset"); 99 sip_number_alias = session:getVariable("sip_number_alias"); 100 destination_number = session:getVariable("destination_number"); 101 wakeup_destination = session:getVariable("wakeup_destination"); 102 sip_from_user = session:getVariable("sip_from_user"); 103 sip_auto_answer = session:getVariable("sip_auto_answer"); 104 snooze_time = session:getVariable("snooze_time"); 105 wakeup_caller_id_name = session:getVariable("wakeup_caller_id_name"); 106 wakeup_caller_id_number = session:getVariable("wakeup_caller_id_name"); 107 wakeup_destination_sound = session:getVariable("wakeup_destination_sound"); 108 wakeup_greeting_sound = session:getVariable("wakeup_greeting_sound"); 109 wakeup_call_sound = session:getVariable("wakeup_call_sound"); 110 wakeup_scheduled_sound = session:getVariable("wakeup_scheduled_sound"); 111 wakeup_accept_sound = session:getVariable("wakeup_accept_sound"); 112 wakeup_cancelled_sound = session:getVariable("wakeup_cancelled_sound"); 113 114 --set the defaults 115 if (not time_zone_offset) then time_zone_offset = ''; end 116 if (not sip_number_alias) then sip_number_alias = ''; end 117 if (not sip_auto_answer) then sip_auto_answer = ''; end 118 if (not snooze_time) then snooze_time = '600'; end 119 if (not wakeup_caller_id_name) then wakeup_caller_id_name = 'wakeup call'; end 120 if (not wakeup_caller_id_number) then wakeup_caller_id_number = destination_number; end 121 if (not wakeup_destination_sound) then wakeup_destination_sound = 'phrase:wakeup-destination'; end 122 if (not wakeup_greeting_sound) then wakeup_greeting_sound = 'phrase:wakeup-greeting'; end 123 if (not wakeup_call_sound) then wakeup_call_sound = 'phrase:wakeup-call'; end 124 if (not wakeup_scheduled_sound) then wakeup_scheduled_sound = 'phrase:wakeup-scheduled'; end 125 if (not wakeup_accept_sound) then wakeup_accept_sound = 'phrase:wakeup-accept'; end 126 if (not wakeup_cancelled_sound) then wakeup_cancelled_sound = 'phrase:wakeup-cancelled'; end 127 128 --sleep 129 session:sleep('500'); 130 end 131 if (session:ready()) then 132 --set the wakeup destination 133 if (wakeup_destination == "prompt") then 134 min_digits = 1; 135 max_digits = 11; 136 --if (wakeup_destination_sound == nil and string.len(wakeup_destination_sound) == 0) then 137 -- wakeup_destination_sound = "phrase:wakeup-destination"; 138 --end 139 wakeup_destination = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", wakeup_destination_sound, "", "\\d+"); 140 else 141 if (is_numeric(wakeup_destination)) then 142 --use the provided wakeup id 143 else 144 if (is_numeric(sip_number_alias)) then 145 wakeup_destination = sip_number_alias; 146 else 147 wakeup_destination = sip_from_user; 148 end 149 end 150 end 151 152 --sleep 153 session:sleep('500'); 154 end 155 if (session:ready()) then 156 --play the wakeup greeting 157 min_digits = 0; 158 max_digits = 1; 159 max_tries = 1; 160 digit_timeout = '1000'; 161 session:streamFile(wakeup_greeting_sound); 162 --wakeup_greeting = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", wakeup_greeting_sound, "", "\\d+"); 163 end 164 if (session:ready()) then 165 --get the wakeup hours 166 min_digits = 2; 167 max_digits = 2; 168 max_tries = 3; 169 digit_timeout = '3000'; 170 if (wakeup_hours_sound == nil) then 171 wakeup_hours_sound = "phrase:wakeup-hours"; 172 end 173 wakeup_hours = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", wakeup_hours_sound, "", "\\d+"); 174 end 175 if (session:ready()) then 176 --get the wakeup minutes 177 min_digits = 2; 178 max_digits = 2; 179 max_tries = 3; 180 if (wakeup_minutes_sound == nil) then 181 wakeup_minutes_sound = "phrase:wakeup-minutes"; 182 end 183 wakeup_minutes = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", wakeup_minutes_sound, "", "\\d+"); 184 end 185 if (session:ready()) then 186 --send the time to the log 187 freeswitch.consoleLog("NOTICE", "wakeup time: "..wakeup_hours.."\n"); 188 189 --get the current time 190 current_hours = tonumber(os.date("%H")); 191 current_minutes = tonumber(os.date("%M")); 192 current_seconds = tonumber(os.date("%S")); 193 194 --adjust the time zone offset 195 if (time_zone_offset) then 196 current_hours = time_zone_offset + current_hours; 197 if (current_hours < 0) then 198 current_hours = current_hours + 24; 199 end 200 if (current_hours > 23) then 201 current_hours = current_hours - 24; 202 end 203 end 204 205 --show the current hours minutes and seconds to the log 206 --freeswitch.consoleLog("NOTICE", "Hours: " .. current_hours .. "\n"); 207 --freeswitch.consoleLog("NOTICE", "Mins: " .. current_minutes .. "\n"); 208 --freeswitch.consoleLog("NOTICE", "Seconds: " .. current_seconds .. "\n"); 209 210 --prepare the current time 211 current_time = (current_hours * 100) + current_minutes; 212 213 --get the wakeup hours and minutes 214 --wakeup_hours = string.sub(wakeup_time, 1, 2); 215 --wakeup_minutes = string.sub(wakeup_time, 3); 216 217 --show the wakeup time, hours, and minutes to the log 218 --freeswitch.consoleLog("NOTICE", "wakeup_time "..wakeup_time.."\n"); 219 --freeswitch.consoleLog("NOTICE", "wakeup_hours "..wakeup_hours.."\n"); 220 --freeswitch.consoleLog("NOTICE", "wakeup_minutes "..wakeup_minutes.."\n"); 221 222 --convert the time, hours and minutes to numbers 223 wakeup_time = tonumber(tostring(wakeup_hours)..tostring(wakeup_minutes)); 224 --wakeup_hours = tonumber(wakeup_hours); 225 --wakeup_minutes = tonumber(wakeup_minutes); 226 227 --set the time 228 if (current_time > wakeup_time) then 229 --get the current_time_in_seconds 230 current_time_in_seconds = (current_hours * 3600) + (current_minutes * 60); 231 --freeswitch.consoleLog("NOTICE", "sched_api_time = ("..current_hours.." * 3600) + ("..current_minutes.." * 60)\n"); 232 --get the seconds until midnight 233 seconds_until_midnight = (24 * 3600) - current_time_in_seconds; 234 --freeswitch.consoleLog("NOTICE", "sched_api_time = (24 * 3600) - "..current_time_in_seconds.."\n"); 235 --get the wakeup_time_in_seconds 236 wakeup_time_in_seconds = (wakeup_hours * 3600) + (wakeup_minutes * 60); 237 --freeswitch.consoleLog("NOTICE", "sched_api_time = ("..wakeup_hours.." * 3600) + ("..wakeup_minutes.." * 60)\n"); 238 --add the seconds_until_midnight to the wakeup_time_in_seconds 239 sched_api_time = wakeup_time_in_seconds + seconds_until_midnight; 240 --freeswitch.consoleLog("NOTICE", "sched_api_time = "..wakeup_time_in_seconds.." + "..seconds_until_midnight.."\n"); 241 else 242 --get the current_time_in_seconds 243 current_time_in_seconds = (current_hours * 3600) + (current_minutes * 60); 244 --freeswitch.consoleLog("NOTICE", "current_time_in_seconds = ("..current_hours.." * 3600) + ("..current_minutes.." * 60);\n"); 245 --get the wakeup_time_in_seconds 246 wakeup_time_in_seconds = (wakeup_hours * 3600) + (wakeup_minutes * 60); 247 --freeswitch.consoleLog("NOTICE", "wakeup_time_in_seconds = ("..wakeup_hours.." * 3600) + ("..wakeup_minutes.." * 60);\n"); 248 --subtract the current time from wakeup_time_in_seconds 249 sched_api_time = wakeup_time_in_seconds - current_time_in_seconds; 250 --freeswitch.consoleLog("NOTICE", "sched_api_time = "..wakeup_time_in_seconds.." - "..current_time_in_seconds.."\n"); 251 end 252 --freeswitch.consoleLog("NOTICE", "sched_api_time "..sched_api_time.."\n"); 253 end 254 if (session:ready()) then 255 --wakeup call has been scheduled 256 session:streamFile(wakeup_scheduled_sound); 257 session:say(wakeup_time, "en", "number", "ITERATED"); 258 259 --handle auto answer 260 if (sip_auto_answer == "true") then 261 auto_answer = "sip_auto_answer=true,sip_h_Alert-Info='Ring Answer'"; 262 else 263 auto_answer = "sip_auto_answer=false"; 264 end 265 266 --schedule the wakeup call 267 cmd_string = "sched_api +"..sched_api_time.." wakeup-call-"..wakeup_destination.." originate {hangup_after_bridge=false,"..auto_answer..",snooze_time="..snooze_time..",origination_caller_id_name='".. wakeup_caller_id_name.."',origination_caller_id_number=".. wakeup_caller_id_number..",wakeup_caller_id_name='".. wakeup_caller_id_name.."',wakeup_caller_id_number=".. wakeup_caller_id_number.."}user/"..wakeup_destination.."@"..domain_name.." &lua('wakeup.lua "..domain_name.." "..wakeup_destination.." "..wakeup_call_sound.."') "; 268 freeswitch.consoleLog("NOTICE", "wakeup: "..cmd_string.."\n"); 269 api = freeswitch.API(); 270 reply = api:executeString(cmd_string); 271 272 --wakeup confirm press 1 to 3 273 min_digits = 1; 274 max_digits = 1; 275 wakeup_accept = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", wakeup_accept_sound, "", "\\d+"); 276 277 --accept 278 if (wakeup_accept == "1") then 279 --send a message to the console 280 freeswitch.consoleLog("NOTICE", "wakeup: accepted\n"); 281 --hangup 282 session:hangup(); 283 end 284 285 --cancel 286 if (wakeup_accept == "2") then 287 --send a message to the console 288 freeswitch.consoleLog("NOTICE", "wakeup: cancelled\n"); 289 290 --unschedule the call 291 cmd_string = "sched_del wakeup-call-"..wakeup_destination; 292 freeswitch.consoleLog("NOTICE", "wakeup: "..cmd_string.."\n"); 293 api = freeswitch.API(); 294 reply = api:executeString(cmd_string); 295 296 --play the cancel message 297 session:streamFile(wakeup_cancelled_sound); 298 299 --hangup 300 session:hangup(); 301 end 302 end 303end 304