diff --git a/README.md b/README.md index da6a13c..9eb0368 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 +message('Hello, this is a test message') + ->to('34676010101'); + } +} +``` + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/composer.json b/composer.json index 105be2f..5417fb6 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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", diff --git a/src/WhatsAppChannel.php b/src/WhatsAppChannel.php index e18fdb1..0479434 100644 --- a/src/WhatsAppChannel.php +++ b/src/WhatsAppChannel.php @@ -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(), diff --git a/src/WhatsAppTemplate.php b/src/WhatsAppTemplate.php index e0e9b4d..d99b496 100644 --- a/src/WhatsAppTemplate.php +++ b/src/WhatsAppTemplate.php @@ -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; @@ -121,4 +126,9 @@ public function hasRecipient(): bool { return ! empty($this->to); } + + public function type(): string + { + return self::$type; + } } diff --git a/src/WhatsAppTextMessage.php b/src/WhatsAppTextMessage.php new file mode 100644 index 0000000..289a0f8 --- /dev/null +++ b/src/WhatsAppTextMessage.php @@ -0,0 +1,66 @@ +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; + } +} diff --git a/tests/WhatsAppTextMessageTest.php b/tests/WhatsAppTextMessageTest.php new file mode 100644 index 0000000..1d39388 --- /dev/null +++ b/tests/WhatsAppTextMessageTest.php @@ -0,0 +1,44 @@ +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()); + } +}