-
Notifications
You must be signed in to change notification settings - Fork 1
/
wa_sender.php
232 lines (199 loc) · 7.97 KB
/
wa_sender.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
use Blesta\Core\Util\Input\Fields\InputFields;
use WaSender\WaSenderClient;
require_once dirname(__FILE__) . DS . 'libs' . DS . 'client.php';
/**
* WA Sender Messenger
*
* @package blesta
* @subpackage blesta.components.messengers.wasender
* @copyright Copyright (c) 2023, PT Pedjoeang Digital Networks.
* @license http://www.blesta.com/license/ The Blesta License Agreement
* @link http://www.pedjoeangdigital.net/ Pedjoeang Digital Networks
*/
class WaSender extends Messenger
{
/**
* Initializes the messenger.
*/
public function __construct()
{
// Load configuration required by this messenger
$this->loadConfig(dirname(__FILE__) . DS . 'config.json');
// Load the helpers required by this messenger
Loader::loadHelpers($this, ['Html']);
// Load the language required by this messenger
Language::loadLang('wa_sender', null, dirname(__FILE__) . DS . 'language' . DS);
}
/**
* Returns all fields used when setting up a messenger, including any
* javascript to execute when the page is rendered with these fields.
*
* @param array $vars An array of post data submitted to the manage messenger page
* @return InputFields An InputFields object, containing the fields to render
* as well as any additional HTML markup to include
*/
public function getConfigurationFields(&$vars = [])
{
$fields = new InputFields();
// Phone Number
$phoneNumber = $fields->label(Language::_('WaSender.configuration_fields.phone_number', true), 'wasender_phone_number');
$phoneNumber->attach(
$fields->fieldText('phone_number', (isset($vars['phone_number']) ? $vars['phone_number'] : null), ['id' => 'wasender_phone_number'])
);
$phoneNumber->attach(
$fields->tooltip(Language::_('WaSender.configuration_fields.phone_number_help', true))
);
$fields->setField($phoneNumber);
// API Key
$apiKey = $fields->label(Language::_('WaSender.configuration_fields.api_key', true), 'wasender_api_key');
$apiKey->attach(
$fields->fieldText('api_key', (isset($vars['api_key']) ? $vars['api_key'] : null), ['id' => 'wasender_api_key'])
);
$apiKey->attach(
$fields->tooltip(Language::_('WaSender.configuration_fields.api_key_help', true))
);
$fields->setField($apiKey);
// API Url
$apiUrl = $fields->label(Language::_('WaSender.configuration_fields.api_url', true), 'wasender_api_url');
$apiUrl->attach(
$fields->fieldText('api_url', (isset($vars['api_url']) ? $vars['api_url'] : null), ['id' => 'wasender_api_url'])
);
$apiUrl->attach(
$fields->tooltip(Language::_('WaSender.configuration_fields.api_url_help', true))
);
$fields->setField($apiUrl);
return $fields;
}
/**
* Updates the meta data for this messenger
*
* @param array $vars An array of messenger info to add
* @return array A numerically indexed array of meta fields containing:
*
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
*/
public function setMeta(array $vars)
{
$meta_fields = ['phone_number', 'api_key', 'api_url'];
$encrypted_fields = ['api_key'];
$meta = [];
foreach ($vars as $key => $value) {
if (in_array($key, $meta_fields)) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
];
}
}
return $meta;
}
/**
* Send a message.
*
* @param mixed $to_user_id The user ID this message is to
* @param string $content The content of the message to send
* @param string $type The type of the message to send (optional)
*/
public function send($to_user_id, $content, $type = null)
{
// Initialize the API
$meta = $this->getMessengerMeta();
if (!($api = $this->getApi())) {
return null;
}
Loader::loadModels($this, ['Staff', 'Clients', 'Contacts']);
// Fetch user information
$is_client = true;
if (($user = $this->Staff->getByUserId($to_user_id))) {
$is_client = false;
} else {
$user = $this->Clients->getByUserId($to_user_id);
$phone_numbers = $this->Contacts->getNumbers($user->contact_id);
if (is_array($phone_numbers) && !empty($phone_numbers)) {
$user->phone_number = reset($phone_numbers);
}
}
// Send message
$error = null;
$success = false;
$recipient = $is_client
? (isset($user->phone_number->number) ? $user->phone_number->number : null)
: (isset($user->number_mobile) ? $user->number_mobile : null);
// remove any spaces or dashes or dot from the phone number
$recipient = str_replace([' ', '-', '.'], '', $recipient);
$recipient = str_replace(['+'], '', $recipient);
if ($type == 'sms') {
// SMS allows up to 918 characters, by concatenating 6 messages of 153 characters each
if (strlen($content) > 918) {
$content = substr($content, 0, 918);
}
$params = [
'from' => $meta->phone_number,
'body' => $content,
'to' => $recipient
];
$this->log($to_user_id, json_encode($params, JSON_PRETTY_PRINT), 'input', true);
if (empty($recipient)) {
$response = [
'status' => false,
'message' => 'Recipient phone number is empty'
];
$this->log($to_user_id, json_encode($response, JSON_PRETTY_PRINT), 'output', false);
return null;
}
$recipient = str_replace('+', '', $recipient);
// Send SMS
try {
$response = $api->sendTextMessage(
$recipient,
$content
);
$json = json_decode($response);
$success = $json->status == true;
$this->log($to_user_id, json_encode($response, JSON_PRETTY_PRINT), 'output', $success);
} catch (\Exception $e) {
$error = $e->getMessage();
$success = false;
$this->log($to_user_id, json_encode($error, JSON_PRETTY_PRINT), 'output', $success);
}
} else {
$params = [
'from' => $meta->phone_number,
'body' => $content
];
$this->log($to_user_id, json_encode($params, JSON_PRETTY_PRINT), 'input', true);
// Send Whatsapp
try {
$response = $api->sendTextMessage(
$recipient,
$content
);
$success = $response->status == true;
$this->log($to_user_id, json_encode($response, JSON_PRETTY_PRINT), 'output', $success);
} catch (\Exception $e) {
$error = $e->getMessage();
$success = false;
$this->log($to_user_id, json_encode($error, JSON_PRETTY_PRINT), 'output', $success);
}
}
}
/**
* Gets an instance of the WaSender API.
*
* @return WaSender\WaSenderClient An instance of the WaSender API Wrapper or null if the API can't be initialized
*/
private function getApi()
{
$meta = $this->getMessengerMeta();
try {
return new WaSenderClient($meta->api_url, $meta->api_key, $meta->phone_number);
} catch (\Exception $e) {
$this->setMessage('error', $e->getMessage());
return null;
}
}
}