18 private static $senders;
25 if (self::$senders ===
null)
29 if (Sms\Twilio::isSupported())
33 if (Sms\Twilio2::isSupported())
37 if (Sms\SmsLineBy::isSupported())
41 if (Sms\MfmsRu::isSupported())
45 if (Sms\Rest::isSupported())
49 if (Sms\SmscUa::isSupported())
53 if (Sms\ISmsCenter::isSupported())
59 if (Sms\SmsRu::isSupported() || $sender->isRegistered())
61 self::$senders[] = $sender;
65 if (Sms\SmsAssistentBy::isSupported() || $sender->isRegistered())
67 self::$senders[] = $sender;
71 if (Sms\SmsEdnaru::isSupported() || $sender->isRegistered())
73 self::$senders[] = $sender;
77 if (Sms\Ednaru::isSupported() || $sender->isRegistered())
79 self::$senders[] = $sender;
83 if (Sms\EdnaruImHpx::isSupported() || $sender->isRegistered())
85 self::$senders[] = $sender;
88 self::fireSendersEvent();
90 return self::$senders;
93 private static function fireSendersEvent()
95 $event =
new Event(
'messageservice',
'onGetSmsSenders');
97 foreach ($event->getResults() as $result)
99 if ($result->getType() === EventResult::ERROR)
103 $resultData = $result->getParameters();
104 foreach ($resultData as $sender)
107 $sender instanceof
Base
109 && $sender::isSupported()
112 self::$senders[] = $sender;
124 foreach (static::getSenders() as $sender)
126 $select[$sender->getId()] = $sender->getName();
137 foreach (static::getSenders() as $sender)
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()
157 foreach (static::getSenders() as $sender)
159 if ($sender->getId() === $id)
173 $region = \Bitrix\Main\Application::getInstance()->getLicense()->getRegion();
175 if ($region ===
'ru')
177 return static::getSenderById(\
Bitrix\MessageService\
Sender\Sms\SmsRu::ID);
179 if ($region ===
'by')
181 return static::getSenderById(\
Bitrix\MessageService\
Sender\Sms\SmsAssistentBy::ID);
184 return static::getSenders()[0];
192 return static::getUsableSender() !==
null;
198 public static function getManageUrl()
201 $defaultProvider = static::getDefaultSender();
211 foreach (static::getSenders() as $sender)
213 if ($sender->canUse())
227 foreach (static::getSenders() as $sender)
234 $senderList[] = $sender;
249 if (!$sender && !isset($messageFields[
'SENDER_ID']))
251 $sender = static::getUsableSender();
254 if ($sender ===
null)
256 $message =
new Message($sender);
257 $message->setError(
new Main\
Error(
'There are no registered SMS providers.'));
260 if (isset($messageFields[
'MESSAGE_TO']))
262 $normalizedTo = \NormalizePhone($messageFields[
'MESSAGE_TO']);
265 $messageFields[
'MESSAGE_TO'] =
'+'.$normalizedTo;
272 $sender = $message->getSender();
273 if ($sender && !$message->getFrom())
275 $message->setFrom($sender->getDefaultFrom());
286 public static function createMessageListByTemplate(
string $eventName, array $fields = []): array
290 $templateMessageListResult = $event->createMessageList();
291 if (!$templateMessageListResult->isSuccess())
296 $templateMessages = $templateMessageListResult->getData();
297 $sender = isset($fields[
'SENDER_ID']) ? static::getSenderById($fields[
'SENDER_ID']) : static::
getUsableSender();
300 foreach($templateMessages as $templateMessage)
304 'MESSAGE_FROM' => $fields[
'DEFAULT_FROM'] ?? $sender->getDefaultFrom(),
305 'MESSAGE_TO' => $templateMessage->getReceiver(),
306 'MESSAGE_BODY' => $templateMessage->getText(),
311 $messageList[] = $message;
325 if (!$sender && isset($messageFields[
'SENDER_ID']))
327 $sender = static::getSenderById($messageFields[
'SENDER_ID']);
329 if ($sender ===
null)
331 return (
new Result())->addError(
new Main\
Error(
'Incorrect sender id.'));
334 $message = static::createMessage($messageFields, $sender);
336 if ($message->getError() !==
null)
338 return (
new Result())->addError($message->getError());
341 $result = $message->send();
343 if ($result->isSuccess())
347 static::ON_MESSAGE_SUCCESSFULLY_SENT_EVENT,
349 'ID' => $result->getId(),
350 'ADDITIONAL_FIELDS' => $messageFields[
'ADDITIONAL_FIELDS'] ?? [],
366 $message = static::createMessage($messageFields, $sender);
368 return $message->sendDirectly();
371 public static function getMessageStatus($messageId)
373 $message = MessageTable::getById($messageId)->fetch();
377 $result->setId($messageId);
378 $result->addError(
new Main\
Error(
'Message not found'));
384 $sender = static::getSenderById($message[
'SENDER_ID']);
385 if (!$sender || !$sender->isConfigurable())
387 $result =
new Result\MessageStatus();
388 $result->setId($messageId);
389 $result->addError(
new Main\
Error(
'Incorrect sender id.'));
394 return $sender->getMessageStatus($message);