Telephone system:Betamax credit

From Hackerspace ACKspace
Revision as of 12:54, 23 September 2012 by Xopr (talk | contribs) (add credit script)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This collection of scripts will allow the user to hear the credit available on the Betamax services (VoipBuster, Intervoip, etc.), when dialing 1244 (the number for prepaid services as defined by Ministry of Economic affairs)

credit.php

Todo.. (I need to clean this)

This script will take the 'account' parameter to log in to the appropriate page, and returns the information in the following format:

[{ "currency": "€", "credit": "0.71", "freedays": "0" }]

Note that 'freedays' is only applicable to VoipBuster, and therefore optional

credit.js

This is the script that you have to customize yourself.

include("voipcredit.js");

if (session.ready())
{
    session.preAnswer();

    var voipCredit = new VoipCredit();

    // This will add the accounts which loads the php script
    voipCredit.setAccounts(
    {
	"voipbuster": VoipCredit.LANDLINE,
	"intervoip":  VoipCredit.CELL
    } );

    // will say "One moment, please"
    voipCredit.checkAll();

    // will say    "The account balance is.."
    // followed by "x euro/dollar and x cents (and x freedays, if applicable)", optionally with the account type description, see table
    // followed by "next", followed by the next account, if any
    voipCredit.speakAll();
};

You can use the following types in setAccounts():

type pronunciation
VoipCredit.UNKNOWN no pronunciation
VoipCredit.LANDLINE "at landline"
VoipCredit.CELL "at cell"
"abc" (string) "at A, Bee, Cee"
"ABC" (string in caps) "at alpha, bravo, charlie"
1 (unquoted) "at account number one"
"1st" (quoted) "at first account number"

voipcredit.js

This script is the VoipCredit class used to prepare the accounts, load the information and say the results This class needs the JSON parser and uses the fetchUrl (javascript curl) feature.

// include the minified JSON parser
include("json2_min.js");

function VoipCredit()
{
    // Constructor
    this.accounts = [];
    this.succeeded = false;
};

VoipCredit.UNKNOWN = false;
VoipCredit.LANDLINE = "landline";
VoipCredit.CELL = "cell";

VoipCredit.prototype.setAccounts = function( _accounts )
{
    // Clear array
    this.accounts.splice( 0, this.accounts.length );

    for ( var idx in _accounts )
	this.addAccount( idx, _accounts[idx] );
};

VoipCredit.prototype.addAccount = function( _provider, _type )
{
    // Add account
    this.accounts[this.accounts.length] =
    {
	"provider" : _provider,
	"uri" : "https://ackspace.nl/credit.php?field=json&account=" + _provider,
	"type" : _type,
	"loaded" : false,
	"credit" : {}
    };
};

VoipCredit.prototype.checkAll = function()
{
    // Say: One moment please..
    session.streamFile("ivr/ivr-one_moment_please.wav", "", null, "");

    this.succeeded = true;

    // Check all accounts
    for ( var idx = 0; idx < this.accounts.length; idx++ )
	this.succeeded |= this.check( this.accounts[idx] );
};

VoipCredit.prototype.check = function( _account )
{
    var credit = fetchUrl( _account.uri );
    if (credit === false)
    {
	console_log( 3, "could not fetch credit info for " + _account.provider );
	return false;
    }
    else
    {
	credit = JSON.parse( credit );

	// Make sure to get only the first entry of the credit info
	if ( credit.length )
	    credit = credit[0];

	_account.credit = credit;
	_account.loaded = true;
	return true;
    }
};

VoipCredit.prototype.speakAll = function()
{
    // Say all credit info available
    if ( !this.succeeded )
    {
	session.streamFile( "zrtp/zrtp-status_error.wav", "", null, "" );
	return false;
    }

    session.streamFile( "ivr/ivr-account_balance_is.wav", "", null, "" );

    for ( var idx = 0; idx < this.accounts.length; idx++ )
    {
	if ( !this.accounts[idx].loaded )
	    continue;

	if ( idx )
	    session.streamFile( "voicemail/vm-next.wav", "", null, "" );

	this.speak( this.accounts[idx] );
    }
};

VoipCredit.prototype.speak = function( _account )
{
    if ( !_account.hasOwnProperty( "credit" ) &&  !_account.credit.hasOwnProperty( "credit" ) )
	return;


    session.execute( "say", "en number pronounced " + parseInt( _account.credit.credit ) );

    switch ( _account.credit.currency.toLowerCase() )
    {
	case "€":
	case "eur":
	case "euro":
	case "€":
	    session.streamFile("custom/euro.wav", "", null, "");
	    break;

	case "$":
	case "usd":
	case "dollar":
	    session.streamFile("currency/dollar.wav", "", null, "");
	    break;
    }

    session.streamFile("currency/and.wav", "", null, "");
    session.execute( "say", "en number pronounced " + parseInt( (parseFloat( _account.credit.credit ) % 1) * 100 ) );

    session.streamFile("currency/cents.wav", "", null, "");

    if ( _account.credit.hasOwnProperty( "freedays" ) )
    {
	session.streamFile("currency/and.wav", "", null, "");
	session.execute( "say", "en number pronounced " + _account.credit.freedays );
	session.streamFile("custom/freedays.wav", "", null, "");
    }

    if ( _account.hasOwnProperty( "type" ) && _account.type !== false )
    {
	// for
	//session.streamFile( "digits/4.wav", "", null, "" );
	// at
	session.streamFile( "ascii/64.wav", "", null, "" );

	switch ( _account.type )
	{
	    case "landline":
	    case VoipCredit.LANDLINE:
		session.streamFile("custom/landline.wav", "", null, "");
		break;

	    case "cell":
	    case VoipCredit.CELL:
		session.streamFile( "custom/cell.wav", "", null, "" );
		break;

	    default:
		// Not a predefined type, pronounce it
		if ( (typeof _account.type) == "number" )
		{
		    session.streamFile( "ivr/ivr-account_number.wav", "", null, "" );
		    session.execute( "say", "en number pronounced " + _account.type );
		}
		else if ( parseInt( _account.type ))
		{
		    session.execute( "say", "en number counted " + parseInt( _account.type ));
		    session.streamFile( "ivr/ivr-account_number.wav", "", null, "" );
		}
		else if ( _account.type.toString().toUpperCase() === _account.type.toString() )
		    session.execute( "say", "en name_phonetic pronounced " + _account.type );
		else
		    session.execute( "say", "en name_spelled pronounced " + _account.type );

		break;
	}

    }

    session.sleep( 500 );
};

json2.js

This is a public domain JSON parser, created by Douglas Crockford You can find it here

Advised is to use the minified version (You can do this online here)