SRFax Gateway
Preface
This is a basic installation of an encrypted secure delivery to the SRFax service for use with Oscar.
This script will allow you to send faxes from Oscar using SRFax. If you want to use a different fax provider (or cheaper!) such as RingCentral, contact us and we can write a custom script for you that will integrate with their API to send the faxes. There is a script available for RingCentral fax sending.
Document Version History
- v1.0 – initial public release to oscarmanual.org with python – May 10, 2016
- v1.1 – amended for Ubuntu 18.04 – Sept 28, 2019
- v2.0 – greatly simplified curl based– Feb 27, 2020
- v2.1 – script generalised – Mar 1, 2020
- v2.2 – corrected crontab line, added note about files in /tmp – Oct 16,2020
- v2.3 – script to match crontab; remove signature file – Jan 29,2022
- v2.4 – notes added for Tomcat 9 – Feb 4, 2022
- v2.5 – permissions fixed and direct cron script output to null – April 17, 2022
Copyright ©2016-2022 by Peter Hutten-Czapski MD under the Creative Commons Attribution-Share Alike 3.0 Unported License. Updated by Adrian Starzynski.
Preamble
OSCAR drops files to a directory identified in oscar.properties as fax_file_location= . Failing that it uses the system property for java io tmp directory which usually resolves to the tmp directory to enable faxing. The three types are a text file with the fax number(s), the corresponding pdf and an optional signature.jpg. The .txt filename is one of the following patterns
- Prescription:
prescription_<prescription_provider_no><serial>.txt
- eForm:
EForm-<eform_data.fdid>.<serial>.txt
- Consult request form:
CRF-<consultationRequests.requestId>.0.<serial>.txt
An Internet Fax Gateway is a commercial subscription service that allows for conversion of email to fax and vice versa, fax to email. The main advantage of this service over using a method to fax locally is that the phone line and the modem are provided. The costs are the need for internet connectivity and the gateway subscription cost.
Apply for an account at SRfax and then with those particulars proceed
The Bash Script
Use your favorite text editor and load the following into /usr/share/oscar-emr/srfax2.sh
#!/bin/bash
#
# SRFax Gateway cron
# (c) 2022 Peter Hutten-Czapski released on GPL 2+
# version date Feb 4, 2022
#
# Picks up the files dropped by OSCAR
# and POSTs it to the SRFax API
# and it clears the files dropped by OSCAR
# Adjust the constants at the top of the page to match actual
#
ID=***** # this is your SRfax account number
PWD=****** # Escape any special characters with non quoted \
CID=******** # this is your SRfax fax number, just numbers
EMAIL=******** # use the same email registered with SRfax for access to their web interface
FROM=******* # this one is whatever you like
FAXLOG=fax2.log
SCRIPT_FILE=$(basename "$0")
LOCKDIR=/tmp/${SCRIPT_FILE}.lock
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPT_PATH="${SCRIPT_DIR}/${SCRIPT_FILE}"
FAXLOG=${SCRIPT_DIR}/fax2.log
PROPFILE=$CATALINA_HOME/oscar.properties
echo "Calling ${SCRIPT_FILE} on ${SCRIPT_PATH} with logging on ${FAXLOG}"
# --- select the running Tomcat or the highest installed version
TOMCAT=$(ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $1 }')
if [ -z "$TOMCAT" ]; then
#Tomcat is not running, find the highest installed version
if [ -f /usr/share/tomcat9/bin/version.sh ] ; then
TOMCAT=tomcat9
else
if [ -f /usr/share/tomcat8/bin/version.sh ] ; then
TOMCAT=tomcat8
else
if [ -f /usr/share/tomcat7/bin/version.sh ] ; then
TOMCAT=tomcat7
fi
fi
fi
fi
TMP=$(grep fax_file_location $PROPFILE | sed -e "s/.*[=:][[:space:]]*//" -e "s/#.*//")
if [ -z "$TMP" ]; then
TMP=/tmp/${TOMCAT}-${TOMCAT}-tmp
fi
echo "Searching ${TMP} for faxes...."
if test -n "$(find ${TMP} -maxdepth 1 -name '*.txt' -print -quit)"; then
echo "Faxes found to be sent"
for f in `ls $TMP/*.txt`; do
faxto=`sed s"/ *//g" $f|tr -d "\n"`
# allow for a second or so for the pdf to be generated
sleep 4
bfile=`echo $f | sed s"/txt/pdf/"`
openssl base64 -e -A -in $bfile -out temp.b64
echo -n `date` "fax to 1$faxto " >> $FAXLOG
json=$(curl \
-F "action=Queue_Fax" \
-F "access_id=$ID" \
-F "access_pwd=$PWD" \
-F "sCallerID=$CID" \
-F "sSenderEmail=$EMAIL" \
-F "sFaxFromHeader=$FROM" \
-F "sFaxType=SINGLE" \
-F "sToFaxNumber=1$faxto" \
-F "sCPSubject=Oscar Fax" \
-F "sRetries=3" \
-F "sFileName_x=$bfile" \
-F "sFileContent_x=<temp.b64" \
https://www.srfax.com/SRF_SecWebSvc.php)
if [ -n "$json" ]; then
success=$(echo "${json}" | grep -Po "Success")
if [ -z "$success" ]; then
echo "#### ERROR: ${json} for ${f} " >> $FAXLOG
echo "exiting but renaming source file"
cp $f $f.fail
else
echo "${json} for" `basename ${f}` >> $FAXLOG
rm $bfile;
fi
else
echo "### ERROR: \$json is empty for ${f} " >> $FAXLOG
cp $f $f.fail
fi
rm $f;
rm -f `echo $f | sed s"/prescription_/signature_/;s/.txt/.jpg/"`
rm temp.b64
done
else
echo "No faxes found"
fi
Of course you will need to alter the lines at the top of the script with your id, password, caller id, and account email.
Note: 1) You may need to add a ‘;’ before the ‘then’, depending on your version of bash. 2) The script deletes the .txt and .pdf files in the /tmp folder, but the signature.jpg will remain. These get deleted when the server is restarted.
Now make it executable
chmod 710
srfax2.sh
Now a cron job
And set it up as a cron job (you will need to run it as root or the tomcat user to open the files that are dropped by Oscar into the tmp directory.) The following example has the root user sending the files
sudo crontab -e
And add an entry like the following that executes every 2 minutes
*/2 * * * * /usr/share/oscar-emr/srfax2.sh
> /dev/null
Logs will be in the root home directory with one line per fax that should look something like:
Sat Feb 29 07:05:06 EST 2020 fax to <fax no> {"Status":"Success","Result":<SRfaxserial>} for prescription_<user><OSCAR serial>.txt