Fax reporting via custom context

I needed to add reporting capability to the outgoing faxes and found the great starting point on these forums, thanks to @kjohnsoncda, original writeup is here:

I was able to improve on the concept by splitting the dial process into two separate contexts to avoid double reporting and sharing the code to give back to the community.

Example call file:

Channel: local/[email protected]
Callerid: "FAX" <+15551231234>
Context: stagetwo
Extension: sendfax
Priority: 1
# Reporter can be an executable to a script
Set: REPORTER=/var/path/report.pl
#Set: REPORTER=php /var/path/code.php
Set: ATTEMPT=1
Set: FAXFILE=/tmp/tmp.Y0N7RSh0SC_fax/pdf_61d9c9a137cf5.tif
Set: [email protected]
Set: FULLPATH=/mnt/data/faxes/outbox/admin/5553214321.pdf
Set: FAXNUM=5553214321
Set: Station=FAX 5551231234

Context in extensions_custom.conf:
Note that you’ll want to customize it with your channel type (SIP,PJSIP in my case, etc) and outgoing trunk name in the stage one section, you could set the whole connection string from the call file, but I prefer to keep the call file as simple as possible, while hardcoding location unique data into the context.

[stageone]
exten => start,1,NoOp(Incomming Request)
; Dial command will connect us to stagetwo context if the call is answered
same => n,Dial(PJSIP/${FAXNUM}@trunkname)
; If we find ourselves here, the call has failed for some reason, report it as such.
same => n,NoOp(NOTICE, Dial Result: ${DIALSTATUS})
same => n,system(${REPORTER} --file "${FAXFILE}" --attempt "${ATTEMPT}" --to "${EMAILADDR}" --status "FAILED" --statusstr "Connection Failed" --error "-1" --dial "${DIALSTATUS}" --end "${HANGUPCAUSE}" --faxnum "${FAXNUM}" --pages "0" --remote "")

[stagetwo]
exten => sendfax,1,NoOp(Sending a fax)
same => n,Set(FAXOPT(ecm)=yes)
same => n,Set(FAXOPT(localstationid)=${Station})
same => n,Set(FAXOPT(maxrate)=14400)
same => n,Set(FAXOPT(minrate)=9600)
same => n,SendFAX(${FAXFILE},d,z)

; When sendfax extension completes, call will automatically hang-up, report it during the hang-up process
exten => h,1,NoOp(Report fax to ${REPORTER})
same => n,system(${REPORTER} --file "${FAXFILE}" --attempt "${ATTEMPT}" --to "${EMAILADDR}" --status "${FAXOPT(status)}" --statusstr "${FAXOPT(statusstr)}" --error "${FAXOPT(error)}" --dial "ANSWER" --end "${HANGUPCAUSE}" --faxnum "${FAXNUM}" --pages "${FAXOPT(pages)}" --remote "${FAXOPT(remotestationid)}")

The path to the report code / command is set within the call file. Fax variables from stagetwo will not be available to stageone report, so they are hardcoded into command to simplify the reporting code. I keep track of the attempt number in the call file and retry from the report code, depending on the situation. For example if it’s a first try and dial comes back BUSY, I’ll create a new call file with a future modify date some random time in next 5 minutes. If we kept getting a busy signal for an hour, job will send an email report and halt until the user determines what is wrong.

To Do:
I’m still not clear on how to set the outgoing caller ID from the stageone context, before dialing the outside trunk, this would simplify the call file.
I’d love to pass the variable between stageone and stagetwo, this would again simply the call file and allow me to hardcode the localstationid within the context of stageone, rather then having to set it within the call file.

If you have any questions, or can suggest improvements, please let me know. Thanks and keep on coding :wink:

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.