Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kuns25's avatar

Need Help in Twilio Sms API Integration

Hii , Im using http://laravel-notification-channels.com/twilio/ package , for integration of twilio sms .

I am verifying user mobile number during registration . I did all the configuration given in the package , and luckily im getting sms as well , user is getting verified as well .

I want to change the flow of verification of user , currently im first saving user with mobile number in database , then sending sms .i want to verify mobile number before saving data in Database . in package its given we can we can send sms by using below function in notifiable model , but for request we dont use any model , how can i fix that ,i dont know how to send sms to input mobile no.,

// code to enter in notifiable user model
public function routeNotificationForTwilio()
{
    return '+1234567890';    // or we can say $this->mobile
}

Please Help me how can i pass my $request->mobile in my notification

The Code is pretty simple .

you can check it

My web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('verifyotp', function () {
    return view('verifyotp');
});


Route::post('/verifyotp','verifyotpcontroller@verify'); 
Route::post('/resendotp','verifyotpcontroller@resendotp');  
Auth::routes();

 

Route::group(['middleware'=>'verifyotp'],function(){
Route::get('/home', 'HomeController@index')->name('home');

});

my login controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }



    protected function attemptLogin(Request $request)
    {
        
     $result= $this->guard()->attempt(
        $this->credentials($request), $request->filled('remember')
    );
     if ($result && auth()->user()->verifyotp==0) { 
         auth()->user()->sendotp();      
     }

     return $result;
     
 }

 
}


my model

<?php

namespace App;

use App\Notifications\otpnotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','mobile','verifyotp'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

public function otpkey(){
        return "otp_for_{$this->id}";
    }


    public function Cachetheotp(){

        $otp = rand(10000,1000);
        Cache::put([$this->otpkey()=>$otp],now()->addSeconds(20));
        return $otp;


    }

    public function otp(){

        return Cache::get($this->otpkey());

    }


    public function sendotp(){
 
        $otp = $this->Cachetheotp();
    $this->notify(new otpnotification($otp));     
 
   }

   public function routeNotificationForTwilio()
{
    return $this->mobile;
}
 
}


my notification

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioSmsMessage;
use Illuminate\Notifications\Notification;


class otpnotification extends Notification
{
    use Queueable;

public $otp;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($otp)
    {
        $this->otp = $otp;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
     public function via($notifiable)
    {
        return [TwilioChannel::class];
    }

      public function toTwilio($notifiable)
    {
        return (new TwilioSmsMessage())
            ->content("Your Verification code is {$this->otp}");
         
    }


    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}


0 likes
2 replies
kuns25's avatar

@BOBBYBOUWMANN - Hii Really Thanks for replying to my question , Actually i have never integrated a third party api without any packages , can you please guide me or share any tutorials where i can learn how to integrate api by reading simply documentation , i tried finding on youtube , but most of them are about using packages .

Also there is one more twilio package which dose not required notification https://github.com/aloha/laravel-twilio

i also successfully integrated this package in laravel 5.7 , but my current project is in laravel 5.3.

in aloha readme its written

And make these objects resolvable from the IoC container:

Aloha\Twilio\Manager (aliased as twilio)
Aloha\Twilio\TwilioInterface (resolves a Twilio object, the default connection object created by the Manager).
 

i Can't understand this step for 5.3 .

Help!!

Thanks in advance

Please or to participate in this conversation.