Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Monday, April 18, 2011

Tropo ClicktoCall with Conference in PHP - Cloud Telephony



$calloptions['onAnswer'] = 'secondCallInitiator';
$calloptions['callerID'] = 'Calleridhere';
call('+1'.$numbertodial, $calloptions);

function secondCallInitiator($event)
{
$confid = $conferenceid;
$confoptions['mute'] = false;
$confoptions['playTones'] = true;
$confoptions['leaveprompt'] = 'beep';
$sleep(2);
Say('Please hold');
Conference($confid,$confoptions);
}


Pass two variables, numbertodail & conferenceid as POST parameters.
ClicktCall with Conference, That is so Simple to a Complex Infrastructure.

Tropo ClicktoCall PHP Script - Cloud Internet Telephony Made Easy !



$aftercalloptions['onAnswer'] = 'onAnswerCB';
call('+1'. agentnumber, $aftercalloptions);

function onAnswerCB($event)
{
    global $customernumber;
    Sleep(3);
    transfer('+1' . $customernumber);
}



Cool and Pretty for a Complex Need !

Initiate a call with the following URL
https://api.tropo.com/1.0/sessions?action=create&token=yourtoken&agentnumber=agentnum&customernumber=custnum

Ensoy !

Wednesday, June 24, 2009

Asterisk How to Capture Events and Handle it with PHP easily, works with all 1.2, 1.4 & 1.6

PHP makes the life easily when it comes to string parsing. Just enjoy to handle the events from asterisk.

#!/usr/bin/php -q

ob_implicit_flush(false);

$socket = fsockopen("hostname or ip","port", $errornum, $errorstr);
if(!$socket) {
print "Couldn't open socket. Error #" . $errornum . ": " . $errorstr;
} else {
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: username\r\n");
fputs($socket, "Secret: password\r\n\r\n");
fputs($socket, "Action: Events\r\n");
fputs($socket, "EventMask: on\r\n\r\n");

fgets($socket); // Ignore the welcome message

$change = false;
$agentid = NULL;
$agentchannel = NULL;
$login = NULL;

while(true)
{
while(!feof($socket) )
{

$readbuf = "";
$resp = fread($socket,8192);
$readbuf .= $resp;
$allevents = split("\r\n\r\n",$readbuf);
foreach($allevents as $event)
{
$eventdetails = split("\r\n",$event);
$event_assoc = "";
foreach($eventdetails as $value ) {
$namevalue = split(": ",$value);
$event_assoc[$namevalue[0]] = $namevalue[1];
}
switch( $event_assoc['Event'] )
{
case 'Link': // Calls getting bridged
break;

default:
if( !empty($event_assoc['Event']) )
print_r($event_assoc);
}
}
fputs($socket, "Action: Ping\r\n\r\n");
usleep(1000000);
}

}

}

?>

Tuesday, April 21, 2009

Asterisk 1.2 , 1.4 & 1.6 Mp3 Voicemail using lame

Google Code checkout:
svn co http://codeoftheday.googlecode.com/svn/trunk/21April2009

Uncomment the following line in voicemail.conf

externnotify=/var/lib/asterisk/agi-bin/sendvoicemail.php

#!/usr/bin/php

<?php
/*
Released under Public Domain. Feel Free to modify and use it.
*/
mb_language('uni');
mb_internal_encoding('UTF-8');
require("class.phpmailer.php");

@mysql_connect("localhost","sqlusername","sqlpassword");
$dbselect = @mysql_select_db("asterisktable");

$voicemaildir = "/var/spool/asterisk/voicemail/" . $argv[1] . "/";

$sfilename = sprintf($voicemaildir .
"%s/INBOX/msg%04d.wav",$argv[2],$argv[3]-1);

$dfilename = sprintf($voicemaildir .
"%s/INBOX/msg%04d.mp3",$argv[2],$argv[3]-1);

$infofilename = sprintf($voicemaildir .
"%s/INBOX/msg%04d.txt",$argv[2],$argv[3]-1);

system("lame -V2 $sfilename $dfilename");



$stmt = sprintf("Select * from voicemail_users where mailbox='%s' and
context='%s'",$argv[2],$argv[1]);
$result = mysql_query($stmt);
if( mysql_num_rows($result) == 0 )
{
exit;
}

$row = mysql_fetch_assoc($result);
$rcptemail = $row['email'];
$rcptname = $row['fullname'];

$msginfo = parse_ini_file($infofilename);
$mail = new PHPMailer();
$mail->From = "noreply@voicetoemail.com";
$mail->FromName = "VoicetoEmail";
$mail->AddAddress($rcptemail, $rcptname);
$mail->AddReplyTo("noreply@voicetoemail.com", "VoicetoEmail");
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->AddAttachment($dfilename); // add attachments
$mail->IsHTML(true); // set email format to HTML

$mail->Subject = "Voicemail from " . $msginfo['callerid'];
$mail->Body = "Dear " . $rcptname . ",\n<br><br>Attached is the voice received
from " . $msginfo['callerid'] . "\n<br>Date & Time:" . $msginfo['origdate'] .
"\n\n<br><br>Support,<br>\nhttp://www.CalltoEmail.com";
$mail->AltBody = $mail->Body;
$mail->Send();

system("rm -rf $sfilename");
system("rm -rf $dfilename");
system("rm -rf $infofilename");

?>

Friday, August 3, 2007

Code to generate Unique Random Numbers - PHP

I was looking for a Unique Random Number with different length to use it for one of the calling card company. This PHP script just rocks. Enjoy, download the code from svn rather than copy and paste.

#!/usr/bin/php

function generateuniquenumbers($len,$count,$includezero=1)
{

if( strlen($count) > $len )
{
echo "Looks something funny in the parameter which you have supplied. \n";
return ;
}
echo "Generating " . $count . " numbers with length " . $len . "\n";
$mult = 0;
$fract = 0;
$numbers[] = "";
mt_srand(10);
if( $len > 9 )
{
$mult = (int)($len/9);
$fract = (int) $len % 9;
} else {
$fract = $len;
}
if( $fract > 0 )
{
$minfract = "";
$maxfract = "";
for($m=0; $m<$fract; $m++)
{
if($includezero)
$minfract = "0";
else
$minfract .= "1";
$maxfract .= "9";
}
}

for($numcount=0;$numcount<$count;$numcount++)
{
$curnumber = "";
for($i=0;$i<$mult;$i++)
{
if( $includezero )
$currand = mt_rand(0,999999999);
else
$currand = mt_rand(111111111,999999999);
if( strlen($currand) <>
{
$templen = strlen($currand);
for( $pad = 9 - $templen; $pad > 0; $pad--)
$curnumber .= "0";
}
$curnumber .= $currand;
}
if( $fract > 0 )
{
$currand = mt_rand($minfract,$maxfract);
if( strlen($currand) <>
{
$templen = strlen($currand);
for( $pad=strlen($maxfract)-$templen;$pad > 0;$pad--)
$curnumber .= "0";
}
$curnumber .= $currand;
}
if( !in_array($curnumber,$numbers) )
{
$numbers[] = $curnumber;
}
else
$numcount --;
}
return $numbers;
}

$ncount = 20;
$nlength = 20;

$numbers = generateuniquenumbers($nlength,$ncount,0);

for($k=1;$k<=$ncount;$k++)
echo $numbers[$k] . "\n";

?>

Here is the output:

[root@kansdevhost devel]# ./kansrand.php
Generating 20 numbers with length 20
16181070355240094632
35133279664046683582
56683089348828603637
61797018627269033641
32242637617039241045
53775119541769589123
98009706424319286130
67830750255154478737
4554441961805820434
24613857566557562252
86485415760605734578
30890354240158234429
12549747923000041677
85786735955666425870
44706552566779852177
41866325246584913132
61537972143640201927
52078343311552209047
53657534152010051142
74283518264934633688

You can download using svn with the following command,

svn co http://codeoftheday.googlecode.com/svn/trunk/03Aug2007

Do Let me know if there are any improvements done, will keep updated.

Enjoy.