Skip to content

Commit

Permalink
Add working demo example with whatsapp and webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
lornajane committed Aug 6, 2020
1 parent 3d3febc commit 4d91f99
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=

This comment has been minimized.

Copy link
@olamic

olamic Oct 31, 2020

dope

3 changes: 2 additions & 1 deletion app/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class VerifyCsrfToken extends Middleware
* @var array
*/
protected $except = [
//
'/webhooks/status',
'/webhooks/inbound'
];
}
57 changes: 47 additions & 10 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 4d91f99

Please sign in to comment.