How to know the type of notification received? #92
Replies: 3 comments 3 replies
-
As far as I know there isn't, but you can use instanceof to determine the type, as follows public function __invoke(array $data): void
{
$event = $this->webhook->read($data);
Storage::disk('public')->put('events.log', print_r($event, true));
//Contact Type Notification
if ($event instanceof Contact) {
Log::info('Contacto: ', [$event]);
}
//Text Type Notification
if ($event instanceof Text) {
$exists = self::processEvents($event->id());
if ($exists->isEmpty()) {
self::saveMessage(
wamId: $event->id(),
name: $event->customer()->name(),
waId: $event->customer()->phoneNumber(),
type: 'text',
timestamp: $event->receivedAt(),
body: $event->message(),
caption: null,
data: null,
)->save();
}
}
//Media Type Notification
if ($event instanceof Media) {
$exists = self::processEvents($event->id());
if ($exists->isEmpty()) {
self::saveMessage(
wamId: $event->id(),
name: $event->customer()->name(),
waId: $event->customer()->phoneNumber(),
type: 'image',
timestamp: $event->receivedAt(),
body: self::compileMedia($event->imageId(), $event->mimeType()),
caption: $event->caption(),
data: null,
)->save();
}
}
//Location Type Notification
if ($event instanceof Location) {
Log::info('Location: ', [$event]);
}
//Reaction Type Notification
if ($event instanceof Reaction) {
Log::info('Reaction: ', [$event]);
}
//Button Type Notification
if ($event instanceof Button) {
Log::info('Button: ', [$event]);
}
//System Type Notification
if ($event instanceof System) {
Log::info('System: ', [$event]);
}
//Order Type Notification
if ($event instanceof Order) {
Log::info('Order: ', [$event]);
}
//Interactive Type Notification
if ($event instanceof Interactive) {
Log::info('Interactive: ', [$event]);
}
//Unknown Type Notification
if ($event instanceof Unknown) {
Log::info('Unknown: ', [$event]);
}
//StatusNotification Type Notification
if ($event instanceof StatusNotification) {
$status = $event->status(); // sent, delivered, read
$wam = Messages::query()->where('wam_id', $event->id())->first();
if (! empty($wam)) {
$wam->status = $status;
$wam->save();
self::emitWhatsAppWebhookEvent($wam, true);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
You could open a PR with the code to add the new feature 🙏 @daniel89fg |
Beta Was this translation helpful? Give feedback.
-
I implemented a simple approach to get this into the current library but do not know if it's the best: Added a variable and getters & setters to the MessageNotification class: private string $content_type;
protected function getContentType(): string
{
return $this->content_type;
}
protected function setContentType(string $content_type): void
{
$this->content_type = $content_type;
} Which is the one that all the other Notifications for messages (not status messages) inherit from. I decided to call it $content_type instead of $notification_type because notifications received from WhatsApp can be "messages" or "statuses". Then, on each of the messages types like the Text class, we add the type of the message and we pass it up to the parent $content_type variable: private string $content_type = 'text';
public function __construct(string $id, Support\Business $business, string $message, string $received_at_timestamp)
{
parent::__construct($id, $business, $received_at_timestamp);
$this->message = $message;
$this->setContentType($this->content_type);
} Same pattern with all the other messages types, like the Interactive: private string $content_type = 'interactive';
public function __construct(
string $id,
Support\Business $business,
string $item_id,
string $title,
string $description,
string $received_at_timestamp
) {
parent::__construct($id, $business, $received_at_timestamp);
$this->item_id = $item_id;
$this->title = $title;
$this->description = $description;
$this->setContentType($this->content_type);
} Then, after we do read($payload) and we are sure we got a MessageNotification (not a StatusNotification) we can just do $notification->getContentType() to get the type string back. What do you guys think? @aalbarca should I open a PR?. |
Beta Was this translation helpful? Give feedback.
-
Hello.
The WebHook class receives, reads, and returns a class based on the type of notification received. But how do we know the type it is?
In my controller I need to do a switch() based on the notification type. But I don't see a way to know if it is: text, sticker, image, reaction, audio, etc...
I only have to get the name of the class and see what its final extension is.
Is there a function that returns the type of the message?
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions