Skip to content

Added TextMessage class and Laravel 11 support #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Components supported by Whatsapp template sections:
- Body: currency, datetime and text
- Buttons: url and quick reply,

### Send a notification
### Send a notification from a template

To use this package, you need to create a notification class, like `MovieTicketPaid` from the example below, in your Laravel application. Make sure to check out [Laravel's documentation](https://laravel.com/docs/master/notifications) for this process.

Expand Down Expand Up @@ -140,6 +140,42 @@ class MovieTicketPaid extends Notification
}
```

### Send a text message

You can only send a text message after you've send a template and the user responded.

```php
<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\WhatsApp\Component;
use NotificationChannels\WhatsApp\WhatsAppChannel;
use NotificationChannels\WhatsApp\WhatsAppTextMessage;

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

public function toWhatsapp($notifiable)
{
return WhatsAppTextMessage::create()
->message('Hello, this is a test message')
->to('34676010101');
}
}
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Laravel notification driver for WhatsApp",
"homepage": "https://github.com/netflie/laravel-notification-whatsapp",
"license": "MIT",
"version": "1.2.0",
"keywords": [
"whatsapp notification",
"laravel",
Expand All @@ -19,10 +20,10 @@
}
],
"require": {
"php": ">=7.4",
"illuminate/notifications": "~8.0 || ~9.0 || ~10.0",
"illuminate/support": "~8.0 || ~9.0 || ~10.0",
"netflie/whatsapp-cloud-api": "^2.0.0"
"php": ">=7.4 || ^8.2",
"illuminate/notifications": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"netflie/whatsapp-cloud-api": "^2.2.0"
},
"require-dev": {
"mockery/mockery": "^1.0",
Expand Down
7 changes: 7 additions & 0 deletions src/WhatsAppChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function send($notifiable, Notification $notification): ?Response
}

try {
if ($message->type() === 'text') {
return $this->whatsapp->sendTextMessage(
$message->recipient(),
$message->getMessage()
);
}

return $this->whatsapp->sendTemplate(
$message->recipient(),
$message->configuredName(),
Expand Down
10 changes: 10 additions & 0 deletions src/WhatsAppTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class WhatsAppTemplate
*/
protected array $components;

/**
* The message type.
*/
protected static string $type = 'template';

protected function __construct($to = '', $name = '', $language = 'en_US')
{
$this->to = $to;
Expand Down Expand Up @@ -121,4 +126,9 @@ public function hasRecipient(): bool
{
return ! empty($this->to);
}

public function type(): string
{
return self::$type;
}
}
66 changes: 66 additions & 0 deletions src/WhatsAppTextMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace NotificationChannels\WhatsApp;

class WhatsAppTextMessage
{
/**
* WhatsApp ID or phone number for the person you want to send a message to.
*/
protected string $to;

/**
* Message.
*/
protected string $message;

/**
* The message type.
*/
protected static string $type = 'text';

protected function __construct($to = '', $message = '')
{
$this->to = $to;
$this->message = $message;
}

public static function create($to = '', $message = ''): self
{
return new self($to, $message);
}

public function to(string $to): self
{
$this->to = $to;

return $this;
}

public function message(string $message): self
{
$this->message = $message;

return $this;
}

public function getMessage(): string
{
return $this->message;
}

public function recipient(): ?string
{
return $this->to;
}

public function hasRecipient(): bool
{
return ! empty($this->to);
}

public function type(): string
{
return self::$type;
}
}
44 changes: 44 additions & 0 deletions tests/WhatsAppTextMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace NotificationChannels\WhatsApp\Test;

use NotificationChannels\WhatsApp\WhatsAppTextMessage;
use PHPUnit\Framework\TestCase;

final class WhatsAppTextMessageTest extends TestCase
{
/** @test */
public function the_notification_recipient_can_be_set()
{
$message = WhatsAppTextMessage::create()
->to('346762014584');

$this->assertEquals('346762014584', $message->recipient());
}

/** @test */
public function the_notification_message_can_be_set()
{
$message = WhatsAppTextMessage::create()
->message('Hello, World!');

$this->assertEquals('Hello, World!', $message->getMessage());
}

/** @test */
public function it_can_check_if_a_recipient_is_set()
{
$message = WhatsAppTextMessage::create()
->to('346762014584');

$this->assertTrue($message->hasRecipient());
}

/** @test */
public function it_can_return_the_message_type()
{
$message = WhatsAppTextMessage::create();

$this->assertEquals('text', $message->type());
}
}
Loading