1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
8
|
|
|
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
9
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
10
|
|
|
use Services_Twilio as Twilio; |
11
|
|
|
|
12
|
|
|
class TwilioChannel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Twilio |
16
|
|
|
*/ |
17
|
|
|
protected $twilio; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Dispatcher |
21
|
|
|
*/ |
22
|
|
|
protected $events; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default 'from' from config. |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $from; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* TwilioChannel constructor. |
32
|
|
|
* |
33
|
|
|
* @param Twilio $twilio |
34
|
|
|
* @param Dispatcher $events |
35
|
|
|
*/ |
36
|
9 |
|
public function __construct(Twilio $twilio, Dispatcher $events, $from) |
37
|
|
|
{ |
38
|
9 |
|
$this->twilio = $twilio; |
39
|
9 |
|
$this->events = $events; |
40
|
9 |
|
$this->from = $from; |
41
|
9 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Send the given notification. |
45
|
|
|
* |
46
|
|
|
* @param mixed $notifiable |
47
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
48
|
|
|
* @return mixed |
49
|
|
|
* @throws CouldNotSendNotification |
50
|
|
|
*/ |
51
|
9 |
|
public function send($notifiable, Notification $notification) |
52
|
|
|
{ |
53
|
9 |
|
if (! $to = $notifiable->routeNotificationFor('twilio')) { |
54
|
8 |
|
if (! $to = $notifiable->phone_number) { |
55
|
1 |
|
return; |
56
|
|
|
} |
57
|
7 |
|
} |
58
|
|
|
|
59
|
|
|
try { |
60
|
8 |
|
$message = $notification->toTwilio($notifiable); |
61
|
|
|
|
62
|
8 |
|
if (is_string($message)) { |
63
|
2 |
|
$message = new TwilioSmsMessage($message); |
64
|
2 |
|
} |
65
|
|
|
|
66
|
8 |
|
if (! $message instanceof TwilioMessage) { |
67
|
1 |
|
throw CouldNotSendNotification::invalidMessageObject($message); |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
return $this->sendMessage($message, $to); |
71
|
3 |
|
} catch (Exception $exception) { |
72
|
3 |
|
$this->events->fire( |
73
|
3 |
|
new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()]) |
74
|
3 |
|
); |
75
|
|
|
} |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Send message to Twilio. |
80
|
|
|
* |
81
|
|
|
* @param TwilioMessage $message |
82
|
|
|
* @param string $to |
83
|
|
|
* @return mixed |
84
|
|
|
* |
85
|
|
|
* @throws \NotificationChannels\Twilio\Exceptions\CouldNotSendNotification |
86
|
|
|
*/ |
87
|
7 |
|
protected function sendMessage(TwilioMessage $message, $to) |
88
|
|
|
{ |
89
|
7 |
|
$from = $this->getFrom($message); |
90
|
|
|
|
91
|
6 |
|
if ($message instanceof TwilioSmsMessage) { |
92
|
3 |
|
return $this->twilio->account->messages->sendMessage( |
93
|
3 |
|
$from, |
94
|
3 |
|
$to, |
95
|
3 |
|
trim($message->content) |
96
|
3 |
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
if ($message instanceof TwilioCallMessage) { |
100
|
2 |
|
return $this->twilio->account->calls->create( |
101
|
2 |
|
$from, |
102
|
2 |
|
$to, |
103
|
2 |
|
trim($message->content) |
104
|
2 |
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
throw CouldNotSendNotification::invalidMessageObject($message); |
108
|
|
|
} |
109
|
|
|
|
110
|
7 |
|
protected function getFrom($message) |
111
|
|
|
{ |
112
|
7 |
|
if (! $from = $message->from ?: $this->from) { |
113
|
1 |
|
throw CouldNotSendNotification::missingFrom(); |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
return $from; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|