Making Ribbit app development easier with command line php tools
Like many other programmers I find myself in a Linux shell often.
Linux provides so many command line tools that at times, it's easier to accomplish something using those tools then using an application on your desktop. Recently, while working on a Ribbit application, I found myself in need of a Ribbit command line tool for questions like; "what was the purpose number for this account again?" or tasks like "I need to set my callBackUrl to a different server". Luckily, it's easy to accomplish this using php command line functionality and the Ribbit PHP SDK.
You can run any php script from the command line by using the php command like this: "php <scriptName>". Everything after the script name will be treated as arguments and passed into the php script through the $argv array, just like C/C++ command line programs. The number of arguments can be found in $argc, just keep in mind the script name is always the first argument, so $argc will be at least 1. From there, it is as simple as passing command line arguments into calls to the Ribbit API. For my first version, I added some basic calls I use often, along with a help function that displays available commands, as seen below:
# php ribbitCLI.php help
Ribbit PHP Command Line Script
Usage: php ribbitCLI.php [Command] [Args]
Commands:
help Shows this
call [<number1> | <number1> ... <numberX> ] Makes an N legged call with the list of numbers provided. Returns a call id
status <call id> Returns the status of a call
sms <phone number> <subject> <body> Sends an SMS to <phone number>
devices [all|phone|purpose] Lists the devices for this user
app Lists the application data
setcallback <url> Set the callBack url for your application
setautoanswer <device id> [true | false] Lists the application data
ver Print the Ribbit PHP SDK version
While this tool is useful for aiding development and testing, it can also be combined with other features of Linux, making it even more powerful. You can add a sms command at the end of a shell script, a n-legged call command to a cron job to kick off daily conference calls, and use pipe to pass the data in and out of Ribbit to other programs. There are still many more features to add, such as setting application and user settings from the command line, exposing the rest of the Ribbit API calls and more. For now, take a look at the code below and feel free to use/extend it.
<?php
error_reporting(E_ALL);
// Ribbit PHP Command Line Tool
// version 0.1
// Michael Meyers
// 03.31.10
require_once("ribbit/Ribbit.php");
class ribbitCLI
{
private $ribbit;
private $USER = "USERNAME";
private $PASS = "PASSWORD";
function __construct()
{
}
public function run($argv,$argc)
{
$this->ribbit = Ribbit::getInstance();
try{$this->ribbit->Login($this->USER,$this->PASS);}
catch(InvalidUserNameOrPasswordException $e){die("Login to ribbit failed \n");}
catch(RibbitException $e){die("Login to ribbit failed \n");}
if ($argc > 1)
$cmd = $argv[1];
else
$cmd = '';
switch ($cmd)
{
case "sms":
$this->checkArgs(3,$argc,$cmd);
$response = $this->sendSMS($argv[2],$argv[3],$argv[4]);
break;
case "call":
$this->checkArgs(1,$argc,$cmd);
$numbers = array();
for ($i = 2;$i < $argc;$i++)
$numbers[] = $argv[$i];
$response = $this->makeCall($numbers);
break;
case "status":
$this->checkArgs(1,$argc,$cmd);
$response = $this->getStatus($argv[2]);
break;
case "devices":
$this->checkArgs(1,$argc,$cmd);
$response = $this->devices($argv[2]);
break;
case "ver":
$response = Ribbit::VERSION;
break;
case "app":
$response = $this->showAppSettings();
break;
case "setcallback":
$this->checkArgs(1,$argc,$cmd);
$response = $this->setCallbackUrl($argv[2]);
break;
case "setautoanswer":
$this->checkArgs(2,$argc,$cmd);
$response = $this->setAutoAnswer($argv[2],$argv[3]);
break;
case "help":
default:
$response = $this->printHelp();
break;
}
print $response;
print "\n";
}
//Helper functions
private function checkArgs($num,$total,$cmd)//Make sure the correct number of args was passed
{
if ($num > ($total - 2))
{
print "Invalid number of arguments for call $cmd \n Use command 'help' for command detail.";
die("\n");
}
}
private function printHelp()
{
$str = "Ribbit PHP Command Line Script\n";
$str .= "Usage: php ".basename(__FILE__)." [Command] [Args]\n";
$str .= "Commands:\n";
$str .= "help Shows this\n";
$str .= "call [<number1> | <number1> ... <numberX> ] Makes an N legged call with the list of numbers provided. Returns a call id\n";
$str .= "status <call id> Returns the status of a call\n";
$str .= "sms <phone number> <subject> <body> Sends an SMS to <phone number>\n";
$str .= "devices [all|phone|purpose] Lists the devices for this user\n";
$str .= "app Lists the application data\n";
$str .= "setcallback <url> Set the callBack url for your application\n";
$str .= "setautoanswer <device id> [true | false] Lists the application data\n";
$str .= "ver Print the Ribbit PHP SDK version\n";
return $str;
}
private function sendSMS($phone,$subject,$body)
{
$ribbit = $this->ribbit;
$phone = "tel:" . $phone;
$recipients = array($phone);
$data = $ribbit->Messages()->createMessage($recipients,$body,null,$subject);
return $data;
}
private function makeCall($numbers)
{
$ribbit = $this->ribbit;
$legs = array();
for ($i = 0;$i < count($numbers);$i++)
{
$legs[] = "tel:" . $numbers[$i];
}
$callID = $ribbit->Calls()->createCall($legs);
return $callID;
}
private function getStatus($callid)
{
$ribbit = $this->ribbit;
$status = $ribbit->Calls()->getCall($callid);
$status = print_r($status,true);
return $status;
}
private function devices($filter)
{
$ribbit = $this->ribbit;
$devices = $ribbit->Devices()->getDevices();
if ($filter == "all")
$return = $devices;
else if ($filter == "phone")
{
for ($i = 0;$i < count($devices);$i++)
if ($devices[$i]['carrier'] == "PSTN")
$return[] = $devices[$i];
}
else if ($filter == "purpose")
{
for ($i = 0;$i < count($devices);$i++)
if ($devices[$i]['name'] == "purpose number")
$return[] = $devices[$i];
}
else
return $this->printHelp();
return print_r($return,true);
}
private function showAppSettings()
{
$ribbit = $this->ribbit;
$return = $ribbit->Applications()->getApplication();
return print_r($return,true);
}
private function setCallbackUrl($url)
{
$ribbit = $this->ribbit;
$return = $ribbit->Applications()->updateApplication($url);
if ($return['notificationUrl'] == $url)
return '1';
else
return '0';
}
private function setAutoAnswer($device,$flag)
{
if ($flag == "true")
$flag = true;
else
$flag = false;
$ribbit = $this->ribbit;
$return = $ribbit->Devices()->setAutoAnswer($device,$flag);
return print_r($return,true);
}
}
//Command line entry point to ribbitCLI tool
if (array_key_exists('SHELL', $_ENV))
{
$cli = new ribbitCLI();
$cli->run($argv,$argc);
}
else
print "This should only be run from the command line";
?>
- mike's blog
- Login or register to post comments
-






