Conditional music on hold without the use of queues
Last week, RMU won the first round of the NIT tournament against Kentucky. We got a request this week to make the hold music for the athletic department be a radio clip of the final minute of the game. Using a call queue, this would incredibly easy. But unfortunately, not all of the numbers in the athletic department are part of a call queue. I devised a solution to this problem today using AstDB. We'll automate the process of loading the extentions that require special hold music into AstDB along with the department name.
Example:
database put department 9001 Athletics
We'll then add an entry into musiconhold.conf with a context name that matches the department name:
[Athletics]
mode=files
directory=/var/lib/asterisk/moh/athletics 
From there, we just need to modify stdexten to check for a database entry and modify the channel variable musicclass on the fly:
exten => n,Set(CHANNEL(musicclass)=${IF($[${DB_EXISTS(department/${EXTEN})}]?${DB_RESULT}:default)})
This line checks to see if a database entry exists at department/${EXTEN}, and if so, sets the musicclass channel variable equal to the department name, ${DB_RESULT}. If there is no entry in the database, it simply uses the default music class.
Here's what this looks like in action, before and after a database entry (not contained within stdexten for clarity's sake):
asterisk*CLI> 
  == Using UDPTL CoS mark 5
  == Using SIP RTP CoS mark 5
    -- Executing [9001@ld:1] NoOp("SIP/1000-00000071", "") in new stack
    -- Executing [9001@ld:2] Set("SIP/1000-00000071", "CHANNEL(musicclass)=default") in new stack
    -- Executing [9001@ld:3] Dial("SIP/1000-00000071", "SIP/9001,30,r") in new stack
  == Using UDPTL CoS mark 5
  == Using SIP RTP CoS mark 5
    -- Called SIP/9001
    -- SIP/9001-00000072 is ringing
    -- SIP/9001-00000072 answered SIP/1000-00000071
    -- Remotely bridging SIP/1000-00000071 and SIP/9001-00000072
    -- Started music on hold, class 'default', on SIP/1000-00000071
Updated database successfullyasterisk*CLI>
== Using UDPTL CoS mark 5
== Using SIP RTP CoS mark 5
-- Executing [9001@ld:1] NoOp("SIP/1000-00000073", "") in new stack
-- Executing [9001@ld:2] Set("SIP/1000-00000073", "CHANNEL(musicclass)=Athletics") in new stack
-- Executing [9001@ld:3] Dial("SIP/1000-00000073", "SIP/9001,30,r") in new stack
== Using UDPTL CoS mark 5
== Using SIP RTP CoS mark 5
-- Called SIP/9001
-- SIP/9001-00000074 is ringing
-- SIP/9001-00000074 answered SIP/1000-00000073
-- Remotely bridging SIP/1000-00000073 and SIP/9001-00000074
-- Started music on hold, class 'Athletics', on SIP/1000-00000073
I think this is a fairly good solution to this problem. Hopefully someone else will find this useful.