With func_odbc, you can write your own SQL functions that become available from the dialplan. FreePBX is already set up to access your CDR database with ODBC so it’s pretty easy to write a custom CDR generator that you can use anywhere, including in your messages context.
In /etc/asterisk/func_odbc.conf (this file is not managed by FreePBX, so you may edit it without worry), add a new section to define your SQL function. I wrote one that looks like this:
[MESSAGECDR]
dsn=asteriskcdrdb
writesql=INSERT INTO cdr SET calldate=NOW(), clid='${SQL_ESC(${VAL1})}', src='${SQL_ESC(${VAL2})}', dst='${SQL_ESC(${VAL3})}', lastapp='MessageSend', disposition='${VAL4}', userfield='${SQL_ESC(${VAL5})}'
The dsn=asteriskcdrdb is already configured for you by FreePBX. The writesql line defines the SQL query. I am inserting the most relevant fields. Anywhere you have user-generated data be sure to use the SQL_ESC
function to sanitize the value.
Now in my dialplan I can call this function after doing a MessageSend
:
(from /etc/asterisk/extensions_custom.conf; I’m using old chan_sip here but you can easily rework it for pjsip)
[messages]
; Deliver to local 4-digit extension
exten => _XXXX,1,MessageSend(sip:${EXTEN},${MESSAGE(from)})
same => n,Set(FROMUSER=${CUT(MESSAGE(from),<,2)})
same => n,Set(FROMUSER=${CUT(FROMUSER,@,1)})
same => n,Set(FROMUSER=${CUT(FROMUSER,:,2)})
same => n,Set(ODBC_MESSAGECDR()=${MESSAGE(from)},${FROMUSER},${EXTEN},${MESSAGE_SEND_STATUS},${MESSAGE(body)})
same => n,ExecIf($["${MESSAGE_SEND_STATUS}" == "FAILURE"]?Goto(messages,mail-${EXTEN},1))
same => n,Hangup()
...
After some manipulation to get the From user I put the values into CDR using the new ODBC function defined. It ends up looking like this in the CDR report:
Hovering over the caller ID field I get the full URI as a tooltip.