Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
smsmanager.php
1<?php
3
13
15{
16 public const ON_MESSAGE_SUCCESSFULLY_SENT_EVENT = 'OnMessageSuccessfullySent';
17
18 private static $senders;
19
23 public static function getSenders()
24 {
25 if (self::$senders === null)
26 {
27 self::$senders = [];
28
29 if (Sms\Twilio::isSupported())
30 {
31 self::$senders[] = new Sms\Twilio();
32 }
33 if (Sms\Twilio2::isSupported())
34 {
35 self::$senders[] = new Sms\Twilio2();
36 }
37 if (Sms\SmsLineBy::isSupported())
38 {
39 self::$senders[] = new Sms\SmsLineBy();
40 }
41 if (Sms\MfmsRu::isSupported())
42 {
43 self::$senders[] = new Sms\MfmsRu();
44 }
45 if (Sms\Rest::isSupported())
46 {
47 self::$senders[] = new Sms\Rest();
48 }
49 if (Sms\SmscUa::isSupported())
50 {
51 self::$senders[] = new Sms\SmscUa();
52 }
53 if (Sms\ISmsCenter::isSupported())
54 {
55 self::$senders[] = new Sms\ISmsCenter();
56 }
57
58 $sender = new Sms\SmsRu();
59 if (Sms\SmsRu::isSupported() || $sender->isRegistered())
60 {
61 self::$senders[] = $sender;
62 }
63
64 $sender = new Sms\SmsAssistentBy();
65 if (Sms\SmsAssistentBy::isSupported() || $sender->isRegistered())
66 {
67 self::$senders[] = $sender;
68 }
69
70 $sender = new Sms\SmsEdnaru();
71 if (Sms\SmsEdnaru::isSupported() || $sender->isRegistered())
72 {
73 self::$senders[] = $sender;
74 }
75
76 $sender = new Sms\Ednaru();
77 if (Sms\Ednaru::isSupported() || $sender->isRegistered())
78 {
79 self::$senders[] = $sender;
80 }
81
82 $sender = new Sms\EdnaruImHpx();
83 if (Sms\EdnaruImHpx::isSupported() || $sender->isRegistered())
84 {
85 self::$senders[] = $sender;
86 }
87
88 self::fireSendersEvent();
89 }
90 return self::$senders;
91 }
92
93 private static function fireSendersEvent()
94 {
95 $event = new Event('messageservice', 'onGetSmsSenders');
96 $event->send();
97 foreach ($event->getResults() as $result)
98 {
99 if ($result->getType() === EventResult::ERROR)
100 {
101 continue;
102 }
103 $resultData = $result->getParameters();
104 foreach ($resultData as $sender)
105 {
106 if (
107 $sender instanceof Base
108 && $sender->getType() === MessageType::SMS
109 && $sender::isSupported()
110 )
111 {
112 self::$senders[] = $sender;
113 }
114 }
115 }
116 }
117
121 public static function getSenderSelectList()
122 {
123 $select = array();
124 foreach (static::getSenders() as $sender)
125 {
126 $select[$sender->getId()] = $sender->getName();
127 }
128 return $select;
129 }
130
134 public static function getSenderInfoList()
135 {
136 $info = array();
137 foreach (static::getSenders() as $sender)
138 {
139 $info[] = array(
140 'id' => $sender->getId(),
141 'type' => $sender->getType(),
142 'isTemplatesBased' => ($sender->isConfigurable() && $sender->isTemplatesBased()),
143 'name' => $sender->getName(),
144 'shortName' => $sender->getShortName(),
145 'canUse' => $sender->canUse()
146 );
147 }
148 return $info;
149 }
150
155 public static function getSenderById($id)
156 {
157 foreach (static::getSenders() as $sender)
158 {
159 if ($sender->getId() === $id)
160 {
161 return $sender;
162 }
163 }
164 return null;
165 }
166
171 public static function getDefaultSender()
172 {
173 $region = \Bitrix\Main\Application::getInstance()->getLicense()->getRegion();
174
175 if ($region === 'ru')
176 {
177 return static::getSenderById(\Bitrix\MessageService\Sender\Sms\SmsRu::ID);
178 }
179 if ($region === 'by')
180 {
181 return static::getSenderById(\Bitrix\MessageService\Sender\Sms\SmsAssistentBy::ID);
182 }
183
184 return static::getSenders()[0];
185 }
186
190 public static function canUse()
191 {
192 return static::getUsableSender() !== null;
193 }
194
198 public static function getManageUrl()
199 {
201 $defaultProvider = static::getDefaultSender();
202 return $defaultProvider instanceof BaseConfigurable ? $defaultProvider->getManageUrl() : '';
203 }
204
209 public static function getUsableSender()
210 {
211 foreach (static::getSenders() as $sender)
212 {
213 if ($sender->canUse())
214 {
215 return $sender;
216 }
217 }
218 return null;
219 }
220
224 public static function getRegisteredSenderList(): array
225 {
226 $senderList = [];
227 foreach (static::getSenders() as $sender)
228 {
229 if (
230 $sender instanceof BaseConfigurable
231 && $sender->isRegistered()
232 )
233 {
234 $senderList[] = $sender;
235 }
236 }
237
238 return $senderList;
239 }
240
247 public static function createMessage(array $messageFields, Base $sender = null)
248 {
249 if (!$sender && !isset($messageFields['SENDER_ID']))
250 {
251 $sender = static::getUsableSender();
252 }
253
254 if ($sender === null)
255 {
256 $message = new Message($sender);
257 $message->setError(new Main\Error('There are no registered SMS providers.'));
258 }
259
260 if (isset($messageFields['MESSAGE_TO']))
261 {
262 $normalizedTo = \NormalizePhone($messageFields['MESSAGE_TO']);
263 if ($normalizedTo)
264 {
265 $messageFields['MESSAGE_TO'] = '+'.$normalizedTo;
266 }
267 }
268
269 $message = Message::createFromFields($messageFields, $sender);
270 $message->setType(MessageType::SMS);
271
272 $sender = $message->getSender();
273 if ($sender && !$message->getFrom())
274 {
275 $message->setFrom($sender->getDefaultFrom());
276 }
277
278 return $message;
279 }
280
286 public static function createMessageListByTemplate(string $eventName, array $fields = []): array
287 {
288 $messageList = [];
289 $event = new Main\Sms\Event($eventName, $fields);
290 $templateMessageListResult = $event->createMessageList();
291 if (!$templateMessageListResult->isSuccess())
292 {
293 return $messageList;
294 }
295
296 $templateMessages = $templateMessageListResult->getData();
297 $sender = isset($fields['SENDER_ID']) ? static::getSenderById($fields['SENDER_ID']) : static::getUsableSender();
298
300 foreach($templateMessages as $templateMessage)
301 {
302 $message = Message::createFromFields(
303 [
304 'MESSAGE_FROM' => $fields['DEFAULT_FROM'] ?? $sender->getDefaultFrom(),
305 'MESSAGE_TO' => $templateMessage->getReceiver(),
306 'MESSAGE_BODY' => $templateMessage->getText(),
307 ],
308 $sender
309 );
310 $message->setType(MessageType::SMS);
311 $messageList[] = $message;
312 }
313
314 return $messageList;
315 }
316
323 public static function sendMessage(array $messageFields, Base $sender = null)
324 {
325 if (!$sender && isset($messageFields['SENDER_ID']))
326 {
327 $sender = static::getSenderById($messageFields['SENDER_ID']);
328
329 if ($sender === null)
330 {
331 return (new Result())->addError(new Main\Error('Incorrect sender id.'));
332 }
333 }
334 $message = static::createMessage($messageFields, $sender);
335
336 if ($message->getError() !== null)
337 {
338 return (new Result())->addError($message->getError());
339 }
340
341 $result = $message->send();
342
343 if ($result->isSuccess())
344 {
345 (new Event(
346 'messageservice',
347 static::ON_MESSAGE_SUCCESSFULLY_SENT_EVENT,
348 [
349 'ID' => $result->getId(),
350 'ADDITIONAL_FIELDS' => $messageFields['ADDITIONAL_FIELDS'] ?? [],
351 ]
352 ))->send();
353 }
354
355 return $result;
356 }
357
364 public static function sendMessageDirectly(array $messageFields, Base $sender = null)
365 {
366 $message = static::createMessage($messageFields, $sender);
367
368 return $message->sendDirectly();
369 }
370
371 public static function getMessageStatus($messageId)
372 {
373 $message = MessageTable::getById($messageId)->fetch();
374 if (!$message)
375 {
376 $result = new Result\MessageStatus();
377 $result->setId($messageId);
378 $result->addError(new Main\Error('Message not found'));
379
380 return $result;
381 }
382
384 $sender = static::getSenderById($message['SENDER_ID']);
385 if (!$sender || !$sender->isConfigurable())
386 {
387 $result = new Result\MessageStatus();
388 $result->setId($messageId);
389 $result->addError(new Main\Error('Incorrect sender id.'));
390
391 return $result;
392 }
393
394 return $sender->getMessageStatus($message);
395 }
396}
static createFromFields(array $fields, Sender\Base $sender=null)
Definition message.php:89
static createMessage(array $messageFields, Base $sender=null)
static sendMessage(array $messageFields, Base $sender=null)
static sendMessageDirectly(array $messageFields, Base $sender=null)