Skip to content

Commit 7c766d9

Browse files
committed
Some working send and receive SMS/voice
1 parent 658fe68 commit 7c766d9

File tree

8 files changed

+189
-3
lines changed

8 files changed

+189
-3
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Log;
7+
use Vonage\Client;
8+
use Vonage\Voice\Endpoint\Phone;
9+
use Vonage\Voice\NCCO\Action\Talk;
10+
use Vonage\Voice\NCCO\NCCO;
11+
use Vonage\Voice\OutboundCall;
12+
13+
class MakeVoiceCallWithNcco extends Command
14+
{
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'vonage:voice';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Make a voice call with an NCCO object';
28+
29+
/**
30+
* Create a new command instance.
31+
*
32+
* @return void
33+
*/
34+
public function __construct()
35+
{
36+
parent::__construct();
37+
}
38+
39+
/**
40+
* Execute the console command.
41+
*
42+
* @return int
43+
*/
44+
public function handle(Client $client)
45+
{
46+
$outboundCall = new OutboundCall(
47+
new Phone('14843331234'),
48+
new Phone('14843335555')
49+
);
50+
51+
$ncco = new NCCO();
52+
$ncco->addAction(new Talk('This is a text to speech call from Vonage'));
53+
54+
$outboundCall->setNCCO($ncco);
55+
56+
$response = $client->voice()->createOutboundCall($outboundCall);
57+
Log::info(var_dump($response));
58+
return 0;
59+
}
60+
}

app/Console/Commands/SendSMS.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Vonage\Client;
7+
use Vonage\SMS\Message\SMS;
8+
9+
class SendSMS extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'vonage:sms';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Send a test SMS';
24+
25+
/**
26+
* Create a new command instance.
27+
*
28+
* @return void
29+
*/
30+
public function __construct()
31+
{
32+
parent::__construct();
33+
}
34+
35+
/**
36+
* Execute the console command.
37+
*
38+
* @return int
39+
*/
40+
public function handle(Client $client)
41+
{
42+
$text = new SMS('0123445', '123345', 'Hello from Vonage!');
43+
$response = $client->sms()->send($text);
44+
var_dump($response);
45+
return 0;
46+
}
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Vonage\Client;
7+
8+
class IncomingCallController extends Controller
9+
{
10+
public function __invoke(Request $request, Client $client)
11+
{
12+
$ncco = [
13+
[
14+
'action' => 'talk',
15+
'text' => 'Thank you for calling Vonage!'
16+
]
17+
];
18+
19+
return response()->json($ncco);
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Log;
7+
use Vonage\SMS\Webhook\Factory;
8+
9+
class IncomingSmsController extends Controller
10+
{
11+
public function __invoke(Request $request)
12+
{
13+
Log::info('Hit the SMS inbound endpoint');
14+
$sms = Factory::createFromGlobals($request);
15+
Log::info(var_dump($sms));
16+
}
17+
}

app/Http/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Kernel extends HttpKernel
3535
\Illuminate\Session\Middleware\StartSession::class,
3636
// \Illuminate\Session\Middleware\AuthenticateSession::class,
3737
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
38-
\App\Http\Middleware\VerifyCsrfToken::class,
38+
// \App\Http\Middleware\VerifyCsrfToken::class,
3939
\Illuminate\Routing\Middleware\SubstituteBindings::class,
4040
],
4141

app/Providers/VonageProvider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Vonage\Client;
7+
8+
class VonageProvider extends ServiceProvider
9+
{
10+
/**
11+
* Register services.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Bootstrap services.
22+
*
23+
* @return void
24+
*/
25+
public function boot()
26+
{
27+
$this->app->bind(Client::class, function() {
28+
$basic = new Client\Credentials\Basic(config('vonage.apiKey'), config('vonage.apiSecret'));
29+
$keypair = new Client\Credentials\Keypair(
30+
file_get_contents(base_path() . '/' . config('vonage.apiKeyPath')),
31+
config('vonage.applicationId')
32+
);
33+
34+
return new Client(new Client\Credentials\Container($basic, $keypair));
35+
});
36+
}
37+
}

config/vonage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
return [
44
'apiKey' => env('API_KEY'),
5-
'apiSecret' => env('API_SECRET')
5+
'apiSecret' => env('API_SECRET'),
6+
'apiKeyPath' => env('API_KEY_PATH'),
7+
'applicationId' => env('APPLICATION_ID')
68
];

routes/web.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use App\Http\Controllers\IncomingCallController;
4+
use App\Http\Controllers\IncomingSmsController;
45
use Illuminate\Support\Facades\Route;
56

67
/*
@@ -18,4 +19,5 @@
1819
return view('welcome');
1920
});
2021

21-
Route::get('/incoming', IncomingCallController::class);
22+
Route::get('/incomingVoice', IncomingCallController::class);
23+
Route::post('/incomingSms', IncomingSmsController::class);

0 commit comments

Comments
 (0)