Twilio WhatsApp Integration Not Working
Hello folks, I am now trying to send WhatsApp notifications to the tenants when the transaction is created. I have followed as Twilio guides in this tutorial, https://www.twilio.com/blog/create-laravel-php-notification-channel-whatsapp-twilio but it returns 'null' when I try to fire the function as it described.
I have mentioned my codes below. Please check them and help me with this. Btw, this is the last function I have to implement in my current project.
App\Channels\WhatsAppChannel.php
<?php
namespace App\Channels;
use Twilio\Rest\Client;
use Illuminate\Notifications\Notification;
class WhatsAppChannel
{
public function send($notifiable, Notification $notification)
{
$message = $notification->toWhatsApp($notifiable);
$to = $notifiable->routeNotificationFor('WhatsApp');
$from = config('services.twilio.whatsapp_from');
$twilio = new Client(config('services.twilio.sid'), config('services.twilio.token'));
return $twilio->messages->create('whatsapp:' . $to, [
"from" => 'whatsapp:' . $from,
"body" => $message->content
]);
}
}
App\Channels\Messages\WhatsAppMessage.php
<?php
namespace App\Channels\Messages;
class WhatsAppMessage
{
public $content;
public function content($content)
{
$this->content = $content;
return $this;
}
}
App\Tenant.php
// Twilio
public function routeNotificationForWhatsApp() {
return $this->phone_no;
}
App\Notifications\InvoiceProcessed.php
<?php
namespace App\Notifications;
use App\Models\Transaction;
use Illuminate\Bus\Queueable;
use App\Channels\WhatsAppChannel;
use App\Channels\Messages\WhatsAppMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class InvoiceProcessed extends Notification
{
use Queueable;
public $transaction;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Transaction $transaction)
{
$this->transaction = $transaction;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [WhatsAppChannel::class];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toWhatsApp($notifiable)
{
// $orderUrl = url("/orders/{$this->order->id}");
// $company = 'Acme';
// $deliveryDate = $this->order->created_at->addDays(4)->toFormattedDateString();
$tenant_name = $this->transaction->tenant->name;
$start_date = $this->transaction->start_date;
$end_date = $this->transaction->end_date;
$total_price = $this->transaction->total_price;
$content = $tenant_name . ' has rented bedspace(s) from ' . $start_date . ' to ' . $end_date . ' at the fees of ' . $total_price;
return (new WhatsAppMessage)->content($content);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
App\Http\Controllers\Operations\TransactionController.php (Store Method)
// There are codes for storing the transaction, updating the tenant with the transaction id, and other required methods here
$tenant->notify(new InvoiceProcessed($transaction));
Please or to participate in this conversation.