first add this line to extensions_custom.conf:
[code]
#include extensions_users.conf
[/code]
then create a file called extensions_users.conf and add the following:
[code]
[userlogon]
exten => s,1,Read(AMPUSER|vm-extension) ;get users desired extension
exten => s,n,Set(DB(AMPUSER/${AMPUSER}/device)=); logoff any other devices
exten => s,n,Macro(user-logon,${AMPUSER}) ;log user in to device
exten => s,n,Macro(setq,${AMPUSER})
exten => s,n,AddQueueMember(${q}|Local/${AMPUSER}@from-internal) ;add user to queue
exten => s,n,Set(DB(AMPUSER/${AMPUSER}/lastactivity)=${EPOCH}) ;set lastactivity flag
exten => s,n,Playback(vm-goodbye)
exten => s,n,Hangup
[userlogoff]
exten => s,1,Set(dev=${DB(DEVICE/${CALLERID(num)}/user}) ;set the device user is logged in to
exten => s,n,Macro(user-logoff,) ;log off user
exten => s,n,Macro(setq,${dev})
exten => s,n,RemoveQueueMember(${q}|Local/${dev}@from-internal) ;remove user from queue
;exten => s,n,Set(DB(DEVICE/${dev}/user)=none) ;clean-up whatever macro-user-logoff didnt do
exten => s,n,Set(DB(AMPUSER/${dev}/lastactivity)=none) ;clear lastactivity flag
exten => s,n,Playback(vm-goodbye)
exten => s,n,Hangup
exten => autologoff,1,Goto(s,1)
[macro-setq]
exten => s,1,Noop(Macro Set Queue Number for ${ARG1})
exten => s,n,Set(AMPUSER=${IF($["${AMPUSER}" = ""]?${ARG1}:${AMPUSER})})
exten => s,n,Set(q=${IF($[$["${AMPUSER}" >= "800"] & $["${AMPUSER}" <= "899"]]?10:${q})})
exten => s,n,Set(q=${IF($[$["${AMPUSER}" >= "900"] & $["${AMPUSER}" <= "989"]]?11:${q})})
exten => s,n,Set(q=${IF($[$["${AMPUSER}" >= "990"] & $["${AMPUSER}" <= "999"]]?12:${q})})
exten => s,n,Set(q=${IF($[$["${AMPUSER}" >= "700"] & $["${AMPUSER}" <= "710"]]?5:${q})})
[users]
exten => *46,1,Gotoif($["${DB(DEVICE/${CALLERID(num)}/user)}" = "none"]?dial:nouser,1)
exten => *46,n(dial),Dial(Local/${EXTEN}@from-internal)
exten => *47,1,Dial(Local/${EXTEN}@from-internal)
exten => _.,1,Set(USER=${DB(DEVICE/${CALLERID(num)}/user)})
exten => _.,n,Noop(*******ampuser is ${USER}, callerid id ${CALLERID(num)})
exten => _.,n,Gotoif($["${USER}" = "none"]?nouser,1)
exten => _.,n,Gotoif($["${USER}" = ""]?nouser,1)
exten => _.,n,Set(callnum=${EXTEN})
exten => _.,n,Macro(user-callerid)
exten => _.,n,Set(DB(AMPUSER/${USER}/lastactivity)=${EPOCH})
exten => _.,n,Dial(Local/${EXTEN}@from-internal)
exten => nouser,1,Playback(vm-incorrect)
exten => nouser,n,Hangup
exten => h,1,Hangup
[/code]
Next add the following to extensions.conf. This line goes under the first two lines of macro-hangupcall
[code]
exten => s,n,Set(DB(AMPUSER/${CALLERID(num)}/lastactivity)=${IF($["$LEN(${CALLERID(num)})" <= "4"]?${EPOCH})}) ;set last activity flag for extensions
[/code]
Next add the following script to /var/lib/asterisk/bin, and save it with the name agent_loggoff.sh
(hint: make sure its executable)
[code]
#!/bin/bash
#Run this script every x minutes to logoff dynamicaly addd queue memebers. This script is desinged to work with freepbx's users and devices
logoff ()
{
file="$RANDOM.call"
echo "Channel: $1" >> "$file"
echo "CallerID: system <${2}>" >> "$file"
echo "MaxRetries: 6" >> "$file"
echo "RetryTime: 2" >> "$file"
echo "WaitTime: 30" >> "$file"
echo "Context: custom-userlogoff" >> "$file"
echo "Extension: autologoff" >> "$file"
echo "Priority: 1" >> "$file"
echo "Set: SIPADDHEADER=Call-Info: \;answer-after=0" >> "$file"
echo "Set: ALERT_INFO=Ring Answer" >> "$file"
echo "Set: SIP_URI_OPTIONS=intercom=true" >> "$file"
chown asterisk:asterisk "$file"
mv $file /var/spool/asterisk/outgoing
}
#define max idle time
maxtime=$2
#define queue to monitor
qnum=$1
qmembers=`/usr/sbin/asterisk -rx "queue show $qnum" | awk '/Local/ && /\(dynamic\)/{ print $1,$(NF-2) }'`
if ! [ "$qmembers" ]; then exit; fi #no q qmemberss - no need to do anything
while read qmembers qidletime; do
device=`/usr/sbin/asterisk -rx "database showkey AMPUSER/$(sed 's/@from-internal//' <<< ${qmembers:6})/device" | grep -a device | cut -d":" -f2 | sed 's/ //g' `
dial=`/usr/sbin/asterisk -rx "database showkey DEVICE/$device/dial" | grep -a dial | cut -d":" -f2 | sed 's/ //g' `
lastactivity=`/usr/sbin/asterisk -rx "database showkey AMPUSER/$(sed 's/@from-internal//' <<< ${qmembers:6})/lastactivity" | grep -a lastactivity | cut -d":" -f2 | sed 's/ //g' `
now=`date +%s`
#echo TIME $time
#echo QIDELTIME $qidletime
#echo LASTACTIVITY $lastactivity
#echo MAXTIME $maxtime
#echo $((now - lastactivity))
# qmembers didnt recive a q call in the past maxtime, check the lastactivity and log him off
if (( qidletime >= maxtime )) && (( (now - lastactivity) >= maxtime )); then
logoff $qmembers $device;
echo "no call for $qidletime, lastactivity is $lastactivity (time now is $time)"
exit
#qmembers hasnt recived ANY q calls (so he has no qidletime), just check lastactivity and log him off
elif [[ $qidletime = *[^0-9]* ]] && (( (now - lastactivity) >= maxtime )); then
logoff $qmembers $device;
echo "no q calls, lastactivity is $(( $now - $lastactivity))"
exit
#Do nothing
else
echo "nothing to do for qmember $qmembers"
fi
done <<< "$qmembers"
[/code]
Now for the setup:
First of all, you need to be running in FreePBX's users and devices mode. To enable this change the AMPEXTENSIONS setting in /etc/amportal.conf to deviceanduser
Second, in order to ensure that users don’t make outgoing calls unless there logged in, you'll need to change the context of the device to "users" (no quotes).
Next you'll want to use the Misc Applications module to create a custom destination for logging in and logging out. Click on Misc Applications, click Add Misc Applications. In the Description field add: agent-logon. For feature code I use *46. Set the custom destination to userlogon,s,1. Click submit. Now add another Mics App with the description agent-logoff, Feature code *47, and destination userlogoff,s,1
Now edit /etc/crontab and add the following line:
[code]
* * * * * root /var/lib/asterisk/bin/agent-logoff.sh 10 600
[/code]
Where 10 is the desired queue number and 600 is the desired amount of inactivity after which to logoff an agent (in seconds). To make the changes take affect do:
[code]
service crond restart
[/code]
Please note: after you switch the context, you will not be able to place any phone calls without logging in first. Also you will need to edit the macro-setq to match your queues.
Last but not least, don't forget to do an amportal restart to ensure permissions are set properly and that asterisk re-reads the configs
Last updated: jan, 10 2008