Skip to content

Commit

Permalink
Support adding to dynamically (#17)
Browse files Browse the repository at this point in the history
* support setting recipient dynamically

* wip
  • Loading branch information
ossycodes authored May 6, 2022
1 parent aabbd75 commit a2d350f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ You can also modify who the notification(SMS) is sent from, this will overide th
->from("set any sender id/name here");
```
You can also modify who the notification(SMS) is sent to (the recipient)
``` php
return (new AfricasTalkingMessage())
->content('Your SMS message content')
->to("put the recipient phonenumber here"); //eg ->to(1111111111)
```
It's important to know the Order in which the recipient phone number the notification(SMS) will be sent to will be used
1) If you have defined the routeNotificationForAfricasTalking() method on the Notifiable class (User.php in this case) and returned a valid phone number, then that will be used.
2) if you did not define routeNotificationForAfricasTalking() method on the Notifiable class (User.php in this case), then the phone attribute of the User will be used ($user->phone)
3) Lastly if 1 and 2 are not set, then you can set the recipient phone number using ->to(19283281921)
``` php
return (new AfricasTalkingMessage())
->content('Your SMS message content')
->to("put the recipient phonenumber here");
```
## Testing
Expand Down
11 changes: 8 additions & 3 deletions src/AfricasTalkingChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace NotificationChannels\AfricasTalking;

use AfricasTalking\SDK\AfricasTalking as AfricasTalkingSDK;
use Exception;
use Illuminate\Notifications\Notification;
use AfricasTalking\SDK\AfricasTalking as AfricasTalkingSDK;
use NotificationChannels\AfricasTalking\Exceptions\InvalidPhonenumber;
use NotificationChannels\AfricasTalking\Exceptions\CouldNotSendNotification;

class AfricasTalkingChannel
Expand All @@ -30,7 +31,11 @@ public function send($notifiable, Notification $notification)
$message = $notification->toAfricasTalking($notifiable);

if (!$phoneNumber = $notifiable->routeNotificationFor('africasTalking')) {
$phoneNumber = $notifiable->phone_number;
$phoneNumber = $notifiable->phone_number ?? $message->getTo();
}

if(empty($phoneNumber)) {
throw InvalidPhonenumber::configurationNotSet();
}

if (empty(($message->getSender())) || is_null($message->getSender())) {
Expand All @@ -47,7 +52,7 @@ public function send($notifiable, Notification $notification)
}

try {
$this->africasTalking->sms()->send($params);
return $this->africasTalking->sms()->send($params);
} catch (Exception $e) {
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
}
Expand Down
26 changes: 26 additions & 0 deletions src/AfricasTalkingMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class AfricasTalkingMessage
/** @var string|null */
protected $from;

/** @var string|null */
protected $to;

/**
* Set content for this message.
*
Expand All @@ -36,6 +39,19 @@ public function from(string $from): self
return $this;
}

/**
* Set recipient for this message.
*
* @param string $from
* @return self
*/
public function to(string $to): self
{
$this->to = trim($to);

return $this;
}

/**
* Get message content.
*
Expand All @@ -55,4 +71,14 @@ public function getSender()
{
return $this->from ?? config('services.africastalking.from');
}

/**
* Get recipient info.
*
* @return string
*/
public function getTo()
{
return $this->to ?? null;
}
}
21 changes: 21 additions & 0 deletions src/Exceptions/InvalidPhonenumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace NotificationChannels\AfricasTalking\Exceptions;

use Exception;

class InvalidPhonenumber extends Exception
{
public static function configurationNotSet(): self
{
return new static(
"please provide a phonenumber to which the notification(SMS) will be sent to, you can do these in three ways:
1) by defining a routeNotificationForAfricasTalking method on your notifiable entity
or
2) by creating a new column called `phone` in your notifiable table
3) by chaining from(phonenumber) method to your AfricasTalkingMessage() eg (new AfricasTalkingMessage())
->content('Your SMS message content')->to(11111111); method in your event class
"
);
}
}

0 comments on commit a2d350f

Please sign in to comment.