Skip to main content

JavaScript Example - Session in Hangup Hook

About

A simple example for accessing the session object after hangup when invoking the JS from the XML dialplan. More specifically, we're capturing the duration of the call and are saving it in a mySQL database with ODBC.

Dialplan

For the session to still be available after hangup, session_in_hangup_hook must be set. See below on how to invoke a script from the XML dialplan after hangup with api_hangup_hook.

Dialplan session_in_hangup_hook / api_hangup_hook

<action application="set" data="session_in_hangup_hook=true"/>
<action application="bridge" data="{api_hangup_hook=jsapi::callDuration.js}sofia/internal/123"/>

Javascript

This is the JavaScript file, which goes into the /scripts folder.

callDuration.js

// Database access, requires ODBC, see https://freeswitch.org/confluence/display/FREESWITCH/Using+ODBC+in+the+core
use("ODBC");

// Connect to mySQL
var db = new ODBC("MySQL", "databaseUser", "databasePassword");
db.connect();

// Get and log all available session variables.
var env = request.dumpENV("text");
console_log(env);

// Fetch call duration from request object.
var duration = request.getHeader("billsec"); // This will work for any variable contained in the list logged above

// Insert into DB.
db.query("INSERT INTO myCallLog (duration) VALUES ('"+duration+"')";"); // Make sure you properly escape more complex values!