Guest User

Untitled

a guest
Apr 17th, 2018
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.09 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Data::Dumper;
  4. use AnyEvent::Impl::Perl;
  5. use AnyEvent;
  6. use Asterisk::AMI;
  7. use DBI;
  8. use Socket;
  9.  
  10. my $dbhost = inet_ntoa(10.13.105.15);
  11. my $user = "matt";
  12. my $pass = "";
  13. my $db = "autodial";
  14. my $datasource = "dbi:mysql:autodial\@localhost";
  15.  
  16. my $dbh = DBI->connect (
  17.         "dbi:mysql:database=$db:host=$dbhost",
  18.         "$user", "$pass"
  19.         )
  20.         or die $DBI::errstr;
  21.  
  22. #Create your connection
  23. my $astman = Asterisk::AMI->new(PeerAddr => '127.0.0.1',
  24.                                 PeerPort => '5038',
  25.                                 Username => 'autodial',
  26.                                 Secret => 'phone',
  27.                                 Events => 'on',
  28.                                 Handlers => { default => \&eventhandler, Hangup => \&hanguphandler }
  29.                         );
  30. #Alternatively you can set Blocking => 0, and set an on_error sub to catch connection errors
  31. die "Unable to connect to asterisk" unless ($astman);
  32.  
  33. my $max = 1;
  34. my $cv = AE::cv;
  35. my $current = 0;
  36. my %action =           (Action => 'Originate',
  37.                         Channel => 'GTALK/asterisk/+13124049672@voice.google.com',
  38.                         Context => 'local',
  39.                         Exten => 'test',
  40.                         Priority => 1,
  41.                         Async => 1,
  42.             Account => 123456678
  43. );
  44. #Define the subroutines for events
  45. sub eventhandler {
  46.     my ($ami, $event) = @_;
  47.     print 'Got Event: ',$event->{'Event'},"\r\n";
  48. }
  49.  
  50. sub hanguphandler {
  51.     my ($ami, $event) = @_;
  52.     print "Hanging up channel: $event->{'Channel'} - $event->{'Cause-txt'}\n";
  53.     $current--;
  54. }
  55.  
  56.  
  57. #Define a subroutine for your action callback
  58. sub actioncb {
  59.     my ($ami, $response) = @_;
  60.     print Dumper($response);
  61.     #print 'Got Action Reponse: ',$response->{'Response'},"\r\n";
  62.     #print 'Action: ',$response->{'PARSED'}->{'Ping'},"\r\n";
  63.     #print 'Time: ',$response->{'PARSED'}->{'Timestamp'},"\r\n";
  64.     #print "Current: $$count\n";
  65. #   if ($response->{'Response'} eq 'Success') {
  66. #       $current--;
  67. #       $cv->send;
  68. #   }
  69. }
  70.  
  71.  
  72. #Send an action
  73. sub sendaction {
  74.     while ( $current < $max ) {
  75.         my $originate = buildAction();
  76.         # send action to asterisk with the action callback &actioncb, timeout of 3 secs and passing a reference to $current.
  77.         my $action = $astman->send_action($$originate, \&actioncb, 3);
  78.         $current++;
  79.         print Dumper($action);
  80.         #print "Current count is $current\n";
  81.     }
  82. }
  83.  
  84. sub getNumber {
  85.     my $number = dbh->selectrow_hashref("SELECT number FROM dial_numbers WHERE completed is null LIMIT 1");
  86.     return $number;
  87. }
  88.  
  89. sub buildAction {
  90.     my $num = getNumber();
  91.     my %action = (Action => 'Originate',
  92.                         Channel => "GTALK/asterisk/+1$num"."voice.google.com",
  93.                         Context => 'local',
  94.                         Exten => 'test',
  95.                         Priority => 1,
  96.                         Async => 1,
  97.                         Account => 123456678
  98. )};
  99.  
  100. # create a timer that fires right away then every one second and calls &sendaction.
  101. my $timer = AE::timer 0, 1, \&sendaction;
  102.  
  103. #$cv->recv;
  104. AnyEvent::Impl::Perl::loop;
Add Comment
Please, Sign In to add comment