[asterisk-users] Web based call back

Tzafrir Cohen tzafrir.cohen at xorcom.com
Thu Jun 16 02:56:18 CDT 2011


On Thu, Jun 16, 2011 at 11:26:21AM +0800, asterisk asterisk wrote:
> Hi,
> 
> I am looking for a simple solution to do this.
> 
> I wish to have the user to enter their preferred method of connection i.e.
> for the cheapest solution to their desktop phone or mobile phone, then plan
> callfile based on the number that user provided and dial to the user.
> 
> Any suggestions?

But doing so *is* simple. See a simple example attached. It relies on an
assumption that the origination IP address authenticates a user and also
the user's location (specifically: the phone).

You would probably need your own schema for that. But the actual dialing
is very simple.

-- 
               Tzafrir Cohen
icq#16849755              jabber:tzafrir.cohen at xorcom.com
+972-50-7952406           mailto:tzafrir.cohen at xorcom.com
http://www.xorcom.com  iax:guest at local.xorcom.com/tzafrir
-------------- next part --------------
#!/usr/bin/perl -w -T

# A web-based dialer for Asterisk.
# Authenticates and sets originating channel by IP address.

# Written by Tzafrir Cohen <tzafrir.cohen at xorcom.com>
# Copyright (C) 2011, Xorcom
# This program is free software; you can redistribute and/or
# modify it under the same terms as Perl itself.

# Required manager.conf settings:
# [dialer-web]
# secret=top-secret
# deny=0.0.0.0/0.0.0.0
# permit=127.0.0.1/255.255.255.0
# read=
# write=call

### Apache rewrite rules:
# RewriteEngine on
# RewriteRule ^/dial$ /cgi-bin/dial.cgi [R]
# RewriteRule ^/dial/(.*) /cgi-bin/dial.cgi?dial_number=$1 [R]

use strict;

use CGI qw/:standard/;
use Asterisk::Manager;

$ENV{PATH} = "/bin:/usr/bin";

my $Style = <<EOT;
<!--
body { background-color: #eee; }
#main { background-color: white; }
h1 { color: blue; text-align: center }

.error {
	background-color: red;
}
-->
EOT

my %CallerMap = (
	'172.16.5.128' => 'DAHDI/33', 
	'172.16.5.181' => 'DAHDI/42',
	'172.16.5.122' => 'SIP/279',
	'172.16.5.139' => 'DAHDI/38',
	'172.16.5.121' => 'SIP/235',
	'172.16.5.39' => 'SIP/235',
);
my $Title = "Asterisk Dialer";
my $admin = "Tzafrir";

my @Phones = sort values(%CallerMap);

my $device;
if (exists $CallerMap{remote_host()}){
	$device = $CallerMap{remote_host()};
}

sub dial($$) {
	my $device = shift;
	my $number = shift;

	my $astman = new Asterisk::Manager;
	$astman->user('dialer-web');
	$astman->secret('top-secret');
	$astman->host('localhost');
	$astman->connect || 
		die "Could not connect to Asterisk manager interface: $!";
	my %response = $astman->sendcommand(
		Action => 'Originate',
		Channel => $device,
		Context => 'from-internal',
		Exten => $number,
		Priority => 1,
	);
	if ($response{Response} eq 'Success') {
		return 1;
	}
	# Print response for debugging
	print p({-class=>'error'}, "Originate from $device to $number failed");
	for (keys %response) {
		print p("[$_ => $response{$_}]");
	}
	return 0;
}

sub footer() {
	my $base_url = url(-base=>1)."/dial";
	print
		hr,
		p("The Xorcom Dialer at ", a({-href=>$base_url},$base_url), 
			". Also try ",
			a({-href=>"$base_url/*65"},$base_url."/".em("number")),
		),
		end_html;
}

print 
	header,
	start_html(-title=>$Title, -style=>{-code=>$Style}),
	h1($Title),
	;

my $number = param('dial_number');
if (defined $number) {
	# Basic sanitation: Do allow '#', '*', numbers and digits.
	# Nothing else.
	$number =~ s/[^[:alnum:]\*#]//g;
	my $rc = dial($device, $number);

	if ($rc) {
		print p("Dialed from [$device] to [$number]");
	} else {
		print p({-class>"error"},
			"Error dialing from [$device] to [$number]");
	}
}

if(! defined $device) {
	my $host = remote_host();
	print
		p("You are connected from [$host], and no phone is ",
			"assigned to it. Please contact $admin."),
		;
	footer();
	exit 0;
}

print 
	div({-id=>'main'},
		p("Call from [$device] to:"),
		start_form,
			p(textfield('dial_number'), submit("Dial")) ,
		end_form,
	),
	;
footer();


More information about the asterisk-users mailing list