diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..2c1fc0c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..4a7ff2b8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + +before_script: + - curl -s http://getcomposer.org/installer | php + - php composer.phar install --dev + +script: phpunit \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..f67096ed --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "travisjryan/twilio", + "description": "Twilio API for Laravel 4", + "type": "library", + "keywords": ["sms","ivr","laravel4","twilio"], + "authors": [ + { + "name": "Travis J Ryan", + "email": "travisjryan@gmail.com" + } + ], + "homepage":"https://github.com/travisjryan/laravel4-twilio", + "license": "MIT", + "require": { + "php": ">=5.3.0", + "illuminate/support": "4.0.x", + "twilio/sdk":"dev-master" + }, + "autoload": { + "psr-0": { + "Travisjryan\\Twilio": "src/" + } + }, + "minimum-stability": "dev" +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..12bee7aa --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/src/Travisjryan/Twilio/Commands/TwilioCallCommand.php b/src/Travisjryan/Twilio/Commands/TwilioCallCommand.php new file mode 100644 index 00000000..c51dccdc --- /dev/null +++ b/src/Travisjryan/Twilio/Commands/TwilioCallCommand.php @@ -0,0 +1,82 @@ +line('Creating a call via Twilio to: '.$this->argument('phone')); + + // Grab options + $from = $this->option('from'); + $url = $this->option('url'); + + // Set a default URL if we havent specified one since is mandatory. + if(is_null($url)) { + $url = 'http://demo.twilio.com/docs/voice.xml'; + } + + \Twilio::call($this->argument('phone'), $url, array(), $from); + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return array( + array('phone', InputArgument::REQUIRED, 'The phone number that will receive a test message.'), + ); + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array( + array('url', null, InputOption::VALUE_OPTIONAL, 'Optional url that will be used to fetch xml for call.', null), + array('from', null, InputOption::VALUE_OPTIONAL, 'Optional from number that will be used.', null) + ); + } + +} diff --git a/src/Travisjryan/Twilio/Commands/TwilioCallCommand.php-e b/src/Travisjryan/Twilio/Commands/TwilioCallCommand.php-e new file mode 100644 index 00000000..2b79a3d6 --- /dev/null +++ b/src/Travisjryan/Twilio/Commands/TwilioCallCommand.php-e @@ -0,0 +1,82 @@ +line('Creating a call via Twilio to: '.$this->argument('phone')); + + // Grab options + $from = $this->option('from'); + $url = $this->option('url'); + + // Set a default URL if we havent specified one since is mandatory. + if(is_null($url)) { + $url = 'http://demo.twilio.com/docs/voice.xml'; + } + + \Twilio::call($this->argument('phone'), $url, array(), $from); + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return array( + array('phone', InputArgument::REQUIRED, 'The phone number that will receive a test message.'), + ); + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array( + array('url', null, InputOption::VALUE_OPTIONAL, 'Optional url that will be used to fetch xml for call.', null), + array('from', null, InputOption::VALUE_OPTIONAL, 'Optional from number that will be used.', null) + ); + } + +} \ No newline at end of file diff --git a/src/Travisjryan/Twilio/Commands/TwilioSmsCommand.php b/src/Travisjryan/Twilio/Commands/TwilioSmsCommand.php new file mode 100644 index 00000000..2378e5b1 --- /dev/null +++ b/src/Travisjryan/Twilio/Commands/TwilioSmsCommand.php @@ -0,0 +1,84 @@ +line('Sending SMS via Twilio to: '.$this->argument('phone')); + + // Grab the text option if specified + $text = $this->option('text'); + + // If we havent specified a message, setup a default one + if(is_null($text)) { + $text = "This is a test message sent from the artisan console"; + } + + $this->line($text); + + Twilio::message('18085551212', $text); + + + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return array( + array('phone', InputArgument::REQUIRED, 'The phone number that will receive a test message.'), + ); + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array( + array('text', null, InputOption::VALUE_OPTIONAL, 'Optional message that will be sent.', null) + ); + } + +} diff --git a/src/Travisjryan/Twilio/Commands/TwilioSmsCommand.php-e b/src/Travisjryan/Twilio/Commands/TwilioSmsCommand.php-e new file mode 100644 index 00000000..2e137226 --- /dev/null +++ b/src/Travisjryan/Twilio/Commands/TwilioSmsCommand.php-e @@ -0,0 +1,84 @@ +line('Sending SMS via Twilio to: '.$this->argument('phone')); + + // Grab the text option if specified + $text = $this->option('text'); + + // If we havent specified a message, setup a default one + if(is_null($text)) { + $text = "This is a test message sent from the artisan console"; + } + + $this->line($text); + + Twilio::message('18085551212', $text); + + + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return array( + array('phone', InputArgument::REQUIRED, 'The phone number that will receive a test message.'), + ); + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array( + array('text', null, InputOption::VALUE_OPTIONAL, 'Optional message that will be sent.', null) + ); + } + +} \ No newline at end of file diff --git a/src/Travisjryan/Twilio/Facades/Twilio.php b/src/Travisjryan/Twilio/Facades/Twilio.php new file mode 100644 index 00000000..84eb18bf --- /dev/null +++ b/src/Travisjryan/Twilio/Facades/Twilio.php @@ -0,0 +1,16 @@ +config = $config; + } + + public function message($to, $message, $from=null) { + $twilio = $this->getTwilio(); + // Send SMS via Twilio SDK + return $twilio->account->sms_messages->create( + is_null($from) ? $this->config['from'] : $from, + $to, + $message + ); + } + + public function call($to, $url, $options=array(), $from=null) { + $twilio = $this->getTwilio(); + // Create Call via Twilio SDK + return $twilio->account->calls->create( + is_null($from) ? $this->config['from'] : $from, + $to, + $url, + $options); + } + + private function getTwilio() + { + return new \Services_Twilio($this->config['sid'], $this->config['token']); + } +} diff --git a/src/Travisjryan/Twilio/Twilio.php-e b/src/Travisjryan/Twilio/Twilio.php-e new file mode 100644 index 00000000..f103867f --- /dev/null +++ b/src/Travisjryan/Twilio/Twilio.php-e @@ -0,0 +1,37 @@ +config = $config; + } + + public function message($to, $message, $from=null) { + $twilio = $this->getTwilio(); + // Send SMS via Twilio SDK + return $twilio->account->sms_messages->create( + is_null($from) ? $this->config['from'] : $from, + $to, + $message + ); + } + + public function call($to, $url, $options=array(), $from=null) { + $twilio = $this->getTwilio(); + // Create Call via Twilio SDK + return $twilio->account->calls->create( + is_null($from) ? $this->config['from'] : $from, + $to, + $url, + $options); + } + + private function getTwilio() + { + return new \Services_Twilio($this->config['sid'], $this->config['token']); + } +} \ No newline at end of file diff --git a/src/Travisjryan/Twilio/TwilioServiceProvider.php b/src/Travisjryan/Twilio/TwilioServiceProvider.php new file mode 100644 index 00000000..dc43cbc4 --- /dev/null +++ b/src/Travisjryan/Twilio/TwilioServiceProvider.php @@ -0,0 +1,61 @@ +package('travisjryan/twilio'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app['twilio'] = $this->app->share(function($app) + { + return new Twilio(\Config::get('twilio::twilio')); + }); + + // Register Twilio Test SMS Command + $this->app['twilio.sms'] = $this->app->share(function($app) { + return new Commands\TwilioSmsCommand(); + }); + + // Register Twilio Test Call Command + $this->app['twilio.call'] = $this->app->share(function($app) { + return new Commands\TwilioCallCommand(); + }); + + $this->commands( + 'twilio.sms', + 'twilio.call' + ); + + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('twilio'); + } + +} diff --git a/src/Travisjryan/Twilio/TwilioServiceProvider.php-e b/src/Travisjryan/Twilio/TwilioServiceProvider.php-e new file mode 100644 index 00000000..1be6e578 --- /dev/null +++ b/src/Travisjryan/Twilio/TwilioServiceProvider.php-e @@ -0,0 +1,61 @@ +package('travisjryan/twilio'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app['twilio'] = $this->app->share(function($app) + { + return new Twilio(\Config::get('twilio::twilio')); + }); + + // Register Twilio Test SMS Command + $this->app['twilio.sms'] = $this->app->share(function($app) { + return new Commands\TwilioSmsCommand(); + }); + + // Register Twilio Test Call Command + $this->app['twilio.call'] = $this->app->share(function($app) { + return new Commands\TwilioCallCommand(); + }); + + $this->commands( + 'twilio.sms', + 'twilio.call' + ); + + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('twilio'); + } + +} \ No newline at end of file diff --git a/src/config/twilio.php b/src/config/twilio.php new file mode 100644 index 00000000..a0210af8 --- /dev/null +++ b/src/config/twilio.php @@ -0,0 +1,18 @@ + "", + + /** + * token - Access token that can be found in your Twilio dashboard + */ + "token" => "", + + /** + * from - The Phone number registered with Twilio that your SMS & Calls will come from + */ + "from" => "" +); diff --git a/src/config/twilio.php-e b/src/config/twilio.php-e new file mode 100644 index 00000000..8748320f --- /dev/null +++ b/src/config/twilio.php-e @@ -0,0 +1,18 @@ + "", + + /** + * token - Access token that can be found in your Twilio dashboard + */ + "token" => "", + + /** + * from - The Phone number registered with Twilio that your SMS & Calls will come from + */ + "from" => "" +); \ No newline at end of file diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 00000000..e69de29b