Day of the Week and Time of the Day Forwarding inplan

Catalog of dial plans
Post Reply
mnipp
Posts: 192
Joined: Sat Oct 03, 2009 9:48 am
Location: NSW Australia

Day of the Week and Time of the Day Forwarding inplan

Post by mnipp » Sun Feb 14, 2010 2:03 am

http://www.mysipswitch.com/forum/viewtopic.php?t=1265
Thanks to this original post by TheFug and input from MikeTelis we have had a good timing function on incoming calls. I have now added a day of the week option into the following inplan.

If you need more flexible timing on what phone rings on what day and time my new inplan with a time setting dialler function can give you these options.

This should make it easy to setup complex forwarding rules across various phones on different days and times of the day and different time-zones if needed.

The day settings are:
Sunday(0) to Saturday(6) for everyday calling
Saturday(6) to Sunday(0) for weekend only calling
Monday(1) to Friday(5)
wednesday(3) to wednesday(3) for wednesday only calling

inplan

Code: Select all

# inplan Set time defaults in dialler below, 
# example (0730)7:30am (2330)11:30pm everyday Sunday(0) to Saturday(6) (GMT+11 hour) TimeZone.

$status = "Call Later" # default status message

def dialler(tocall, timer=60, starttime="0730", stoptime="2330", startday=0, endday=6, tz=11)
  t = Time.now.utc + (tz*60*60)
  logtime = t.strftime("%A(%w) (%H%M)%I:%M %p")
  sys.log("#{logtime}")            # Check Day and Time in Console output.
  openday = startday > endday ? !(((endday+1)..(startday-1)) === t.wday) : (startday..endday) === t.wday
  if openday
   hhmm = t.strftime("%H%M")
   openhours = stoptime < starttime ? !((stoptime...starttime) === hhmm) : (starttime...stoptime) === hhmm
     if openhours
         sys.log("calling #{tocall}")
       $status = sys.Dial(tocall, timer)
         sys.log("failed call to #{tocall}")
     end
  end
end
     # The Following sets the incoming CALLERID NAME if not set
         fromname = req.Header.From.FromName.to_s       # create a copy of FromName
         username = req.Header.from.FromURI.User.to_s   # create a copy of userName
           if fromname == "" or fromname == username    # if FromName empty or the same as username
            if username =~ /^[0-9]*[0-9]$/
             fromname = req.Header.From.FromURI.host.to_s   # if a number display caller's host, else full address.
            else
             fromname = username + " " + req.Header.From.FromURI.host.to_s
            end
           end
         sys.SetFromHeader(fromname, nil, nil)   # Set FromName for sys.Dial
         sys.Log("FromName: '#{fromname}'")

dialler("#{sys.Username}@local",20)              # 20sec ring between default times 7:30am to 11:30pm everyday
dialler("0487654321@ptel",20,"1700","1800",1,5)  #      mobile 5:00pm to  6:00pm Monday to Friday 
dialler("0487654321@ptel",20,"0800","2200",6,0)  # same mobile 8:00am to 10:00pm Saturday to Sunday 

code = 480
if $status.to_s == 'Failed' then
     $status = 'Busy or Offline'
     code = 486
end

sys.log("failed code:#{code} #{$status}")
sys.Respond(code, "#{$status}")
Have not fully tested code. Written on day of post, so far so good.
billion 7404VGP
dialplan (<9*:*>[0-9*][0-9*].T<:@sipbroker>|[0-9*].T)

User avatar
TheFug
Posts: 914
Joined: Sat Oct 06, 2007 8:23 am
Location: The Netherlands, North-Holland

Post by TheFug » Sun Feb 14, 2010 11:59 pm

That's nice work !
The Ruby code you now refer to, is still my use from MySipSwitch,
later i noticed slightly different behaviour when used on Sipsorcery,
But it could be, that this doesn't matter in this case.
Thanks, The Fug.

gear: my ISP's Zyxel Modem/Router in bridge, Sitecom WL309 Router, Siemens Gigaset 301D

mnipp
Posts: 192
Joined: Sat Oct 03, 2009 9:48 am
Location: NSW Australia

Post by mnipp » Sat Feb 20, 2010 6:50 am

Adding a BLACKLIST of numbers to this inplan is easy.
But adding a WHITELIST will mean modifying the dialler code with overrides on the time settings so I reposted this plan with the changes in place.

In the WHITELIST you have three(3) options
Set $timeOR = true if you only need to override the time setting
Set $dayOR = true if the day is important.
Set totalOR = true to cancel both time limits in whitelist like the following example.

Code: Select all

    # inplan Set time defaults in dialler below, 
# example (0730)7:30am (2330)11:30pm everyday Sunday(0) to Saturday(6) (GMT+11 hour) TimeZone.

$status = "Call Later" # default status message

totalOR = false
$dayOR  = false
$timeOR = false        # Set time OverRides to false

def dialler(tocall, timer=60, starttime="0730", stoptime="2330", startday=0, endday=6, tz=11)
  t = Time.now.utc + (tz*60*60)
  logtime = t.strftime("%A(%w) (%H%M)%I:%M %p")
  sys.log("#{logtime}")            # Check Day and Time in Console output.
  openday = startday > endday ? !(((endday+1)..(startday-1)) === t.wday) : (startday..endday) === t.wday
  if openday or $dayOR
   hhmm = t.strftime("%H%M")
   openhours = stoptime < starttime ? !((stoptime...starttime) === hhmm) : (starttime...stoptime) === hhmm
     if openhours or $timeOR
         sys.log("calling #{tocall}")
       $status = sys.Dial(tocall, timer)
         sys.log("failed call to #{tocall}")
     end
  end
end
     # The Following sets the incoming CALLERID NAME if not set
         fromname = req.Header.From.FromName.to_s       # create a copy of FromName
         username = req.Header.from.FromURI.User.to_s   # create a copy of userName
           if fromname == "" or fromname == username    # if FromName empty or the same as username
            if username =~ /^[0-9]*[0-9]$/
             fromname = req.Header.From.FromURI.host.to_s   # if a number display caller's host, else full address.
            else
             fromname = username + " " + req.Header.From.FromURI.host.to_s
            end
           end
         sys.SetFromHeader(fromname, nil, nil)   # Set FromName for sys.Dial
         sys.Log("FromName: '#{fromname}'")

sys.Log("#{req.Header.from.FromURI.User.to_s} is calling checking in BLACK and WHITELIST") # check this number in console

 # BLACKLIST of unwanted Callers 
   case req.Header.from.FromURI.User.to_s 
    when /0299999999/ then sys.Respond(403, "Forbidden") # block calls from 0299999999 example 
   end

 # WHITELIST of important Callers
   case req.Header.from.FromURI.User.to_s 
    when /0288888888/ then totalOR = true   # Set Important caller to true
    when /^02662/     then totalOR = true   # Set local numbers 02662..... to true
   end

# If totalOR, override both day and time settings
 if totalOR 
     $dayOR  = true
     $timeOR = true
 end

dialler("#{sys.Username}@local",20)              # 20sec ring between default times 7:30am to 11:30pm everyday

# Reset OverRides back to false for following diallers to still obey their time settings.
$dayOR  = false
$timeOR = false

dialler("0487654321@ptel",20,"1700","1800",1,5)  #      mobile 5:00pm to  6:00pm Monday to Friday 
dialler("0487654321@ptel",20,"0800","2200",6,0)  # same mobile 8:00am to 10:00pm Saturday to Sunday 

code = 480
if $status.to_s == 'Failed' then
     $status = 'Busy or Offline'
     code = 486
end

sys.log("failed code:#{code} #{$status}")
sys.Respond(code, "#{$status}")
In the above example I OverRide only the first dialler on both day and time settings then remove this OverRide on the following dialler times.
billion 7404VGP
dialplan (<9*:*>[0-9*][0-9*].T<:@sipbroker>|[0-9*].T)

kave
Posts: 7
Joined: Sat Jun 05, 2010 11:47 pm

Re: Day of the Week and Time of the Day Forwarding inplan

Post by kave » Tue Jun 22, 2010 2:16 am

Wow! This looks like a great feature. I was just wondering though if you want to implement the "if sys.IsAvailable() then" variable where should it be pasted in this dialplan?

I'm currently using the configuration below, which I want to keep even if the incoming call is made during "opening hours". For example if somebody during openinghours calls while my SIP-phone which happens to be turned off, I want them to be diverted to voicemail.
if sys.IsAvailable() then
sys.Dial("#{sys.Username}@local",30)
sys.Dial("voicemail@provider",20)
sys.Respond(480, "#{sys.Username} Not available")
else
sys.Dial("voicemail@provider",20)
sys.Respond(480, "#{sys.Username} Not available")
end

Another thing that I'm wondering is if the Ruby syntax allows the use of "goto" like programming batchfiles does.
Then maybe there could be a section in the diaplan where you define what SIPSORCERY should do if "sys.IsAvailable" is negative. So that if you have many different opening hours for different days, you dont have to manually configure each day if you for example want to make Sipsorcery divert the calls to a cell-phone instead of to voicemail.

Regards

Kave

mnipp
Posts: 192
Joined: Sat Oct 03, 2009 9:48 am
Location: NSW Australia

Re: Day of the Week and Time of the Day Forwarding inplan

Post by mnipp » Tue Jun 22, 2010 12:55 pm

For your above example you only need these two lines as you are still going to voicemail if the phone is offline.

Code: Select all

dialler("#{sys.Username}@local",20)          # 20sec ring between default times 7:30am to 11:30pm everyday

$status = sys.Dial("voicemail@provider",20)  # 24hr backup voicemail
This example we will forward the call to a fixed line if the voip phone is offline during openhours.

Code: Select all

if sys.IsAvailable() then

    dialler("#{sys.Username}@local",20)          # 20sec ring between default times 7:30am to 11:30pm everyday
    $status = sys.Dial("voicemail@provider",20)  # 24hr backup voicemail    
else
    dialler("backupnumber@provider",20)          # 20sec ring between default times 7:30am to 11:30pm everyday
    $status = sys.Dial("voicemail@provider",20)  # 24hr backup voicemail
end

code = 480
if $status.to_s == 'Failed' then
   $status = 'Busy or Offline'
  code = 486
end

sys.log("failed code:#{code} #{$status}")
sys.Respond(code, "#{$status}")
billion 7404VGP
dialplan (<9*:*>[0-9*][0-9*].T<:@sipbroker>|[0-9*].T)

kave
Posts: 7
Joined: Sat Jun 05, 2010 11:47 pm

Re: Day of the Week and Time of the Day Forwarding inplan

Post by kave » Wed Jun 23, 2010 4:43 am

Thank you mnipp I will try that code.

I'm courious to know what the syntax "$status" means, I haven't been able to figure that out on my own :(

Post Reply