digihub

Laravel code

Here is Laravel sample code that sends an OTP and a notification using the Wosocial API (https://wosocial.com/api/send), you can follow these steps:

Set up a Laravel project if you haven't already:
 
composer create-project --prefer-dist laravel/laravel laravel-otp-notification
cd laravel-otp-notification 
Create a controller to handle OTP and notification sending:
 php artisan make:controller OtpNotificationController 
Open the OtpNotificationController.php file created in the app/Http/Controllers directory and add the following code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class OtpNotificationController extends Controller
{
    public function sendOtpAndNotification(Request $request)
    {
        // Replace these values with your actual credentials
        $instanceId = 'your_instance_id';
        $accessToken = 'your_access_token';
        
        // Generate a random OTP (you can use a more secure method)
        $otp = rand(1000, 9999);

        // Define the API endpoint and data to be sent
        $apiUrl = 'https://wosocial.com/api/send';
        $apiData = [
            'number' => $request->input('number'),
            'type' => 'text',
            'message' => "Your OTP is: $otp",
            'instance_id' => $instanceId,
            'access_token' => $accessToken,
        ];

        // Send the OTP and notification using the API
        $response = Http::post($apiUrl, $apiData);

        // Check if the API request was successful
        if ($response->successful()) {
            return response()->json(['message' => 'OTP and notification sent successfully']);
        } else {
            return response()->json(['error' => 'Failed to send OTP and notification'], 500);
        }
    }
}
Define a route in the routes/web.php file to invoke the sendOtpAndNotification method:
 
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\OtpNotificationController;

Route::post('/send-otp-notification', [OtpNotificationController::class, 'sendOtpAndNotification']);
Create a form or use an API client (e.g., Postman) to make a POST request to the /send-otp-notification route with the following parameters in the request body:
{
    "number": "1234567890", // Replace with the recipient's phone number
    "type": "text",
    "message": "Your OTP is: 1234", // Replace with the desired OTP message
}

Replace your_instance_id and your_access_token in the controller with your actual API credentials provided by the service.

This code will send an OTP and a notification to the specified phone number using the provided API. Make sure to handle error cases and implement appropriate error handling as per your project's requirements.