1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ednaru.php
См. документацию.
1<?php
2
3namespace Bitrix\MessageService\Sender\Sms;
4
5use Bitrix\Main\Loader;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Main\Result;
8use Bitrix\MessageService\Providers;
9use Bitrix\MessageService\Providers\Base;
10use Bitrix\MessageService\Providers\CacheManager;
11use Bitrix\MessageService\Providers\Edna\RegionHelper;
12use Bitrix\MessageService\Providers\Edna\WhatsApp;
13use Bitrix\MessageService\Sender;
14
16{
17 public const ID = 'ednaru';
18 public const DISABLE_INTERNATIONAL = 'disable_international';
19
22
23 public function __construct()
24 {
25 $this->informant = new WhatsApp\Informant();
26 $this->optionManager = new Base\Option($this->getType(), $this->getId());
27 $this->utils = new WhatsApp\Utils($this->getId(), $this->optionManager);
28 $this->registrar = new WhatsApp\Registrar($this->getId(), $this->optionManager, $this->utils);
29 $this->initiator = new WhatsApp\Initiator($this->optionManager, $this->registrar, $this->utils, $this->getId());
30 $emojiConverter = new WhatsApp\EmojiConverter();
31 $this->sender = new WhatsApp\Sender($this->optionManager, $this->registrar, $this->utils, $emojiConverter);
32 $this->templateManager = new WhatsApp\TemplateManager($this->getId(), $this->utils, $emojiConverter);
33 }
34
35 public function isAvailable(): bool
36 {
37 return self::isSupported();
38 }
39
40 public static function isSupported()
41 {
42 if (\Bitrix\Main\Config\Option::get('messageservice', 'force_enable') === 'Y')
43 {
44 return parent::isSupported();
45 }
46
48 if (
49 RegionHelper::isInternational()
50 && \Bitrix\Main\Application::getInstance()->getLicense()->getRegion() !== 'kz'
51 && (new \Bitrix\Main\Type\DateTime('2024-05-29 00:00:00', 'Y-m-d H:i:s'))->getTimestamp() > time()
52 )
53 {
54 return false;
55 }
56
58 if (\Bitrix\Main\Application::getInstance()->getLicense()->getRegion() === 'by')
59 {
60 return false;
61 }
62
63 return parent::isSupported();
64 }
65
66 public function getId(): string
67 {
68 return $this->informant->getId();
69 }
70
71 public function getName(): string
72 {
73 return $this->informant->getName();
74 }
75
76 public function getShortName(): string
77 {
78 return $this->informant->getShortName();
79 }
80
81 public function isRegistered(): bool
82 {
83 return $this->registrar->isRegistered();
84 }
85
86 public function register(array $fields): Result
87 {
88 $result = $this->registrar->register($fields);
89 if ($result->isSuccess())
90 {
91 \Bitrix\Main\Application::getInstance()->addBackgroundJob([$this, 'refreshFromList']);
92 \Bitrix\Main\Application::getInstance()->addBackgroundJob([$this, 'addRefreshFromListAgent']);
93 \CAgent::AddAgent(static::class . "::registerAutoTemplatesAgent();", 'messageservice', 'N', 10);
94 }
95 return $result;
96 }
97
98 public function resetCallback(): Result
99 {
100 return $this->registrar->resetCallback();
101 }
102
103 public function getOwnerInfo(): array
104 {
105 return $this->registrar->getOwnerInfo();
106 }
107
108 public function getExternalManageUrl(): string
109 {
110 return $this->registrar->getExternalManageUrl();
111 }
112
114 {
115 return $this->sender->getMessageStatus($messageFields);
116 }
117
119 {
120 return $this->sender->sendMessage($messageFields);
121 }
122
123 public function testConnection(): Result
124 {
125 return (new WhatsApp\ConnectorLine($this->utils))->testConnection();
126 }
127
131 public function getFromList(): array
132 {
133 return $this->initiator->getFromList();
134 }
135
140 public function refreshFromList(): void
141 {
142 $this->utils->updateSavedChannelList($this->initiator->getChannelType());
143 $this->utils->clearCache(Providers\CacheManager::CHANNEL_CACHE_ENTITY_ID);
144 }
145
146 public static function resolveStatus($serviceStatus): ?int
147 {
148 return (new WhatsApp\StatusResolver())->resolveStatus($serviceStatus);
149 }
150
151 public function getLineId(?int $subjectId = null): ?int
152 {
153 return (new WhatsApp\ConnectorLine($this->utils))->getLineId($subjectId);
154 }
155
156 public function getCallbackUrl(): string
157 {
158 return $this->registrar->getCallbackUrl();
159 }
160
164 public function isTemplatesBased(): bool
165 {
166 return $this->templateManager->isTemplatesBased();
167 }
168
169 public function isCorrectFrom($from)
170 {
171 return $this->initiator->isCorrectFrom($from);
172 }
173
177 public function getTemplatesList(array $context = null): array
178 {
179 return $this->templateManager->getTemplatesList($context);
180 }
181
185 public function prepareTemplate($templateData): array
186 {
187 return $this->templateManager->prepareTemplate($templateData);
188 }
189
190 public function getMessageTemplates(string $subject = ''): Result
191 {
192 return $this->utils->getMessageTemplates($subject);
193 }
194
195 public function getManageUrl(): string
196 {
197 return $this->informant->getManageUrl();
198 }
199
200 public function getSentTemplateMessage(string $from, string $to): string
201 {
202 return $this->utils->getSentTemplateMessage($from, $to);
203 }
204
205 public function prepareMessageBodyForSave(string $text): string
206 {
207 return $this->sender->prepareMessageBodyForSave($text);
208 }
209
211 {
212 $this->optionManager->setSocketTimeout($socketTimeout);
213 return $this;
214 }
215
217 {
218 $this->optionManager->setStreamTimeout($streamTimeout);
219 return $this;
220 }
221
222 public function sendTemplate(string $name, string $text, array $examples = [], ?string $langCode = null): Result
223 {
224 return $this->utils->sendTemplate($name, $text, $examples, $langCode);
225 }
226
232 public function addRefreshFromListAgent(): void
233 {
234 $cacheManager = new CacheManager($this->getId());
235 $period = (int)ceil( $cacheManager->getTtl(CacheManager::CHANNEL_CACHE_ENTITY_ID) * .9);// async with cache expiration
236
237 \CAgent::AddAgent(static::class . "::refreshFromListAgent();", 'messageservice', 'Y', $period);
238 }
239
244 public static function refreshFromListAgent(): string
245 {
246 $sender = new static();
247 if (!$sender::isSupported() || !$sender->isRegistered())
248 {
249 return '';
250 }
251
252 $sender->refreshFromList();
253
254 return __METHOD__ . '();';
255 }
256
260 public static function checkAutoTemplatesAgent(): string
261 {
262 $sender = new static();
263 if (!$sender::isSupported() || !$sender->isRegistered())
264 {
265 return '';
266 }
267
268 \CAgent::AddAgent(static::class . "::registerAutoTemplatesAgent();", 'messageservice', 'N', 10);
269
270 return '';
271 }
272
273 public static function registerAutoTemplatesAgent(): string
274 {
275 if (!Loader::includeModule('messageservice') || !($languageId = Loc::getCurrentLang()))
276 {
277 return '';
278 }
279
280 $notificationsApiClient = new \Bitrix\Messageservice\ApiClient();
281 $listTemplatesResult = $notificationsApiClient->listAutoTemplates($languageId);
282 if ($listTemplatesResult->isSuccess())
283 {
284 $templates = $listTemplatesResult->getData();
286 if (!$sender::isSupported() || !$sender->isRegistered())
287 {
288 return '';
289 }
290
291 foreach ($templates as $template)
292 {
293 $template['EXAMPLES'] = is_array($template['EXAMPLES']) ? $template['EXAMPLES'] : [];
294 $sender->sendTemplate($template['NAME'], $template['CONTENT'], $template['EXAMPLES'], $template['LANGUAGE_ID']);
295 }
296
298 }
299
300 return '';
301 }
302}
$messageFields
Определения callback_ednaru.php:22
static getInstance()
Определения application.php:98
static refreshTemplates(array $templates=[])
Определения templatetable.php:66
int $socketTimeout
Определения base.php:16
int $streamTimeout
Определения base.php:17
Providers Sender $sender
Определения base.php:14
getSentTemplateMessage(string $from, string $to)
Определения ednaru.php:200
getLineId(?int $subjectId=null)
Определения ednaru.php:151
static registerAutoTemplatesAgent()
Определения ednaru.php:273
static checkAutoTemplatesAgent()
Определения ednaru.php:260
WhatsApp EmojiConverter $emoji
Определения ednaru.php:21
setSocketTimeout(int $socketTimeout)
Определения ednaru.php:210
prepareMessageBodyForSave(string $text)
Определения ednaru.php:205
const DISABLE_INTERNATIONAL
Определения ednaru.php:18
setStreamTimeout(int $streamTimeout)
Определения ednaru.php:216
getMessageTemplates(string $subject='')
Определения ednaru.php:190
sendTemplate(string $name, string $text, array $examples=[], ?string $langCode=null)
Определения ednaru.php:222
getTemplatesList(array $context=null)
Определения ednaru.php:177
sendMessage(array $messageFields)
Определения ednaru.php:118
static refreshFromListAgent()
Определения ednaru.php:244
static resolveStatus($serviceStatus)
Определения ednaru.php:146
static isSupported()
Определения ednaru.php:40
getMessageStatus(array $messageFields)
Определения ednaru.php:113
Providers Edna EdnaRu $utils
Определения ednaru.php:20
prepareTemplate($templateData)
Определения ednaru.php:185
static getSenderById($id)
Определения smsmanager.php:158
$template
Определения file_edit.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
$name
Определения menu_edit.php:35
Определения collection.php:2
$text
Определения template_pdf.php:79
$fields
Определения yandex_run.php:501