From 4d91f995781b6096cc61cf0e4a9e54f838672222 Mon Sep 17 00:00:00 2001 From: Lorna Jane Mitchell Date: Thu, 6 Aug 2020 09:52:27 +0100 Subject: [PATCH] Add working demo example with whatsapp and webhooks --- .env.example | 4 ++ app/Http/Middleware/VerifyCsrfToken.php | 3 +- routes/web.php | 57 ++++++++++++++++++++----- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index ac74863..8548cd0 100644 --- a/.env.example +++ b/.env.example @@ -44,3 +44,7 @@ PUSHER_APP_CLUSTER=mt1 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" + +NEXMO_API_KEY= +NEMXO_API_SECRET= +NEXMO_NUMBER= diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 0c13b85..362c386 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -12,6 +12,7 @@ class VerifyCsrfToken extends Middleware * @var array */ protected $except = [ - // + '/webhooks/status', + '/webhooks/inbound' ]; } diff --git a/routes/web.php b/routes/web.php index 2e338f9..51d1925 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,22 +24,59 @@ }); Route::post('/message', function(Request $request) { - Log::Info(env('NEXMO_API_KEY')); - - $url = "https://rest.nexmo.com/sms/json"; - // $url = "https://ljnexmo.eu.ngrok.io"; - $params = ["api_key" => env('NEXMO_API_KEY'), - "api_secret" => env('NEXMO_API_SECRET'), - "from" => env('NEXMO_NUMBER'), - "to" => $request->input('number'), - "text" => "Hello from Vonage and Laravel :)" + // TODO: validate incoming params first! + + $url = "https://messages-sandbox.nexmo.com/v0.1/messages"; + $params = ["to" => ["type" => "whatsapp", "number" => $request->input('number')], + "from" => ["type" => "whatsapp", "number" => "14157386170"], + "message" => [ + "content" => [ + "type" => "text", + "text" => "Hello from Vonage and Laravel :) Please reply to this message with a number between 1 and 100" + ] + ] ]; + $headers = ["Authorization" => "Basic " . base64_encode(env('NEXMO_API_KEY') . ":" . env('NEXMO_API_SECRET'))]; $client = new \GuzzleHttp\Client(); - $response = $client->request('POST', $url, ['query' => $params]); + $response = $client->request('POST', $url, ["headers" => $headers, "json" => $params]); $data = $response->getBody(); Log::Info($data); return view('thanks'); }); +Route::post('/webhooks/status', function(Request $request) { + $data = $request->all(); + Log::Info($data); +}); + +Route::post('/webhooks/inbound', function(Request $request) { + $data = $request->all(); + + $text = $data['message']['content']['text']; + $number = intval($text); + Log::Info($number); + if($number > 0) { + $random = rand(1, 8); + Log::Info($random); + $respond_number = $number * $random; + Log::Info($respond_number); + $url = "https://messages-sandbox.nexmo.com/v0.1/messages"; + $params = ["to" => ["type" => "whatsapp", "number" => $data['from']['number']], + "from" => ["type" => "whatsapp", "number" => "14157386170"], + "message" => [ + "content" => [ + "type" => "text", + "text" => "The answer is " . $respond_number . ", we multiplied by " . $random . "." + ] + ] + ]; + $headers = ["Authorization" => "Basic " . base64_encode(env('NEXMO_API_KEY') . ":" . env('NEXMO_API_SECRET'))]; + + $client = new \GuzzleHttp\Client(); + $response = $client->request('POST', $url, ["headers" => $headers, "json" => $params]); + $data = $response->getBody(); + } + Log::Info($data); +});