login | register
Fri 29 of Aug, 2008 [20:57 UTC]

voip-info.org

Discuss [2] History

Asterisk tips openclose.agi

Created by: opsys,Last modification on Fri 07 of Oct, 2005 [07:26 UTC] by smurfix

How to play different greetings based on time and date

Lets play a nighttime or daytime greeting and then continue
in the current context.

Place this in your agi-bin directory (usually /var/lib/asterisk/agi-bin):

#!/bin/sh
#
# Done by Alex Lopez (alex.lopez (at) O p s y s (dot) com)
# You may need to change the if/than logic depending on OS and Shell implementation.
#
TODAY=`date +%m%d%y`
TODAYSHORT=`date +%m%d`
#
DAY=`date +%a`
HOUR=`date +%H`
MINUTE=`date +%M`
TIMENOW=$HOUR$MINUTE
#
STATUS=closed
#
case "$TODAYSHORT" in
  0101 ) STATUS=holiday;; # New Years Day
  0704 ) STATUS=holiday;; # July Forth
  1224 ) STATUS=halfday;; # Christmas Eve
  1225 ) STATUS=holiday;; # Christmas Day
  1226 ) STATUS=holiday;; # Year End holiday
  1227 ) STATUS=holiday;; # Year End holiday
  1228 ) STATUS=holiday;; # Year End holiday
  1229 ) STATUS=holiday;; # Year End holiday
  1230 ) STATUS=holiday;; # Year End holiday
  1231 ) STATUS=holiday;; # Year End holiday
esac
case "$TODAY" in
  090505 ) STATUS=holiday;; # Labor Day
  112405 ) STATUS=holiday;; # Thanksgiving
  112505 ) STATUS=holiday;; # Day after ThanksGiving
  052906 ) STATUS=holiday;; # Memorial Day
  090406 ) STATUS=holiday;; # Labor Day
  112306 ) STATUS=holiday;; # Thanksgiving
  112406 ) STATUS=holiday;; # Day after ThanksGiving
  052807 ) STATUS=holiday;; # Memorial Day
  090307 ) STATUS=holiday;; # Labor Day
  112207 ) STATUS=holiday;; # Thanksgiving
  112307 ) STATUS=holiday;; # Day after ThanksGiving
  052608 ) STATUS=holiday;; # Memorial Day
  090108 ) STATUS=holiday;; # Labor Day
  112708 ) STATUS=holiday;; # Thanksgiving
  112808 ) STATUS=holiday;; # Day after ThanksGiving
esac
#
if  test "$STATUS" != "holiday" -a \
    "$DAY" != "Sat" -a \
    "$DAY" != "Sun" -a \
    "$TIMENOW" -gt "0800" -a \
    "$TIMENOW" -lt "1900"
then
  STATUS=open
fi
#
if  test "$STATUS" = "halfday" -a \
    "$TIMENOW" -gt "0800" -a \
    "$TIMENOW" -lt "1200"
then
  STATUS=open
fi
#
if  test "$DAY" = "Sat" -a \
    "$TIMENOW" -gt "0900" -a \
    "$TIMENOW" -lt "1600"
then
  STATUS=open
fi
#
if  test "$DAY" = "Sun" -a \
    "$TIMENOW" -gt "0900" -a \
    "$TIMENOW" -lt "1300"
then
  STATUS=open
fi
#
# Until I can get the Holiday logic done in extensions.conf
# We Be CLOSED!!!
#
if  test "$STATUS" = "holiday"
then
  STATUS=closed
fi
#
echo "SET VARIABLE status $STATUS \"\""

You can use it with an dial-plan (see extensions.conf) like this:

exten => s,1,Wait,2 ; Allow for PRI to grab info in facility
exten => s,2,AGI(openclose.agi)
exten => s,3,GotoIf($[${STATUS} = closed]?6:4)
exten => s,4,GotoIf($[${STATUS} = holiday]?8:10)
exten => s,5,Goto(s,12)
exten => s,6,BackGround(nighttime-greeting)
exten => s,7,Goto(s,12)
exten => s,8,BackGround(holiday-greeting)
exten => s,9,Goto(s,12)
exten => s,10,BackGround(daytime-greeting)
exten => s,11,NoOp
exten => s,12,BackGround(mainmenu)


Comments

Comments Filter
222

333Replaced brackets

by smurfix, Friday 07 of October, 2005 [07:26:57 UTC]
I replaced the brackets with a "test" statement. More wiki friendly, and does the same thing.
222

333Need to add [] around conditionals in if statements

by zwi, Thursday 10 of June, 2004 [18:56:19 UTC]
In order to make this work with my Red Hat 8 system (and adhere to standard bourne shell) I had to enclose the conditionals in [] like:

if [ "$DAY" = "Sun" -a \
    "$TIMENOW" -gt "0900" -a \
    "$TIMENOW" -lt "1300" ]]
then
 STATUS=open
fi

If your system always seems "closed" see if this is your problem. The best way to test this is just to run it on the command line and see if the script gives you errors.

Actually it looks like tiki strips out square brackets when posting so that may be the original problem. I guess when posting code to this wiki use [[ and ]]]