📜 ⬆️ ⬇️

Create a web form to send faxes through Asterisk

Being engaged in the development and operation of a VoIP network in one of the providers inevitably has to face many problems. Probably, one of the most unpleasant and time-consuming problems has been and remains the problems associated with faxes, which often arise when connecting to a new operator or unexpectedly surface with existing ones.

To somehow automate the process of checking the passage of faxes for our technical support implemented a web interface that allows you to send a fax to the desired operator.
The article assumes that there is an installed Asterisk with the working Fax for Asterisk module. Asterisk will make calls to some kind of softswitch, which in turn will send a call to the desired operator.

How does the sending logic work?
A prerequisite is the presence of a receiving fax machine operating in automatic mode, otherwise Asterisk will not be able to detect a fax machine on the receiving side and initialize the sending session using the t38 protocol. Using the t.38 protocol allows you to get a high probability of passing faxes in ip networks.

So, our script will create a call file for Asterisk responsible for dialing the fax machine number. The script will also know which operator to make the call.
A situation may arise when the fax machine is located on a previously known internal number. In this case, we can force Asterisk to dial it up and then initiate sending a fax.
Html form to send has the form
')
The html form code is as follows

<title>   </title> <body> <h2>   </h2>    ? <form name="forma1" method=post action="/cgi-bin/callback-fax.pl"> <p> <SELECT NAME="operator"> <OPTION VALUE ="operator-1">operator-1 <OPTION VALUE ="operator-2">operator-2 <OPTION VALUE ="operator-3">operator-3 <OPTION VALUE ="operator-4">operator-4 <OPTION VALUE ="operator-5">operator-5 <OPTION VALUE ="operator-6">operator-6 </SELECT> <p>     ? <INPUT NAME="number" VALUE ="" SIZE=15 MAXLENGTH=15> <p><b>      ?</b><INPUT TYPE="CHECKBOX" NAME="direct_dial" CHECKED="1" VALUE ="1"> <br> <INPUT NAME="direct_dial_timeout" VALUE ="2" SIZE=2 MAXLENGTH=2> <p>    ,     (  ) <INPUT NAME="disa_number" VALUE ="" SIZE=4 MAXLENGTH=4>        <INPUT NAME="disa_timeout" VALUE ="" SIZE=2 MAXLENGTH=2> <br>     ? <INPUT NAME="mailbox" VALUE ="" SIZE=20 MAXLENGTH=50> <p><input type="submit" name="submit" value=" !"> </form> </body> 

When activated from the fax sending form, the callback-fax.pl script is initialized, which parses the parameters passed to it, creates a call file (callfile)
The important thing here is the substitution of the desired prefix to the number depending on the operator selected in the html form.
Depending on whether you need to dial an fax machine or not, the script calls the necessary logic from the Asterisk dialplan (Extension 100 or Extension 200)

  #!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); my $operator = param('operator'); my $original_number = param('number'); my $send_fax_now = param('direct_dial'); my $send_fax_now_timeout = param('direct_dial_timeout'); my $send_fax_disa_number = param('disa_number'); my $send_fax_disa_timeout = param('disa_number'); my $mailbox = param('mailbox'); my %operator_prefix = ( operator-1 => '01', operator-2 => '02', operator-3 => '03', operator-4 => '04', operator-5 => '05', operator-6 => '06' ); my $number = $operator_prefix{$operator} . $original_number; print "Content-type: text/html\n\n"; print "Sending fax... you will be e-mailed at <b>$mailbox</b><br>\n"; if ($send_fax_now == 1){ open (F, ">/var/spool/asterisk/outgoing/$number"); print F "Channel: SIP/"; print F "$number"; print F "\@softswitch\n"; print F "Callerid: 7495XXXXXXX\n"; print F "Context: send_fax\n"; print F "Extension: 100\n"; print F "MaxRetries: 1\n"; print F "Retrytime: 5\n"; print F "WaitTime: 60\n"; print F "Priority: 1\n"; print F "SetVar: operator=$operator\n"; print F "SetVar: original_number=$original_number\n"; print F "SetVar: email=$mailbox\n"; print F "SetVar: send_fax_now_timeout=$send_fax_now_timeout"; close F; } else { open (F, ">/var/spool/asterisk/outgoing/$number"); print F "Channel: SIP/"; print F "$number"; print F "\@softswitch\n"; print F "Callerid: 7495XXXXXXX\n"; print F "Context: send_fax\n"; print F "Extension: 200\n"; print F "MaxRetries: 1\n"; print F "Retrytime: 5\n"; print F "WaitTime: 60\n"; print F "Priority: 1\n"; print F "SetVar: operator=$operator\n"; print F "SetVar: original_number=$original_number\n"; print F "SetVar: email=$mailbox\n"; print F "SetVar: send_fax_disa_number=$send_fax_disa_number\n"; print F "SetVar: send_fax_disa_timeout=$send_fax_disa_timeout"; close F; } open (LOGFILE, ">>/var/log/faxsend.log"); my $date = localtime; print LOGFILE "$date:$original_number:$operator\n"; close LOGFILE; 

Now move to the Asterisk's configuration extensions.conf.
Here, oddly enough, everything is simple. On extension 100 we send a fax immediately, extension 200 sends a fax after dialing.
By Hangup we send a report to the mail about sending without forgetting to put yourself in a copy;)

  [send_fax] exten => 100,1,Wait(${send_fax_now_timeout}) exten => 100,n,NoOp(${TIFF_FILE}) exten => 100,n,SendFAX(/usr/dumps/test-fax.tiff,d) exten => 100,n,NoOp(${FAXSTATUS}) exten => 100,n,Hangup() exten => 200,1,Wait(${send_fax_disa_timeout}) exten => 200,n,SendDTMF(${send_fax_disa_number}) exten => 200,n,NoOp(${TIFF_FILE}) exten => 200,n,SendFAX(/usr/dumps/test-fax.tiff,d) exten => 200,n,NoOp(${FAXSTATUS}) exten => 200,n,Hangup() exten => h,1,NoOp(email is ${email}:operator is ${operator}:number is ${original_number}) exten => h,n,System(/usr/local/bin/sendEmail -f fax@domain.com -t ${email} -bcc makarov@domain.com -u 'Fax message Sent' -m 'operator:${operator}\nnumber:${original_number}\nStatus:${FAXSTATUS}' -s mx.domain.com -l /var/log/fax.log) 

Optionally, you can also add a form to upload any image and send it to the script, but you need to remember to bring it to a tiff with which Asterisk SendFax () can work.
For example, tiffinfo in my case says:
  [root@PBX-CALLBACK dumps]# tiffinfo test-fax.tiff TIFF Directory at offset 0x8 (8) Subfile Type: multi-page document (2 = 0x2) Image Width: 1728 Image Length: 1172 Resolution: 204, 98 pixels/inch Bits/Sample: 1 Compression Scheme: CCITT Group 4 Photometric Interpretation: min-is-white FillOrder: msb-to-lsb Orientation: row 0 top, col 0 lhs Samples/Pixel: 1 Rows/Strip: 1172 Planar Configuration: single image plane Page Number: 0-0 Software: GPL Ghostscript 8.70 DateTime: 2011:12:23 16:04:13 Group 4 Options: (0 = 0x0) 

I hope in the following articles to highlight some more interesting aspects of Asterisk and VoIP telephony in general

Source: https://habr.com/ru/post/144409/


All Articles