Skip to content

Instantly share code, notes, and snippets.

Last active Sep 30, 2016
What would you like to do?

LaraconUS 'Advice' Demo

If you want to troll your friends with this, here you go. Caution, if used in a live demo, may have unexpected results.

Super Importaint Note: The get() method used here is basically a prototype, so it certianly may change. /warning

App Setup

Add this to composer.json, to bring in both the Nexmo client and Laravel package as their currently in beta:

"nexmo/laravel": "dev-master as 1.0",
"nexmo/client": "dev-master as 1.0"

Add the Nexmo Service Provider to your app.php config:

'providers' => [

And to aliases if you want to use the facade:

'aliases' => [
    'Nexmo' => Nexmo\Laravel\Facade\Nexmo::class    

Remove the CSRF middleware in Kernal.php to allow the inbound POST webhook from Nexmo:

    'web' => [

And update routes.php with the blissfully simplistic code you find here.

Nexmo Setup

Create an account, if you don't have one already. And configure an inbound number to point to you app. Might need to use ngrok to accept outside requests.

Route::get('/', function () {
return view('welcome');
Route::get('/nexmo', function(\Nexmo\Client $nexmo){
$message = $nexmo->message()->send([
'to' => '14845551212',
'from' => '12405553322',
'text' => 'Hey, from the live stage!'
Log::info('sent message: ' . $message['message-id']);
Route::post('/nexmo', function(\Nexmo\Client $nexmo){
$message = \Nexmo\Message\InboundMessage::createFromGlobals();
Log::info('got text: ' . $message->getBody());
$reply =$nexmo->message()->send($message->createReply('Thanks!'));
Log::info('sent reply: ' . $reply['message-id']);
Route::get('/advice', function(\Nexmo\Client $nexmo){
$condition = [];
$advice = [];
$numbers = [];
$query = new \Nexmo\Message\Query(new DateTime('today'), '12405553322');
foreach ($nexmo->message()->get($query) as $message){
$parts = explode(',', $message->getBody());
if(count($parts) == 2){
$condition[] = $parts[0];
$advice[] = $parts[1];
$numbers[] = $message->getFrom();
foreach (array_unique($numbers) as $number){
$text = $condition[array_rand($condition)] . ',' . $advice[array_rand($advice)];
$message = new \Nexmo\Message\Text($number, '12405553322', $text);
Log::info('sent message: ' . $message->getMessageId());
return $condition[array_rand($condition)] . ',' . $advice[array_rand($advice)];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment