3
3
namespace NotificationChannels \Twilio ;
4
4
5
5
use NotificationChannels \Twilio \Exceptions \CouldNotSendNotification ;
6
- use Services_Twilio as TwilioService ;
6
+ use Twilio \ Rest \ Client as TwilioService ;
7
7
8
8
class Twilio
9
9
{
@@ -13,34 +13,38 @@ class Twilio
13
13
protected $ twilioService ;
14
14
15
15
/**
16
- * Default 'from' from config.
17
- * @var string
16
+ * @var TwilioConfig
18
17
*/
19
- protected $ from ;
18
+ private $ config ;
20
19
21
20
/**
22
21
* Twilio constructor.
23
22
*
24
- * @param TwilioService $twilioService
25
- * @param string $from
23
+ * @param TwilioService $twilioService
24
+ * @param TwilioConfig $config
26
25
*/
27
- public function __construct (TwilioService $ twilioService , $ from )
26
+ public function __construct (TwilioService $ twilioService , TwilioConfig $ config )
28
27
{
29
28
$ this ->twilioService = $ twilioService ;
30
- $ this ->from = $ from ;
29
+ $ this ->config = $ config ;
31
30
}
32
31
33
32
/**
34
33
* Send a TwilioMessage to the a phone number.
35
34
*
36
- * @param TwilioMessage $message
37
- * @param $to
35
+ * @param TwilioMessage $message
36
+ * @param string $to
37
+ * @param bool $useAlphanumericSender
38
38
* @return mixed
39
39
* @throws CouldNotSendNotification
40
40
*/
41
- public function sendMessage (TwilioMessage $ message , $ to )
41
+ public function sendMessage (TwilioMessage $ message , $ to, $ useAlphanumericSender = false )
42
42
{
43
43
if ($ message instanceof TwilioSmsMessage) {
44
+ if ($ useAlphanumericSender && $ sender = $ this ->getAlphanumericSender ()) {
45
+ $ message ->from ($ sender );
46
+ }
47
+
44
48
return $ this ->sendSmsMessage ($ message , $ to );
45
49
}
46
50
@@ -51,30 +55,68 @@ public function sendMessage(TwilioMessage $message, $to)
51
55
throw CouldNotSendNotification::invalidMessageObject ($ message );
52
56
}
53
57
54
- protected function sendSmsMessage ($ message , $ to )
58
+ /**
59
+ * Send an sms message using the Twilio Service.
60
+ *
61
+ * @param TwilioSmsMessage $message
62
+ * @param string $to
63
+ * @return \Twilio\Rest\Api\V2010\Account\MessageInstance
64
+ */
65
+ protected function sendSmsMessage (TwilioSmsMessage $ message , $ to )
55
66
{
56
- return $ this ->twilioService ->account ->messages ->sendMessage (
57
- $ this ->getFrom ($ message ),
58
- $ to ,
59
- trim ($ message ->content )
60
- );
67
+ $ params = [
68
+ 'from ' => $ this ->getFrom ($ message ),
69
+ 'body ' => trim ($ message ->content ),
70
+ ];
71
+
72
+ if ($ serviceSid = $ this ->config ->getServiceSid ()) {
73
+ $ params ['messagingServiceSid ' ] = $ serviceSid ;
74
+ }
75
+
76
+ return $ this ->twilioService ->messages ->create ($ to , $ params );
61
77
}
62
78
63
- protected function makeCall ($ message , $ to )
79
+ /**
80
+ * Make a call using the Twilio Service.
81
+ *
82
+ * @param TwilioCallMessage $message
83
+ * @param string $to
84
+ * @return \Twilio\Rest\Api\V2010\Account\CallInstance
85
+ */
86
+ protected function makeCall (TwilioCallMessage $ message , $ to )
64
87
{
65
- return $ this ->twilioService ->account ->calls ->create (
66
- $ this ->getFrom ($ message ),
88
+ return $ this ->twilioService ->calls ->create (
67
89
$ to ,
68
- trim ($ message ->content )
90
+ $ this ->getFrom ($ message ),
91
+ ['url ' => trim ($ message ->content )]
69
92
);
70
93
}
71
94
72
- protected function getFrom ($ message )
95
+ /**
96
+ * Get the from address from message, or config.
97
+ *
98
+ * @param TwilioMessage $message
99
+ * @return string
100
+ * @throws CouldNotSendNotification
101
+ */
102
+ protected function getFrom (TwilioMessage $ message )
73
103
{
74
- if (! $ from = $ message ->from ?: $ this ->from ) {
104
+ if (! $ from = $ message ->getFrom () ?: $ this ->config -> getFrom () ) {
75
105
throw CouldNotSendNotification::missingFrom ();
76
106
}
77
107
78
108
return $ from ;
79
109
}
110
+
111
+ /**
112
+ * Get the alphanumeric sender from config, if one exists.
113
+ *
114
+ * @return string|null
115
+ */
116
+ protected function getAlphanumericSender ()
117
+ {
118
+ if ($ sender = $ this ->config ->getAlphanumericSender ()) {
119
+ return $ sender ;
120
+ }
121
+ }
80
122
}
0 commit comments