|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twilio; |
|
4
|
|
|
|
|
5
|
|
|
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification; |
|
6
|
|
|
use Services_Twilio as TwilioService; |
|
7
|
|
|
|
|
8
|
|
|
class Twilio |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var TwilioService |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $twilioService; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var TwilioConfig |
|
17
|
|
|
*/ |
|
18
|
|
|
private $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Twilio constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param TwilioService $twilioService |
|
24
|
|
|
* @param TwilioConfig $config |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(TwilioService $twilioService, TwilioConfig $config) |
|
27
|
7 |
|
{ |
|
28
|
|
|
$this->twilioService = $twilioService; |
|
29
|
7 |
|
$this->config = $config; |
|
30
|
7 |
|
} |
|
31
|
7 |
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Send a TwilioMessage to the a phone number. |
|
34
|
|
|
* |
|
35
|
|
|
* @param TwilioMessage $message |
|
36
|
|
|
* @param $to |
|
37
|
|
|
* @param bool $useAlphanumericSender |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
* @throws CouldNotSendNotification |
|
40
|
|
|
*/ |
|
41
|
6 |
|
public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false) |
|
42
|
|
|
{ |
|
43
|
6 |
|
if ($message instanceof TwilioSmsMessage) { |
|
44
|
3 |
|
if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) { |
|
45
|
|
|
$message->from($sender); |
|
46
|
|
|
} |
|
47
|
3 |
|
|
|
48
|
2 |
|
return $this->sendSmsMessage($message, $to); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
if ($message instanceof TwilioCallMessage) { |
|
52
|
|
|
return $this->makeCall($message, $to); |
|
53
|
|
|
} |
|
54
|
3 |
|
|
|
55
|
|
|
throw CouldNotSendNotification::invalidMessageObject($message); |
|
56
|
3 |
|
} |
|
57
|
3 |
|
|
|
58
|
|
|
protected function sendSmsMessage($message, $to) |
|
59
|
2 |
|
{ |
|
60
|
|
|
return $this->twilioService->account->messages->sendMessage( |
|
61
|
|
|
$this->getFrom($message), |
|
62
|
|
|
$to, |
|
63
|
2 |
|
trim($message->content), |
|
64
|
|
|
null, |
|
65
|
2 |
|
$this->config->getSmsParams() |
|
66
|
2 |
|
); |
|
67
|
|
|
} |
|
68
|
2 |
|
|
|
69
|
|
|
protected function makeCall($message, $to) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->twilioService->account->calls->create( |
|
72
|
5 |
|
$this->getFrom($message), |
|
73
|
|
|
$to, |
|
74
|
5 |
|
trim($message->content) |
|
75
|
1 |
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
protected function getFrom($message) |
|
79
|
|
|
{ |
|
80
|
|
|
if (! $from = $message->getFrom() ?: $this->config->getFrom()) { |
|
81
|
|
|
throw CouldNotSendNotification::missingFrom(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $from; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getAlphanumericSender() |
|
88
|
|
|
{ |
|
89
|
|
|
if ($sender = $this->config->getAlphanumericSender()) { |
|
90
|
|
|
return $sender; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|