I use Pennytel as one of my VoIP Service Providers. Recently they started offering a SOAP API to query their data.
Being a lover of knowing how much credit I have, I decided to knock up this little AGI script. I’ve got it set up to tell me my credits before every outgoing call.
I’d love for Pennytel to offer an XML version of their call rates with Dialing Prefix’s listed as I’d love to be able to teach asterisk to tell me how many minutes I can spend on a call.
Anyway, without further delay – here’s the code
#!/usr/bin/perl
#****************************************************
#* AGIPennytelCredit.pl Version 1.0 *****************
#****************************************************
#* Writen by Freman aka Fremean *
#****************************************************
#* Pay a vist to http://fremnet.net/contact if you *
#* Feel the burning desire to leave comments, *
#* compliments, or complaints *
#****************************************************
#* Place this file in your AGI-BIN directory which *
#* for me is /var/lib/asterisk/agi-bin *
#* Add a call to it: agi(AGIPennytelCredit.pl) in *
#* your extensions conf/ael *
#****************************************************
use Asterisk::AGI;
use strict;
use vars qw($AGI);
use SOAP::Lite;
my %Config = (
# Your Pennytel phone number
'PennytelVoIPID' => '0987654321',
# The password for your SIP account - NOT the web interface password
'PennytelVoIPPassword' => 'password',
# You can download this file from http://pennytel.com/pennytelapi/services/PennyTelAPI?WSDL
'PathToPennytelWSDL' => 'file:/var/lib/asterisk/agi-bin/Pennytel.wsdl',
# I like to know the exact figure, but others might not care once it gets to parts of cents
'SayPoints' => 1
);
#****************************************************
# Nothing configurable below this line...
#****************************************************
# Talk to asterisk
$AGI = new Asterisk::AGI;
my %input = $AGI->ReadParse();
# Talk to pennytel
my $Answer = SOAP::Lite
->service($Config{PathToPennytelWSDL})
->getAccount($Config{PennytelVoIPID},$Config{PennytelVoIPPassword});
# Make it presentable
my ($Dollars,$Cents) = split(/\./,$Answer->{balance});
$Cents = $Cents / 1000;
my ($Cents,$Points) = split(/\./,$Cents);
# Tell the user
StreamFiles('your','account-balance-is');
$AGI->say_number($Dollars);
StreamFiles('dollar'.(($Dollars == 1)?'':'s'),('and'));
$AGI->say_number($Cents);
if ($Config{SayPoints}) {
StreamFiles('point');
$AGI->say_digits($Points);
}
StreamFiles('cents');
sub StreamFiles {
foreach (@_) {
$AGI->stream_file($_);
}
}