laravel-notification-channels / twilio

Twilio notifications channel for Laravel
https://laravel-notification-channels.com
225 stars 34 forks source link

SMS Notifications Are Not Working on Laravel 5.5 #42

Closed muhamadhhassan closed 5 years ago

muhamadhhassan commented 6 years ago

I am trying to use the release 2.0.6 with a laravel 5.5 app I get no exceptions but the SMS was not sent

service.php

'twilio' => [
    'auth_token' => env('TWILIO_TOKEN'),
    'account_sid' => env('TWILIO_SID'),
    'from' => env('TWILIO_NUMBER')
]

Notification Class

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioSmsMessage;

class ReservationMade extends Notification Implements
{
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * 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('bla');
    }
}

In User.php

public function routeNotificationForTwilio()
{
    return $this->phone;
}
lancemitchell commented 6 years ago

I struggled with this too. The issue is that this "Twilio notifications channel" under "vendor/laravel-notification-channels/twilio" uses the Twillio SDK under "vendor/twilio/sdk/Twilio/" for API call transport. The SDK is maintained by a separate team and unfortunately it does not throw Laravel exceptions.

Here is what I had to do to work around the issue:

Quick workaround: Edit the Rest Client Class to report to your storage/logs/laravel.log on the request / response to Twilio API endpoint.

Longer-Term workaround: Use composer to override the default Rest\Client Class with your customized class that does some conditional reporting based upon a configuration in your .env file. (1) Step 1. Create this folder and customized class in your Laravel App app\MyVendor\twilio\Rest\Client.php (2) Step 2. Update Composer.json to exclude this Class namespace "autoload": { "exclude-from-classmap": [ "vendor/twilio/sdk/Twilio/Rest/Client.php" ] },

(3) Step 3. Tell composer where to find your custom class using psr-4 "autoload": { "psr-4": { "App\": "app/", "Twilio\": "app/MyVendor/twilio/" }, }, (4) Step 4: Refresh composer composer dump-autoload (5) Step 5. Execute your code and observe logs for the call response from Twillio (6) Step 6. Grab a coffee

muhamadhhassan commented 6 years ago

@lancemitchell Thank you for your reply, the solution you suggested worked and I can get the request options now.

I found that the request is made to this endpoint https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json

which retrieves all the messages sent by my account!

jamesgraham commented 6 years ago

@lancemitchell which kind of exceptions are failing to capture?

You mention

it does not throw Laravel exceptions

but what would they be? Can we capture the Twilio Exception and re-throw them with Laravel Notification compatible ones?

setkyar commented 6 years ago

On laravel 5.6 as well. Does not work out sending the sms.

fwartner commented 5 years ago

Should be fixed.

9jadev commented 1 year ago

Doesn't work in laravel 9

diegocaprioli commented 1 year ago

I can confirm that is not working for me neither in Laravel 9.38.0 (with laravel-notification-channels/twilio 3.2.0 and twilio/sdk 6.43.1) . No exceptions, but the SMS is not sent. Nothing on my Twilio logs neither (as if it never reached Twilio).