Skip to content

Instantly share code, notes, and snippets.

@tjlytle
Last active September 30, 2016 01:24
Show Gist options
  • Save tjlytle/61f8c9075f88977692ccbd5ce0fce8e5 to your computer and use it in GitHub Desktop.
Save tjlytle/61f8c9075f88977692ccbd5ce0fce8e5 to your computer and use it in GitHub Desktop.

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' => [
    Nexmo\Laravel\NexmoServiceProvider::class
]

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' => [
        //\App\Http\Middleware\VerifyCsrfToken::class,
    ],

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.

<?php
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);
$nexmo->message()->send($message);
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