1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Andreshg112\HablameSms; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
|
7
|
|
|
class Client |
8
|
|
|
{ |
9
|
|
|
private const API_URL = 'https://api103.hablame.co/api/'; |
10
|
|
|
|
11
|
|
|
/** Clave API suministrada por Háblame SMS. */ |
12
|
|
|
private string $apikey; |
13
|
|
|
|
14
|
|
|
/** Número del cliente en Háblame SMS. */ |
15
|
|
|
private string $account; |
16
|
|
|
|
17
|
|
|
/** Cliente de Guzzle. */ |
18
|
|
|
private GuzzleClient $http; |
19
|
|
|
|
20
|
|
|
/** Token enviado por correo al cliente desde el panel de Háblame SMS. */ |
21
|
|
|
private string $token; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Crea una instancia recibiendo el número del cliente y la clave. |
25
|
8 |
|
*/ |
26
|
|
|
public function __construct(string $account, string $apikey, string $token, GuzzleClient $http = null) |
27
|
8 |
|
{ |
28
|
|
|
$this->account = $account; |
29
|
8 |
|
|
30
|
|
|
$this->apikey = $apikey; |
31
|
8 |
|
|
32
|
8 |
|
$this->token = $token; |
33
|
|
|
|
34
|
|
|
$this->http = $http ?? new GuzzleClient(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Consulta el saldo. |
39
|
1 |
|
*/ |
40
|
|
|
public function checkBalance(): array |
41
|
1 |
|
{ |
42
|
|
|
$url = self::API_URL . 'account/v1/status'; |
43
|
1 |
|
|
44
|
|
|
$response = $this->http->get($url, ['headers' => $this->getAuthHeaders()]); |
45
|
1 |
|
|
46
|
|
|
return json_decode((string)$response->getBody(), true); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
private function getAuthHeaders(): array |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
'account' => $this->account, |
53
|
|
|
'apikey' => $this->apikey, |
54
|
|
|
'token' => $this->token, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
1 |
|
* Envía un mensaje de texto (SMS) al destinatario indicado. |
60
|
|
|
* |
61
|
|
|
* @param string $phoneNumber Número telefonico a enviar SMS. |
62
|
|
|
* @param string $sms Mensaje de texto a enviar. |
63
|
|
|
* @param string|null $datetime [optional] Fecha de envío. Si está vacío, se envía inmediatamente. |
64
|
|
|
* @param string|null $reference [optional] Número de referencia o nombre de campaña. |
65
|
1 |
|
* @param bool $flash [optional] Indica si es un mensaje flash, es decir, que ocupa la pantalla. |
66
|
|
|
* @param bool $priority [optional] Indica si el mensaje es prioritario (costo adicional). |
67
|
|
|
* @return array |
68
|
1 |
|
*/ |
69
|
1 |
|
public function sendMessage( |
70
|
1 |
|
string $phoneNumber, |
71
|
1 |
|
string $sms, |
72
|
1 |
|
string $datetime = null, |
73
|
1 |
|
string $reference = null, |
74
|
|
|
bool $flash = false, |
75
|
|
|
bool $priority = false |
76
|
1 |
|
): array { |
77
|
|
|
$url = self::API_URL . 'sms/v3/send/' . ($priority ? 'priority' : 'marketing'); |
78
|
1 |
|
|
79
|
|
|
$params = [ |
80
|
|
|
'toNumber' => $phoneNumber, |
81
|
|
|
'sms' => $sms, |
82
|
|
|
'flash' => (int)$flash, |
83
|
|
|
'sendDate' => isset($datetime) ? strtotime($datetime) : null, |
84
|
|
|
'reference_1' => $reference, |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$response = $this->http->post($url, ['headers' => $this->getAuthHeaders(), 'json' => array_filter($params)]); |
88
|
|
|
|
89
|
|
|
return json_decode((string)$response->getBody(), true); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|