I am looking for a way to dynamically get a list of queues that an extension is a part of as part of an evaluation in my dialplan.
I was thinking I could use the data in the MySQL asterisk.kvstore_FreePBX_modules_Queuestats. Each record seems to list all the extension users and their queues, but there are thousands of records and I am now sure how to pick the latest one?
I tried sorting ascending and descending on id (not a listed field, but query took). Does anyone know how to get the latest entry (which I am hoping would show the latest updates to queue agents)?
exten => s,n,Set(QueueMemList=${QUEUE_MEMBER_LIST(8003)})
exten => s,n,Noop(Here's the list: ${QueueMemList})
And the log:
-- Executing [s@get-q:3] Set("SIP/64066-000000d4", "QueueMemList=Local/101@from-queue/n,Local/204@from-queue/n,Local/202@from-queue/n,Local/201@from-queue/n") in new stack
-- Executing [s@get-q:4] NoOp("SIP/64066-000000d4", "Here's the list: Local/101@from-queue/n,Local/204@from-queue/n,Local/202@from-queue/n,Local/201@from-queue/n") in new stack
As you can see, this returns the static members as well.
You can probably achieve it by using a while loop and the CUT function.
My only problem with this approach is I won’t know the queue numbers ahead of time and I could code them all to be checked, but then every time a new queue is added I will have to go back and modify the code. I see in the asterisk.kvstore_FreePBX_modules_Queuestats seems like it lists all the agents and all of their queues, but I cannot seem to figure out how to get to the most recent record (the fields are key and val), but it looks like I could use this.
My other thought is to use the show queue command to get a list of all the queues, then use the queue member list to find the agent, but hoping to find a more simplified approach. I’m going to see if I can understand the key value and report back.
I don’t have that module installed, so I don’t have that table on my VM.
I did see tho the following:
MariaDB [(none)]> use asterisk;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [asterisk]> select extension from queues_config;
+-----------+
| extension |
+-----------+
| 4110 |
| 5110 |
| 8000 |
| 8001 |
| 8003 |
| 9000 |
+-----------+
6 rows in set (0.00 sec)
MariaDB [asterisk]>
I shifted gears and this is my approach. It seems inefficient, but works.
Set a loop counter ${QUEUELOOP} to 1. exten => s,n,Set(QUEUELOOP=1)
Count the number of queues on the system, and set them to a variable: ${QUEUELOOPMAX}. I am using the word “max” as that shows up on every queue line for the ‘queue show’ command. exten => s,n(loopstart),Set(QUEUELOOPMAX=${SHELL(asterisk -rx 'queue show' | egrep max | wc -l)}
Get an ordered list of queues and select the nth queue, based on current value of ${QUEUELOOP}. exten => s,n,Set(QUEUECHK=${SHELL(asterisk -rx 'queue show' | egrep max | grep -o '^\S*' | sort | awk 'NR==${QUEUELOOP}{ print; }'
Check for the extension as a member of the current queue under evaluation ${QUEUECHK}. exten => s,n,Set(QUEUEMEMBERRESULT=${SHELL(asterisk -rx 'queue show ${QUEUECHK}' | egrep ${CALLERID(dnid)} | cut -d"@" -f1 | cut -d"/" -f2)}
If the extension is contained in the ${QUEUEMEMBERRESULT} the extension is an agent in the queue, and transfer to the queue, otherwise increase the loop count by +1. exten => s,n,GotoIf($[${QUEUEMEMBERRESULT} = ${CALLERID(dnid)}]?route:noroute) exten => s,n(route),Goto(ext-queues,${QUEUECHK},1) exten => s,n(noroute),Set(QUEUELOOP=$[${QUEUELOOP} + 1])
If ${QUEUELOOPMAX}>${QUEUELOOP} then try again at the top going to the next skill, otherwise the treatment is over. exten => s,n,GotoIf($[${QUEUELOOPMAX} > ${QUEUELOOP}]?loopstart:hangup) exten => s,n(hangup),Hangup()
This cannot be the best way to do it, but it all works, in dialplan. Some of it could be combined, but I think the way it is broken out will make it easier to understand for future editors of the treatment.