Examples faxlib.jm

From FreeSWITCH Wiki

Jump to: navigation, search

These examples require http://www.0xdecafbad.com/files/faxlib.jm / Faxlib.jm to operate. faxlib.jm requires a working socket2me

This is the documentation for faxlib.jm an object based approach to faxing for FreeSWITCH with Javascript ( mod_spidermonkey). faxlib exposes certain methods and properties available to the calling program. They are documented here.

Contents

Properties

  • socketaddr - STRING host host:ip eg "localhost:8084"
  • session - SESSION OBJECT the current session
  • faxDetect - BOOLEAN state of fax CNG detection
  • faxFilePrefix - STRING directory prefix to the fax (for tx or rx)
  • faxFile - STRING the filename in faxFilePrefix of the fax
  • gotFax - BOOLEAN whether or not a fax was received

Methods

  • stopFaxDetect - stops fax CNG detection, called automatically when receiving a fax
  • startFaxDetect - starts fax CNG detection
  • getFaxFile - returns a STRING of the full path to the fax file
  • rxFax - receives a fax
  • txFax - transmits a fax
  • fax2pdf - converts a fax tiff into a pdf, the fax object will point to that pdf from then on - returns true if conversion was successful false otherwise

Constructor

  • fax(session,socketaddr [, faxFilePrefix [, faxFile]])

example:

  • var f = new fax(session,"localhost:8084","/tmp","myfax.tiff");

The last 2 arguments are optional and if not specified will be "/tmp" and the "<uuid>.tiff" where <uuid> is replaced by the current uuid of the call in progress.

Examples

Sending a fax

//  -*- mode:c; tab-width:4; c-basic-offset:4; c-indent-level:4; indent-tabs-mode:nil;  -*-
include("faxlib.jm");
session.answer();
session.execute("sleep","1000");

// create a new fax object
f = new fax(session,"127.0.0.1:8084");

// set the fax properties - these can also be specified when we create the fax object
f.faxFilePrefix = "/var/fax";
f.faxFile = "something.tiff";

// send a fax
f.txFax();

session.hangup();


Receiving a fax and emailing it off

//  -*- mode:c; tab-width:4; c-basic-offset:4; c-indent-level:4; indent-tabs-mode:nil;  -*-
use("etpan");
include("faxlib.jm");
function main_cb(session,type,data,args)
{
    switch(type) {
    case "event":
        fax = data.getHeader("fax");
        if(typeof fax != 'undefined' && fax == 'detected') {
            return 'fax';
        }
        break;
    case "dtmf":
        return data;
    }

    return false; // stop playback if any
}

session.answer();
session.execute("sleep","1000");

// create a new fax object
f = new fax(session,"127.0.0.1:8084");

// turn on fax CNG detection
f.startFaxDetect();

// stream a file that will use some callback
// this could be a voicemail app intro for example
dtmf = session.streamFile("/sounds/welcome.raw",main_cb,"");
if(dtmf == 'fax') {
     f.rxFax();
     // check if one was received
     if(f.gotFax == true) {
         var emailFrom    = "me@my.tld";
         var emailTo      = "you@your.tld";
         var emailHeaders = "Subject: New Fax from " + session.caller_id_num + "\n" +
                            "From: <" + emailFrom + ">\n" +
                            "To: <" + emailTo + ">\n";
         var emailBody    = "You received a fax, which is attached below\n";

         if(f.fax2pdf() == true) {
             email(emailFrom,emailTo,emailHeaders,emailBody,f.getFaxFile(),session.destination + "-" + session.caller_id_num + ".pdf");
         }
     }
}
Personal tools
Community