Skip to main content

  • Login | Forgot Password?
Ribbit Developer logo

Making Ribbit app development easier with command line php tools

Submitted by mike on Tue, 2010-07-13 17:18

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";
    
?>

  • Code
  • Code
  • open api
  • php
  • ribbit API
  • mike's blog
  • Login or register to post comments
  • Share/Save

Ribbit Photos

Ribbit's Webinar SeriesTeam Ribbit Takes the Livestrong ChallengeTeam Ribbit Livestrong UpdateNEW: Ribbit API PricingCloudforce: San Jose 2010Cloudforce: San Jose 2010

See more photos at Flickr

Ribbit Conversations

  • RT @allnick: New: This Week's 10 Most Explosive Facebook Applications http://www.allfacebook.com/most-explosive-applications-2010-08 — 1 week 2 days ago
  • Microsoft IE9 releasing on Sept 15, 2010. http://tcrn.ch/dAODGp <would love all who received MSFT box to donate crayons to a local school> — 2 weeks 6 days ago
  • <sarcasm>Oh how I love keyword spammers. </sarcasm> — 3 weeks 4 days ago
  • How Data Will Impact the Future of Healthcare (Infographic) http://bit.ly/clsGTA /via @marshallk — 3 weeks 6 days ago
  • @gwmack: Sure, happy to chat your ear off. Please send an email to kristie [at] ribbit.com to get something set up. — 4 weeks 12 hours ago
  •  
  • 1 of 102
  • ››
more

Follow Ribbit

 

            

            

           

Categories

  • .NET
  • AIR
  • Applications
  • Articles and Media Mentions
  • Challenge
  • Code
  • Community
  • Contest
  • Design
  • Events
  • Feature
  • Flash
  • Flash Toolkit
  • Flex
  • Flex SDK
  • Gallery
  • General
  • Inside Ribbit
  • Interviews
  • News
  • Newsletter
  • PHP
  • REST
  • Role Playing
  • Silverlight
  • Tips &amp; Tricks
  • Uncategorized
  • Wish List

Archives

  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • November 1999

 

               

               

  • Company
    • Corporate Site
    • About Us
    • Careers
    • Contact Us
    • LegalPrivacy
    • News
    • Media Kit
  • Products
    • Platform
    • Mobile
    • Salesforce
    • Oracle
  • Solutions
    • Digital Agencies
    • Carriers
    • Systems Integrators
    • Hosted Contact Centers
  • Community
    • Corporate Blog
    • Developer Blog
    • CRM Blog
    • Moble Blog
    • Idea Wall
    • Events Calendar
  • Support
    • Developer Help
    • Ribbit for Salesforce Help
    • Ribbit for Oracle Help
    • Ribbit Mobile Help
    • Feedback
    • Developer Forums
    • Ribbit Mobile Forum
  • Developers
    • Developer Center
    • Develop for Ribbit Mobile
    • Register
    • Ribbit Labs

© 2010 Ribbit Corporation. All Rights Reserved.