|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tzsk\Sms\Drivers; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use Twilio\Rest\Client; |
|
6
|
|
|
use Tzsk\Sms\Contract\SendSmsInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Twilio extends MasterDriver implements SendSmsInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Twilio Settings. |
|
12
|
|
|
* |
|
13
|
|
|
* @var null|object |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $settings = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Twilio Client. |
|
19
|
|
|
* |
|
20
|
|
|
* @var null|Client |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $client = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Construct the class with the relevant settings. |
|
26
|
|
|
* |
|
27
|
|
|
* SendSmsInterface constructor. |
|
28
|
|
|
* @param $settings object |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($settings) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->settings = (object) $settings; |
|
33
|
|
|
$this->client = new Client($this->settings->sid, $this->settings->token); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Send text message and return response. |
|
38
|
|
|
* |
|
39
|
|
|
* @return object |
|
40
|
|
|
*/ |
|
41
|
|
|
public function send() |
|
42
|
|
|
{ |
|
43
|
|
|
$response = ['status' => true, 'data' =>[]]; |
|
44
|
|
|
foreach ($this->recipients as $recipient) { |
|
45
|
|
|
$sms = $this->client->account->messages->create( |
|
46
|
|
|
$recipient, |
|
47
|
|
|
array( |
|
48
|
|
|
'from' => $this->settings->from, |
|
49
|
|
|
'body' => $this->body |
|
50
|
|
|
) |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$response['data'][$recipient] = $this->getSmsResponse($sms); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return (object) $response; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the Twilio Response. |
|
61
|
|
|
* |
|
62
|
|
|
* @param $sms |
|
63
|
|
|
* @return object |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function getSmsResponse($sms) |
|
66
|
|
|
{ |
|
67
|
|
|
$attributes = [ |
|
68
|
|
|
'accountSid', 'apiVersion', 'body', 'direction', 'errorCode', |
|
69
|
|
|
'errorMessage', 'from', 'numMedia', 'numSegments', 'price', |
|
70
|
|
|
'priceUnit', 'sid', 'status', 'subresourceUris', 'to', 'uri', |
|
71
|
|
|
'dateCreated', 'dateUpdated', 'dateSent', |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
$res = []; |
|
75
|
|
|
foreach ($attributes as $attribute) { |
|
76
|
|
|
$res[$attribute] = $sms->$attribute; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return (object) $res; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |