Page 1 of 2

My dialplan with time regulated incoming call forwarding

PostPosted: Mon Apr 20, 2009 8:42 pm
by TheFug
With the help of jvwelzen, i finally, have my incoming call section working for a time regulated forward function, (onto the minute)
Notice that you need to include the zero, for the morning hours below 10 o ' clock because of the 24 hour clock use. (H%) (in uppercase makes the difference, i guess)

Code: Select all
#Ruby

sys.Trace=true # when needed to sip trace

sys.Log("call received on #{req.URI.ToString()} from #{req.Header.From.FromURI.ToString()} at #{Time.now}.")

if sys.Out then
sys.Log("Outgoing call from user domain=#{sys.FromDomain}.")

# my outgoing call detection section
   
case req.URI.User
  when /^(100$|101$|102$|103$)/ then sys.Dial("HUIZENW") # speeddials of HuizenWireless
  when /^(8$|613$|612$|958|615$)/ then sys.Dial("BRUJULA") # some speed dials for Brujula
  when /^00498/ then sys.Dial("275498${dst:5}@BRUJULA") # dials German tollfree numbers
# when /^0031/ then sys.Dial("${dst}@CALLCENTRIC") # dials Dutch national numbers
  when /^900/ then sys.Dial("${dst}@TPAD") # selects Tpad for international numbers
  when /^0900/ then sys.Dial("90900${dst:4}@TPAD") # dials Dutch pay info numbers
  when /^121/ then sys.Dial("121@TPAD") # Tpad voicemail
  when /^0018/ then sys.Dial("${dst:2}@SIPHAPPENS") # dials US tollfree numbers
  when /^555/ then sys.Dial("613@BRUJULA") # Brujula echo test
  when /^(0044800|0018|0044500|0044808|0049800|0049130|0047800)/ then sys.Dial("${dst}@SIPHAPPENS") # dials various tollfree numbers with siphappens

  when /^06/ then sys.Dial("900316${dst:2}@TPAD") # directs outgoing mobilephone calls
when /^080/ then sys.Dial("00318${dst:2}@HUIZENW")
when /^(01|02|03|04|05|07|08)/  then sys.Dial("0031${dst:1}@CALLCENTRIC")  # dials Dutch landline numbers
  when /^(883|882)/ then sys.Dial("${dst}@VOXALOT")  #detects iNUM/eNUM & dials with Voxalot
  when /^(00883|00882)/ then sys.Dial("${dst:2}@VOXALOT")  #detects iNUM/eNUM & dials with Voxalot
 
  when /^1777/ then sys.Dial("CALLCENTRIC") # dials to other Callcentric users
  when /^\*123/ then sys.Dial("${dst}@CALLCENTRIC") # Callcentric voice mail
  when /^\*75/ then sys.Dial("CALLCENTRIC") # Dials the Callcentric  speeddials
  when /^(600|500)/ then sys.Dial("*${dst}@VOXALOT") # speed dials Voxalot

# my speeddial section

  when /^\*1/ then sys.Dial("${dst:2}@BRUJULA")
  when /^\*2/ then sys.Dial("${dst:2}@BUDGET")
  when /^\*3/ then sys.Dial("${dst:2}@HUIZENW")
  when /^\*6/ then sys.Dial("${dst:2}@VOXALOT")
  when /^\*7/ then sys.Dial("${dst:2}@CALLCENTRIC")
  when /^\*8/ then sys.Dial("${dst:2}@SIPBROKER")
  when /^\*9/ then sys.Dial("${dst:2}@TPAD")
  when /^\*0/ then sys.Dial("${dst:2}@SIPHAPPENS")
   

end

# my incoming call section

else
sys.Log("Incoming call to domain=#{sys.ToDomain} from host #{req.Header.From.FromURI.Host}.")

  t = Time.now + (1*60*60)  # Get current time and adjust to local time GMT+1 no DST needed
sys.Log(t.strftime('local time time: %c'))
tijd = t.strftime('%H:%M')
sys.Log("Tijd : #{tijd}")
if (1..5) === t.wday && ('06:35'..'17:15') === tijd # next lines will be executed when this condition is valid
sys.Dial("900316xxxxxxxx@TPAD" ,25)
sys.Dial("9003120xxxxxxx@TPAD")

       end

#for all other calls outside the above time and days calls go to MSS registered phone at home     

if sys.IsAvailable() then

sys.Log("FromHost: '#{req.Header.from.FromURI.Host}'")
sys.Log("FromUser: '#{req.Header.from.FromURI.User}'")
sys.Log("FromName: '#{req.Header.From.FromName}'")


   sys.Log("#{sys.Username} is online.")
   sys.Dial("#{sys.Username}@local")

else
   sys.Log("#{sys.Username} is not online.")
sys.Log("FromHost: '#{req.Header.from.FromURI.Host}'")
sys.Log("FromUser2: '#{req.Header.from.FromURI.User}'")
sys.Log("FromName2: '#{req.Header.From.FromName}'")

# when offline calls routed to cellphone

   sys.Dial("00316xxxxxxxx@CALLCENTRIC")
#sys.Respond(480, "#{sys.Username} Not online")
end
end

PostPosted: Tue Apr 21, 2009 8:49 am
by jvwelzen
I still found a problem with the hours

this works great

Code: Select all
t = Time.now + (1*60*60) #er hoeft geen DST bij
sys.Log(t.strftime('local time time: %c'))
tijd = t.strftime('%H:%M')
sys.Log("Tijd : #{tijd}")
if (1..5) === t.wday && ('08:00'..'17:45') === tijd


but this is still not working

The code stops working after 00:00

Code: Select all
t = Time.now + (1*60*60) #er hoeft geen DST bij
sys.Log(t.strftime('local time time: %c'))
tijd = t.strftime('%H:%M')
sys.Log("Tijd : #{tijd}")
if (1..5) === t.wday && ('23:20'..'08:45') === tijd


the morning time needs the 0 in front

A solution could be

Code: Select all
t = Time.now + (1*60*60) #er hoeft geen DST bij
sys.Log(t.strftime('local time time: %c'))
tijd = t.strftime('%H:%M')
sys.Log("Tijd : #{tijd}")
if (1..5) === t.wday && ('23:20'..'00:00') || ('00:00'..'08:45') === tijd

PostPosted: Wed Apr 22, 2009 10:33 am
by jvwelzen
I think I found a solution for the hours

Or I almost know for sure


Code: Select all
t = Time.now + (1*60*60)  # Get current time and adjust to huizenwireless.nl (GMT+1)
sys.Log(t.strftime('huizenwireless.nl time: %c'))
tijd = t.strftime("%H:%M")
sys.Log("Tijd Nu : #{tijd}")

starttime = '22:45' 
stoptime = '12:23'   
   
if stoptime < starttime
  if ("#{starttime}".."00:00") === tijd || ("00:00".."#{stoptime}") === tijd
sys.Respond(486, "User Not Online")
  end
end
if stoptime > starttime
  if ("#{starttime}".."#{stoptime}") === tijd
  sys.Respond(486, "User Not Online")
  end
end

PostPosted: Wed May 06, 2009 5:48 pm
by TheFug
The nice thing is: when the first number is a cellphone, and you cancel the
call on the cellphone, the second number will be dialed, before the 25 seconds of the timer in the first line.
Code: Select all
sys.Dial("900316xxxxxxxx@TPAD" ,25)
sys.Dial("9003120xxxxxxx@TPAD")

So, the timer doesn't need to be set, if you're near these two endpoints, most of the time, and you take the call on a cheaper landline number, by cancelling it on the cellphone.

btw. Tpad has changed it's policy use of it's international DID's you need to add about 7 euro per month to being able to make use of it, if you don't do this, you can't be reached via their DID.

PostPosted: Sat Jul 25, 2009 12:29 am
by TheFug
User jvwelzen kindly rewrote my incoming dialplan, for use with SipSorcery,
this mostly because of the time function which worked on MSS but not yet on SS.
The time function serves for me the function, that during office times, (i include travel time in that) all calls go first to my mobile/GSM i can reject the call if i'm near to the landline phone, which is set in "call hunting" in this plan for that.
Code: Select all
sys.Log("** Call from #{req.Header.From.ToString()} to #{req.URI.User} **")

  t = Time.now + (9*60*60)  # Get current time and adjust US zone to GMT+1)
  sys.Log(t.strftime("time is: %c"))

  tijd = t.strftime("%H:%M")

  sys.Log("Tijd Nu : #{tijd}")

   # Forward at time period
   
starttime = "06:00"
stoptime = "18:00"   
   
  if stoptime < starttime
  if ("#{starttime}".."00:00") === tijd || ("00:00".."#{stoptime}") === tijd
  sys.Respond(486, "User Not Online")
  end
  end
  if stoptime > starttime
if ("#{starttime}".."#{stoptime}") === tijd
sys.Dial("mobilepnone@CALLCENTRIC",19)
sys.Dial("#{req.URI.User}@local",30)
  end
  end

  sys.Dial("#{req.URI.User}@local",30)

  sys.Respond(486, "User is busy, Or not online")


Because SipSorcery is run in a US time zone this needs to be corrected,
which is done in this version, for me this (in) dialplan is working great .
This version does also "cross the midnight hour", so you can set a time period from 23:40 to 04:15

note: you can make and store more dialplans you need at SS, and switch between them... handy !

(btw. tijd is the Dutch translation for time, and nu = now )

My example of time control on incoming call.

PostPosted: Sat Oct 24, 2009 10:22 am
by mnipp
After looking for time control for sipsorcery incoming dial plan.
I now use this example from other posts with timezone setting and corrected midnight time crossing. I found was needed.
Code: Select all
# read Time
Tz = +11     # Time zone (GMT+10)Sydney AEST time change to (+11 ADST for daylight saving time)
  t = Time.now.utc + (Tz*60*60)  # Get current time in utc+0 and adjust to sydney time utc+10(or utc+11).
  sys.Log(t.strftime('Local time: %c'))
   hhmm = t.strftime("%H%M")

starttime = '0800'   #  8:00am(0800) to
stoptime  = '2330'   # 11:30pm(2330) 24 hour time

if stoptime < starttime
  if (starttime..'2359') === hhmm or ('0000'..stoptime) === hhmm
    openhours = true
  else
    openhours = false
  end
end
if stoptime > starttime
  if (starttime..stoptime) === hhmm
    openhours = true
  else
    openhours = false
  end
end

if sys.IsAvailable() and openhours then    # if online and not midnight

   sys.Dial("#{sys.Username}@local",60)
   sys.Respond(480, "#{sys.Username} Not Available")
else
   sys.Respond(480, "#{sys.Username} Not Available") if openhours
   
   hhmm = t.strftime("%I:%M %p")      # Time (HH:MM AM) format
   sys.Respond(480, "(#{hhmm})#{sys.Username} asleep,call later")
end


being new to ruby and sipsorcery thanks for the help these posts have giving me. :)

My example of time control on incoming call.

PostPosted: Fri Nov 06, 2009 11:31 am
by mnipp
I find I can now simplify the time code to a few lines.
Code: Select all
# read Time
Tz = +11     # Time zone (GMT+10)Sydney AEST time change to (+11 ADST for daylight saving time)
  t = Time.now.utc + (Tz*60*60)  # Get current time in utc+0 and adjust to sydney time utc+10(or utc+11).
  sys.Log(t.strftime('Local time: %c'))
   hhmm = t.strftime("%H%M")

starttime = '0800'   #  8:00am(0800) to
stoptime  = '2330'   # 11:30pm(2330) 24 hour time, no midnight ring.

if stoptime < starttime   # test for midnight crossing
                          # If openhours includes midnight hour test the closed time range
   openhours = (stoptime..starttime) === hhmm ? false : true
else
   openhours = (starttime..stoptime) === hhmm ? true : false    # Testing the open time range
end

if sys.IsAvailable() and openhours then    # if online and not midnight

   sys.Dial("#{sys.Username}@local",60)
   sys.Respond(480, "#{sys.Username} Not Available")
else
   sys.Respond(480, "#{sys.Username} Not Available") if openhours
   
   hhmm = t.strftime("%I:%M %p")      # Time (HH:MM AM) format
   sys.Respond(480, "(#{hhmm})#{sys.Username} asleep,call later")
end

PostPosted: Fri Nov 06, 2009 2:23 pm
by MikeTelis
Irrelevant remark: sys.Respond with first argument (code) >= 200 aborts your script execution.
Code: Select all
   sys.Respond(480, "#{sys.Username} Not Available") if openhours
   
   hhmm = t.strftime("%I:%M %p")      # Time (HH:MM AM) format
   sys.Respond(480, "(#{hhmm})#{sys.Username} asleep,call later")

Two lines below the first sys.Respond(480...) won't be processed.

Relevant remark: you could have reduced it to single line:
Code: Select all
openhours = stoptime < starttime ? !((stoptime..starttime) === hhmm) : (starttime..stoptime) === hhmm

Yet another, totally cryptic alternative (without ranges):
Code: Select all
op = stoptime > starttime ? :>= : :<=
openhours =  hhmm.send(op,starttime) && stoptime.send(op,hhmm)

PostPosted: Fri Nov 06, 2009 11:40 pm
by mnipp
The [ if openhours ] on the end of the line, will skip this line if not true. It tests just find on a midnight ring.
The other examples look like a great way to simplify further. Thankyou :-)
Code: Select all
   sys.Respond(480, "#{sys.Username} Not Available") if openhours
   
   hhmm = t.strftime("%I:%M %p")      # Time (HH:MM AM) format
   sys.Respond(480, "(#{hhmm})#{sys.Username} asleep,call later")

PostPosted: Sat Nov 07, 2009 6:52 am
by MikeTelis
Yep, I forgot to invert the result:
Code: Select all
op = (invert = stoptime < starttime)? :<= : :>=
openhours =  invert ^ (hhmm.send(op,starttime) && stoptime.send(op,hhmm))