diff --git a/app/Http/Controllers/CustomersController.php b/app/Http/Controllers/CustomersController.php index 6369cda..195202a 100644 --- a/app/Http/Controllers/CustomersController.php +++ b/app/Http/Controllers/CustomersController.php @@ -11,6 +11,7 @@ use App\Services\CustomerService; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Auth; +use Twilio\Rest\Client; use Carbon\Carbon; @@ -234,9 +235,10 @@ class CustomersController extends Controller $expired = Carbon::now(); $expired = new Carbon(); $expired = $expired->addHours(24); - - $nokp = $request->noic; - + + $nokp = $request->noic_otp; + $customer = Http::get(config('api.url'). '/api/customers/'.$nokp)->json(); + $recipient = '+6'.$customer[0]['telefon']; //pembinaan token function token($length) { $characters = array( @@ -268,7 +270,8 @@ class CustomersController extends Controller 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); - + $message = "Your TAC code is " . $random; + $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp'=>$random]); } }else{ @@ -279,7 +282,8 @@ class CustomersController extends Controller 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); - + $message = "Your TAC code is " . $random; + $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp'=>$random]); } } @@ -295,7 +299,8 @@ class CustomersController extends Controller 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); - + $message = "Your TAC code is " . $random; + $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp'=>$random]); } }else{ @@ -306,10 +311,34 @@ class CustomersController extends Controller 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); - + $message = "Your TAC code is " . $random; + $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp'=>$random]); } } } + public function listenToReplies(Request $request) + { + $from = $request->input('From'); + + $message = "Someone send you a message on whatsapp"; + + $this->sendWhatsAppMessage($message, $from); + + return; + } + + public function sendWhatsAppMessage(string $message, string $recipient) + { + $twilio_whatsapp_number = getenv('TWILIO_WHATSAPP_NUMBER'); + $account_sid = getenv("TWILIO_SID"); + $auth_token = getenv("TWILIO_AUTH_TOKEN"); + $from = "whatsapp:".$twilio_whatsapp_number; + $to = "whatsapp:".$recipient; + + $client = new Client($account_sid, $auth_token); + return $client->messages->create($to, array('from' => $from, 'body' => $message)); + } + } diff --git a/composer.json b/composer.json index b7aba5a..24bec57 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "pusher/pusher-php-server": "~4.0", "simplesoftwareio/simple-qrcode": "^4.1", "spatie/laravel-medialibrary": "^7.19", + "twilio/sdk": "^6.16", "yajra/laravel-datatables-oracle": "^9.11" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 421ea39..bb069c4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b79f8c1e1e1c9dc9c91b67320e3cf3ea", + "content-hash": "79aad5a75460ed20426a86ab992dab8e", "packages": [ { "name": "almasaeed2010/adminlte", @@ -5999,6 +5999,60 @@ }, "time": "2020-07-13T06:12:54+00:00" }, + { + "name": "twilio/sdk", + "version": "6.16.1", + "source": { + "type": "git", + "url": "https://github.com/twilio/twilio-php.git", + "reference": "28c5dd21e95a5fe5221965ec89863a485c625d43" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twilio/twilio-php/zipball/28c5dd21e95a5fe5221965ec89863a485c625d43", + "reference": "28c5dd21e95a5fe5221965ec89863a485c625d43", + "shasum": "" + }, + "require": { + "php": ">=7.1.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "^6.3 || ^7.0", + "phpunit/phpunit": ">=4.5", + "theseer/phpdox": "^0.12.0" + }, + "suggest": { + "guzzlehttp/guzzle": "An HTTP client to execute the API requests" + }, + "type": "library", + "autoload": { + "psr-4": { + "Twilio\\": "src/Twilio/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Twilio API Team", + "email": "api@twilio.com" + } + ], + "description": "A PHP wrapper for Twilio's API", + "homepage": "http://github.com/twilio/twilio-php", + "keywords": [ + "api", + "sms", + "twilio" + ], + "support": { + "issues": "https://github.com/twilio/twilio-php/issues", + "source": "https://github.com/twilio/twilio-php/tree/6.16.1" + }, + "time": "2021-01-27T20:57:17+00:00" + }, { "name": "vlucas/phpdotenv", "version": "v4.1.8", diff --git a/resources/views/customers/info.blade.php b/resources/views/customers/info.blade.php index 16809a2..d51700a 100644 --- a/resources/views/customers/info.blade.php +++ b/resources/views/customers/info.blade.php @@ -1085,7 +1085,7 @@ method:'get', url: "{{ route('customer.otp') }}", data:{ - 'noic' : noic + 'noic_otp' : noic }, dataType:'json', success: function(otp_token){ diff --git a/routes/api.php b/routes/api.php index 4744c2f..b4cc1cb 100644 --- a/routes/api.php +++ b/routes/api.php @@ -18,6 +18,8 @@ Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); +Route::post('/chat-bot', 'CustomerController@listenToReplies'); + Route::group(['prefix' => 'dashboard'], function () { Route::post('/login', 'Auth\LoginController@loginDashboard'); Route::post('/logout', 'Auth\LoginController@logoutDashboard');