33namespace NotificationChannels \Twilio ;
44
55use NotificationChannels \Twilio \Exceptions \CouldNotSendNotification ;
6- use Services_Twilio as TwilioService ;
6+ use Twilio \ Rest \ Client as TwilioService ;
77
88class Twilio
99{
@@ -13,34 +13,38 @@ class Twilio
1313 protected $ twilioService ;
1414
1515 /**
16- * Default 'from' from config.
17- * @var string
16+ * @var TwilioConfig
1817 */
19- protected $ from ;
18+ private $ config ;
2019
2120 /**
2221 * Twilio constructor.
2322 *
24- * @param TwilioService $twilioService
25- * @param string $from
23+ * @param TwilioService $twilioService
24+ * @param TwilioConfig $config
2625 */
27- public function __construct (TwilioService $ twilioService , $ from )
26+ public function __construct (TwilioService $ twilioService , TwilioConfig $ config )
2827 {
2928 $ this ->twilioService = $ twilioService ;
30- $ this ->from = $ from ;
29+ $ this ->config = $ config ;
3130 }
3231
3332 /**
3433 * Send a TwilioMessage to the a phone number.
3534 *
36- * @param TwilioMessage $message
37- * @param $to
35+ * @param TwilioMessage $message
36+ * @param string $to
37+ * @param bool $useAlphanumericSender
3838 * @return mixed
3939 * @throws CouldNotSendNotification
4040 */
41- public function sendMessage (TwilioMessage $ message , $ to )
41+ public function sendMessage (TwilioMessage $ message , $ to, $ useAlphanumericSender = false )
4242 {
4343 if ($ message instanceof TwilioSmsMessage) {
44+ if ($ useAlphanumericSender && $ sender = $ this ->getAlphanumericSender ()) {
45+ $ message ->from ($ sender );
46+ }
47+
4448 return $ this ->sendSmsMessage ($ message , $ to );
4549 }
4650
@@ -51,30 +55,68 @@ public function sendMessage(TwilioMessage $message, $to)
5155 throw CouldNotSendNotification::invalidMessageObject ($ message );
5256 }
5357
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 )
5566 {
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 );
6177 }
6278
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 )
6487 {
65- return $ this ->twilioService ->account ->calls ->create (
66- $ this ->getFrom ($ message ),
88+ return $ this ->twilioService ->calls ->create (
6789 $ to ,
68- trim ($ message ->content )
90+ $ this ->getFrom ($ message ),
91+ ['url ' => trim ($ message ->content )]
6992 );
7093 }
7194
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 )
73103 {
74- if (! $ from = $ message ->from ?: $ this ->from ) {
104+ if (! $ from = $ message ->getFrom () ?: $ this ->config -> getFrom () ) {
75105 throw CouldNotSendNotification::missingFrom ();
76106 }
77107
78108 return $ from ;
79109 }
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+ }
80122}
0 commit comments