text
stringlengths
0
152
We’ve got the condiments now, so let’s get to our dialplan. This is where the magic is
going to happen.
Somewhere in extensions.conf we are going to create the [hotdesk] context. To start,
let’s define a pattern-match extension that will allow the users to log in:
[hotdesk]
include => sets
exten => _*99110[1-5],1,Noop(Hotdesk login)
same => n,Set(HotExten=${EXTEN:3}) ; strip off the leading *99
same => n,Noop(Hotdesk Extension ${HotExten} is changing status) ; for the log
same => n,Set(${HotExten}_STATUS=${HOTDESK_INFO(status,${HotExten})})
same => n,Set(${HotExten}_PIN=${HOTDESK_INFO(pin,${HotExten})})
same => n,Noop(${HotExten}_PIN is now ${${HotExten}_PIN})
same => n,Noop(${HotExten}_STATUS is ${${HotExten}_STATUS})})
We’re not done writing this extension yet, but we need to digress for a few pages to
discuss where we’re at so far.
When a sales agent sits down at a desk, they log in by dialing *99 plus their extension
number. In this case we have allowed the 1101 through 1105 extensions to log in with
our pattern match of _99110[1-5]. You could just as easily make this less restrictive
by using _9911XX (allowing 1100 through 1199). This extension uses func_odbc to
perform a lookup with the HOTDESK_INFO() dialplan function. This custom function
(which we will define in the func_odbc.conf file) performs an SQL statement and
returns whatever is retrieved from the database.
So, let’s create the /etc/asterisk/func_odbc.conf file, and within that define the new
function HOTDESK_INFO():
$ sudo -u asterisk vim /etc/asterisk/func_odbc.conf
[INFO]
prefix=HOTDESK
dsn=asterisk
synopsis=Select value of field in ARG1, where 'extension' matches ARG2
description=Allow dialplan to extract data from any field in pbx.ast_hotdesk table.
readsql=SELECT ${ARG1} FROM pbx.ast_hotdesk WHERE extension = '${ARG2}'
That’s a lot of stuff in just a few lines. Let’s quickly cover them before we move on.
Getting Funky with func_odbc: Hot-Desking
|
267
You should be able to reload your dialplan (dialplan reload) and
func_odbc (module reload func_odbc.so), and test the dialplan
out thus far (dial 991101 from one of the sets you’ve assigned to
this context). Make sure your console verbosity is set to at least 3
(*CLI> core set verbose 3), as you will only be able to see this
dialplan working by the console (a call to this dialplan will return a
fast busy even if it runs successfully). For the rest of this section, we
strongly recommend you test everything after each change. If you
don’t, you’ll have a whale of a time trying to find the bugs. It’s criti‐
cal that you code with a phone registered and the Asterisk console
open, so you can reload and test changes within seconds of writing
them.
First of all, the prefix is optional (default prefix is 'ODBC'). This means that if you
don’t define a prefix, Asterisk adds 'ODBC' to the function name (in this case, INFO),
which means this function would become ODBC_INFO(). This is not very descriptive
of what the function is doing, so it can be helpful to assign a prefix that helps to relate
your ODBC functions to the tasks they are performing. We chose 'HOTDESK', which
means that this custom function will be named HOTDESK_INFO() in the dialplan.
The reason why prefix is separate is that the author of the module
wanted to reduce possible collisions with existing dialplan func‐
tions. The intent of prefix was to allow multiple copies of the same
function, connected to different databases, for multitenant Asterisk
systems. We as authors have been a bit more liberal in our use of
prefix than the developer originally intended.
The dsn attribute tells Asterisk which connection to use from res_odbc.conf. Since
several database connections could be configured in res_odbc.conf, we specify which
one to use here. In Figure 15-1, we show the relationship between the various file
configurations and how they reference down the chain to connect to the database.
The func_odbc.conf.sample file in the Asterisk source contains addi‐
tional information about how to handle multiple databases and
control the reading and writing of information to different DSN
connections. Specifically, the readhandle, writehandle, readsql,
and writesql arguments will provide you with great flexibility for
database integration and control.
Finally, we define our SQL statement with the readsql attribute. Dialplan functions
can be called with two different formats: one for retrieving information, and one for
setting information. The readsql attribute is used when we call the HOTDESK_INFO()
function with the retrieve format (we could execute a separate SQL statement with
268
|
Chapter 15: Relational Database Integration
the writesql attribute; we’ll discuss the format for that attribute a little bit later in