text
stringlengths
0
152
this chapter).
Reading values from this function would take this format in the dialplan:
exten => s,n,Set(RETURNED_VALUE=${HOTDESK_INFO(status,1101)})
This would return the value located in the database within the status column where
the extension column equals 1101. The status and 1101 we pass to the
HOTDESK_INFO() function are then placed into the SQL statement we assigned to the
readsql attribute, available as ${ARG1} and ${ARG2}, respectively. If we had passed a
third option, this would have been available as ${ARG3}.
After the SQL statement is executed, the value returned (if any) is assigned to the
RETURNED_VALUE channel variable.
Using the ARRAY() Function
In our example, we are utilizing two separate database calls and assigning those values
to a pair of channel variables: ${HotdeskExtension}_STATUS and ${HotdeskExten
sion}_PIN. This was done to simplify the example. We’re going to shorten the names
of the variables here because the printed format can’t handle such long lines, so in the
following examples, you’ll see “HE” in place of “HotdeskExtension.” If you’re going to
code this example, please replace HE with HotdeskExtension:
same => n,Set(${HE}_STATUS=${HOTDESK_INFO(status,${HE})})
same => n,Set(${HE}_PIN=${HOTDESK_INFO(pin,${HE})})
As an alternative, we could have returned multiple columns and saved them to sepa‐
rate variables utilizing the ARRAY() dialplan function. If we had defined our SQL
statement in an func_odbc.conf function like so:
readsql=SELECT pin,status FROM ast_hotdesk WHERE extension = '${HE}'
we could have used the ARRAY() function to save each column of information for the
row to its own variable with a single call to the database (note that we’re using an
example function called HOTDESK_INFO(), which we haven’t created):
same => n,Set(ARRAY(${HE}_PIN,${HE}_STATUS)=${HOTDESK_INFO(${HE})})
Using ARRAY() is handy any time you might get comma-separated values back and
want to assign the values to separate variables, such as with CURL(). However, it can
also make your code more complicated to read, debug, and maintain.
So, in the first two lines of the following block of code, we are passing the value
status and the value contained in the ${HotdeskExtension} variable (e.g., 1101) to
the HOTDESK_INFO() function. The two values are then replaced in the SQL statement
Getting Funky with func_odbc: Hot-Desking
|
269
with ${ARG1} and ${ARG2}, respectively, and the SQL statement is executed. Finally,
the value returned is assigned to the ${HotdeskExtension}_STATUS channel variable.
Let’s finish writing the pattern-match extension now:
exten => _*99110[1-5],1,Noop(Hotdesk login)
same => n,Set(HotdeskExtension=${EXTEN:3}) ; strip off the leading *99
same => n,Noop(Hotdesk Extension ${HotdeskExtension} is changing status) ; for the log
same => n,Set(${HotdeskExtension}_STATUS=${HOTDESK_INFO(status,${HotdeskExtension})})
same => n,Set(${HotdeskExtension}_PIN=${HOTDESK_INFO(pin,${HotdeskExtension})})
same => n,Noop(${HotdeskExtension}_PIN is now ${${HotdeskExtension}_PIN})
same => n,Noop(${HotdeskExtension}_STATUS is ${${HotdeskExtension}_STATUS})})
same => n,GotoIf($["${${HotdeskExtension}_PIN}" = ""]?invalid_user)
same => n,GotoIf($[${ODBCROWS} < 0]?invalid_user)
same => n,GotoIf($[${${HotdeskExtension}_STATUS} = 1]?logout:login,1)
We’ll be writing some labels to handle invalid_user and logout a bit later, so don’t
worry if it seems something is missing.
You may have noticed that in some of the Goto/GotoIf examples,
there might be a ,1 in the directive. This might seem confusing
unless you recall that the target only needs the difference between
the current context,extension,priority/label. So, if you send
something to a label, such as logout, that is in the same extension,
you don’t need to specify the context and extension, whereas if you
are sending the call to the extension named login (still in the same
context), you need to specify that you wish the call to go to label/
priority 1. In the previous example, we could write our directive as
follows:
... = 1] ? hotdesk,${EXTEN},logout : hotdesk,login,1
^same ^same ^diff ^same ^diff ^diff
In other words, true goes to context [hotdesk], extension
99110[1-5], label logout; and false goes to context [hotdesk],
extension login, and label/priority 1.
We only wrote what’s different.
If you want, for clarity, you can always write context,exten
sion,priority for all your directives. It’s your call.
After assigning the value of the status column to the ${HotdeskExtension}_STATUS
variable (if the user identifies themself as extension 1101, the variable name will be
1101_STATUS), we check if we’ve received a value back from the database using the
${ODBCROWS} channel variable.
The last row in the block checks the status of the phone and, if the agent is currently
logged in, logs them off. If the agent is not already logged in, it will go to the login