Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ednaru.php
1<?php
2
4
12
14{
15 public const ID = 'ednaru';
16 public const DISABLE_INTERNATIONAL = 'disable_international';
17
19 protected WhatsApp\EmojiConverter $emoji;
20
21 public function __construct()
22 {
23 $this->informant = new WhatsApp\Informant();
24 $this->optionManager = new Base\Option($this->getType(), $this->getId());
25 $this->utils = new WhatsApp\Utils($this->getId(), $this->optionManager);
26 $this->registrar = new WhatsApp\Registrar($this->getId(), $this->optionManager, $this->utils);
27 $this->initiator = new WhatsApp\Initiator($this->optionManager, $this->registrar, $this->utils, $this->getId());
28 $emojiConverter = new WhatsApp\EmojiConverter();
29 $this->sender = new WhatsApp\Sender($this->optionManager, $this->registrar, $this->utils, $emojiConverter);
30 $this->templateManager = new WhatsApp\TemplateManager($this->getId(), $this->utils, $emojiConverter);
31 }
32
33 public function isAvailable(): bool
34 {
35 return self::isSupported();
36 }
37
38 public static function isSupported()
39 {
40 if (
41 RegionHelper::isInternational()
42 && \COption::GetOptionString('messageservice', self::DISABLE_INTERNATIONAL) === 'Y'
43 )
44 {
45 return false;
46 }
47
48 return parent::isSupported();
49 }
50
51 public function getId(): string
52 {
53 return $this->informant->getId();
54 }
55
56 public function getName(): string
57 {
58 return $this->informant->getName();
59 }
60
61 public function getShortName(): string
62 {
63 return $this->informant->getShortName();
64 }
65
66 public function isRegistered(): bool
67 {
68 return $this->registrar->isRegistered();
69 }
70
71 public function register(array $fields): Result
72 {
73 $result = $this->registrar->register($fields);
74 if ($result->isSuccess())
75 {
76 \Bitrix\Main\Application::getInstance()->addBackgroundJob([$this, 'refreshFromList']);
77 \Bitrix\Main\Application::getInstance()->addBackgroundJob([$this, 'addRefreshFromListAgent']);
78 }
79 return $result;
80 }
81
82 public function getOwnerInfo(): array
83 {
84 return $this->registrar->getOwnerInfo();
85 }
86
87 public function getExternalManageUrl(): string
88 {
89 return $this->registrar->getExternalManageUrl();
90 }
91
92 public function getMessageStatus(array $messageFields): Sender\Result\MessageStatus
93 {
94 return $this->sender->getMessageStatus($messageFields);
95 }
96
97 public function sendMessage(array $messageFields): Sender\Result\SendMessage
98 {
99 return $this->sender->sendMessage($messageFields);
100 }
101
102 public function testConnection(): Result
103 {
104 return (new WhatsApp\ConnectorLine($this->utils))->testConnection();
105 }
106
110 public function getFromList(): array
111 {
112 return $this->initiator->getFromList();
113 }
114
119 public function refreshFromList(): void
120 {
121 $this->utils->updateSavedChannelList($this->initiator->getChannelType());
122 }
123
124 public static function resolveStatus($serviceStatus): ?int
125 {
126 return (new WhatsApp\StatusResolver())->resolveStatus($serviceStatus);
127 }
128
129 public function getLineId(): ?int
130 {
131 return (new WhatsApp\ConnectorLine($this->utils))->getLineId();
132 }
133
134 public function getCallbackUrl(): string
135 {
136 return $this->registrar->getCallbackUrl();
137 }
138
142 public function isTemplatesBased(): bool
143 {
144 return $this->templateManager->isTemplatesBased();
145 }
146
147 public function isCorrectFrom($from)
148 {
149 return $this->initiator->isCorrectFrom($from);
150 }
151
155 public function getTemplatesList(array $context = null): array
156 {
157 return $this->templateManager->getTemplatesList($context);
158 }
159
163 public function prepareTemplate($templateData): array
164 {
165 return $this->templateManager->prepareTemplate($templateData);
166 }
167
168 public function getMessageTemplates(string $subject = ''): Result
169 {
170 return $this->utils->getMessageTemplates($subject);
171 }
172
173 public function getManageUrl(): string
174 {
175 return $this->informant->getManageUrl();
176 }
177
178 public function getSentTemplateMessage(string $from, string $to): string
179 {
180 return $this->utils->getSentTemplateMessage($from, $to);
181 }
182
183 public function prepareMessageBodyForSave(string $text): string
184 {
185 return $this->sender->prepareMessageBodyForSave($text);
186 }
187
189 {
190 $this->optionManager->setSocketTimeout($socketTimeout);
191 return $this;
192 }
193
195 {
196 $this->optionManager->setStreamTimeout($streamTimeout);
197 return $this;
198 }
199
205 public function addRefreshFromListAgent(): void
206 {
207 $cacheManager = new CacheManager($this->getId());
208 $period = (int)ceil( $cacheManager->getTtl(CacheManager::CHANNEL_CACHE_ENTITY_ID) * .9);// async with cache expiration
209
210 \CAgent::AddAgent(static::class . "::refreshFromListAgent();", 'messageservice', 'Y', $period);
211 }
212
217 public static function refreshFromListAgent(): string
218 {
219 $sender = new static();
220 if (!$sender::isSupported() || !$sender->isRegistered())
221 {
222 return '';
223 }
224
225 $sender->refreshFromList();
226
227 return __METHOD__ . '();';
228 }
229}
Providers Sender $sender
Definition base.php:13
getSentTemplateMessage(string $from, string $to)
Definition ednaru.php:178
WhatsApp EmojiConverter $emoji
Definition ednaru.php:19
setSocketTimeout(int $socketTimeout)
Definition ednaru.php:188
setStreamTimeout(int $streamTimeout)
Definition ednaru.php:194
getMessageTemplates(string $subject='')
Definition ednaru.php:168
getTemplatesList(array $context=null)
Definition ednaru.php:155
sendMessage(array $messageFields)
Definition ednaru.php:97
static resolveStatus($serviceStatus)
Definition ednaru.php:124
getMessageStatus(array $messageFields)
Definition ednaru.php:92