springloaded
springloaded

Reputation: 1079

Writing a Lua script using FreeSWITCH's native PostgreSQL support?

I am learning how to use FreeSWITCH using the FreeSWITCH 1.2 book written by the authors of FreeSWITCH.

In Chapter 7, it is explained how to use a Lua script along with connecting to a database. However, I have a feeling this book was written before FS 1.2.5 was out and doesn't account for the native support of PostgreSQL. The result is a confusing non working sample code. This is the outdated part of the book: http://books.google.ca/books?id=uISv9J6wt88C&lpg=PT295&dq=Freeswitch%20Connecting%20to%20a%20database%20with%20LuaSQL&pg=PT293#v=onepage&q&f=false

I would like to understand how to interface FreeSWITCH using Lua with some kind of database but I couldn't find any relevant forum thread or resource.

Could anyone point me in the right direction?

Cheers!

Upvotes: 2

Views: 2777

Answers (1)

Caleb Bolton
Caleb Bolton

Reputation: 31

It has been a few months since iv'e written lua for fs. However I do distinctly remember not being able to get native postgresql support working easily. I opted for an odbc connector and running lua scripts from freeswitch's script directory. My steps to get lua script support working on CentOS 6.4 are as follows (should be fairly similar for other *nix environments) This assume you have a fully function freeswitch install up and running with mod_lua enabled.

1) Install postgresql-odbc.x86_64 (or equivalent)

2) Create/modify two files in the /etc directory as follows, the first is odbc.ini, the second is odbcinst.ini

ODBC.INI:

    [freeswitch]
Driver=/usr/lib64/psqlodbcw.so
Description=Connection to LDAP/POSTGRESQL
Servername=<ENTER SERVER IP HERE>
Port=5432   
Protocol=6.4
FetchBufferSize=99
Username=<ENTER USERNAME HERE> 
Password=<ENTER PASSWORD HERE>
Database=<ENTER DATABASE NAME HERE>
ReadOnly=no
Debug=1
CommLog=1

ODBCINST.INI:

[PostgreSQL]
Description = PostgreSQL driver for Linux & Win32
Driver      = /usr/lib64/psqlodbcw.so

You may have to ensure your drivers are pointing to the correct directories, then you should be able to make a lua script in your root freeswitch directory under "scripts", here is an example script, make sure to replace and with your db credentials in the following example:

local dbh = freeswitch.Dbh("odbc://freeswitch:<DB USERNAME>:<DB PASSWORD>") -- connect to ODBC database 

assert(dbh:connected()) -- exits the script if we didn't connect properly

local sqluuid  =    env:getHeader("uuid")
local sqlani   =    env:getHeader("sip_from_user")
local sqldni   =    env:getHeader("sip_req_user")
local sqlstart =    env:getHeader("start_stamp")
local sqlstop  =    env:getHeader("end_stamp")
local sqlpid   =    env:getHeader("presence_id")

local myquery = string.format("INSERT INTO cdr (uuid, ani, destination_number, start_stamp, end_stamp, presence_id) VALUES ('%s', '%s', '%s', TIMESTAMP '%s', TIMESTAMP '%s', '%s')",
sqluuid, sqlani, sqldni, sqlstart, sqlstop, sqlpid)

dbh:query(myquery) 


dbh:release() -- optional

Then call this script from your dialplan using:

<action application="set" data="session_in_hangup_hook=true"/>
<action application="lua" data="someluascript.lua"/>

If you run into any issues, let me know and i'll try to assist further.

Upvotes: 3

Related Questions