3namespace Bitrix\MessageService\Providers\Edna\WhatsApp;
5use Bitrix\Main\Application;
6use Bitrix\Main\Data\Cache;
9use Bitrix\MessageService\Internal\Entity\MessageTable;
10use Bitrix\MessageService\Internal\Entity\TemplateTable;
11use Bitrix\MessageService\Providers;
12use Bitrix\MessageService\Providers\Edna\EdnaUtils;
25 $this->optionManager->getSocketTimeout(),
26 $this->optionManager->getStreamTimeout()
34 if ($this->optionManager->getOption(
'enable_templates_stub',
'N') ===
'Y')
37 return $result->setData($templates);
40 $subjectList = [$subject];
43 $subjectList = $this->optionManager->getOption(
'sender_id', []);
46 $verifiedSubjectIdResult = $this->getVerifiedSubjectIdList($subjectList);
47 if (!$verifiedSubjectIdResult->isSuccess())
49 return $verifiedSubjectIdResult;
52 $verifiedSubjectIdList = $verifiedSubjectIdResult->getData();
56 $cache = Cache::createInstance();
57 if ($cache->initCache(self::CACHE_TIME_TEMPLATES, self::CACHE_KEY_TEMPLATES, self::CACHE_DIR_TEMPLATES))
59 $templates = $cache->getVars();
61 elseif ($cache->startDataCache())
63 foreach ($verifiedSubjectIdList as $subjectId)
66 'subjectId' => $subjectId
69 $templatesRequestResult =
72 if ($templatesRequestResult->isSuccess())
74 $templates = array_merge($templates, $templatesRequestResult->getData());
81 $cache->endDataCache($templates);
85 if ($checkErrors->isSuccess())
93 $result->addErrors($checkErrors->getErrors());
102 if (isset(
$message[
'MESSAGE_HEADERS'][
'template'][
'header'][
'text']))
104 $latestMessage .=
$message[
'MESSAGE_HEADERS'][
'template'][
'header'][
'text'] .
'#BR#';
107 if (isset(
$message[
'MESSAGE_HEADERS'][
'template'][
'text']))
109 $latestMessage .=
$message[
'MESSAGE_HEADERS'][
'template'][
'text'] .
'#BR#';
112 if (isset(
$message[
'MESSAGE_HEADERS'][
'template'][
'footer'][
'text']))
114 $latestMessage .=
$message[
'MESSAGE_HEADERS'][
'template'][
'footer'][
'text'];
117 return $latestMessage;
123 'select' => [
'ID',
'MESSAGE_HEADERS'],
125 '=SENDER_ID' => $this->providerId,
126 '=MESSAGE_FROM' => $from,
127 '=MESSAGE_TO' =>
'+' . $to,
130 'order' => [
'ID' =>
'DESC'],
146 'name' =>
'only text',
147 'channelType' =>
'whatsapp',
150 'attachment' =>
null,
154 'text' =>
"Hello! Welcome to our platform.",
158 'category' =>
'ACCOUNT_UPDATE',
161 'type' =>
'OPERATOR',
162 'createdAt' =>
'2021-07-15T14:16:54.417024Z',
163 'updatedAt' =>
'2021-07-16T13:08:26.275414Z',
167 'name' =>
'text + header + footer',
168 'channelType' =>
'whatsapp',
171 'attachment' =>
null,
175 'text' =>
'Greetings',
177 'text' =>
'Hello! Welcome to our platform.',
179 'text' =>
'Have a nice day',
183 'category' =>
'ACCOUNT_UPDATE',
187 'createdAt' =>
'2021-07-20T09:21:42.444454Z',
188 'updatedAt' =>
'2021-07-20T09:21:42.444454Z',
192 'name' =>
'text + buttons',
193 'channelType' =>
'whatsapp',
196 'attachment' =>
null,
200 'text' =>
"Hello! Welcome to our platform. Have you already tried it?",
208 'buttonType' =>
"QUICK_REPLY",
213 'buttonType' =>
"QUICK_REPLY",
222 'category' =>
'ACCOUNT_UPDATE',
226 'createdAt' =>
'2021-07-20T09:21:42.444454Z',
227 'updatedAt' =>
'2021-07-20T09:21:42.444454Z',
231 'name' =>
'text + button-link',
232 'channelType' =>
'whatsapp',
235 'attachment' =>
null,
239 'text' =>
'Hello! Welcome to our platform. Follow the link bellow to read manuals:',
247 'buttonType' =>
"URL",
248 'url' =>
"https://docs.edna.io/"
255 'category' =>
'ACCOUNT_UPDATE',
259 'createdAt' =>
'2021-07-20T09:21:42.444454Z',
260 'updatedAt' =>
'2021-07-20T09:21:42.444454Z',
265 private function getVerifiedSubjectIdList(
array $subjectList):
Result
268 if (!$channelListResult->isSuccess())
270 return $channelListResult;
273 $filteredSubjectList = [];
274 foreach ($channelListResult->getData() as $channel)
276 if (isset($channel[
'subjectId']) && in_array($channel[
'subjectId'], $subjectList,
true))
278 $filteredSubjectList[] = $channel[
'subjectId'];
283 if (empty($filteredSubjectList))
285 $result->addError(
new Error(
'Verified subjects are missing'));
289 $result->setData($filteredSubjectList);
310 !empty(
$template[
'content'][
'header'][
'text'])
326 !empty(
$template[
'content'][
'footer'][
'text'])
339 return !empty(
$text) && preg_match(
"/{{[\d]+}}/",
$text);
351 $checkResult =
new Result();
356 $exception = new \Bitrix\Main\SystemException(
357 'Incorrect response from the Edna service: ' . var_export($templates,
true)
362 return $checkResult->addError(
363 new Error(
'Incorrect response from the Edna service.', 400, $templates)
373 $filteredTemplates = [];
389 return $filteredTemplates;
395 'filter' => [
'=ACTIVE' =>
'Y']
399 foreach ($autoTemplates as $autoTemplate)
401 $titles[$autoTemplate[
'NAME']] = $autoTemplate[
'TITLE'];
415 'filter' => [
'=ACTIVE' =>
'N']
418 $outdatedTemplates = [];
419 foreach ($outdatedTemplatesResult as $outdatedTemplate)
421 $outdatedTemplates[$outdatedTemplate[
'NAME']] =
true;
424 $activeTemplates = array_filter($templates,
function (
$template) use ($outdatedTemplates) {
428 return array_values($activeTemplates);
433 if (is_null($langCode))
435 $langCode = Application::getInstance()->getContext()->getLanguage();
440 return (
new Result)->addError(
new Error(
'Unknown language code'));
444 if (!$validateTemplateName->isSuccess())
446 return $validateTemplateName;
450 if (!$validateTemplateText->isSuccess())
452 return $validateTemplateText;
455 if (!$validateExamples->isSuccess())
457 return $validateExamples;
460 $subjectList = $this->optionManager->getOption(
'sender_id', []);
461 $verifiedSubjectIdResult = $this->getVerifiedSubjectIdList($subjectList);
462 if (!$verifiedSubjectIdResult->isSuccess())
464 return $verifiedSubjectIdResult;
467 $verifiedSubjectIdList = $verifiedSubjectIdResult->getData();
470 'messageMatcher' => [
473 'language' => $langCode,
474 'category' =>
'UTILITY',
475 'type' =>
'OPERATOR',
476 'contentType' =>
'TEXT',
479 'textExampleParams' => $examples
482 'subjectIds' => $verifiedSubjectIdList,
497 if (mb_strlen(
$text) > 1024)
499 return $result->addError(
new Error(
'The maximum number of characters is 1024'));
502 if (!preg_match(
'/^(?!.* {4,}).*$/ui',
$text))
504 return $result->addError(
new Error(
'The text cannot contain newlines and 4 spaces in a row'));
515 preg_match_all(
'/{{[0-9]+}}/ui',
$text, $variables);
516 if (
count($variables[0]) !==
count($examples))
518 return $result->addError(
new Error(
'The number of examples differs from the number of variables'));
526 $cache = Cache::createInstance();
static getList(array $parameters=array())
validateLanguage(string $langCode)
validateTemplateName(string $name)
getChannelList(string $imType)
hasPlaceholder(string $text)
checkForPlaceholders(array $templates)
checkForErrors(array $templates)
getSentTemplateMessage(string $from, string $to)
getMessageTemplatesStub()
static cleanTemplatesCache()
const CACHE_TIME_TEMPLATES
prepareTemplateMessageText(array $message)
validateTemplateText(string $text)
const CACHE_DIR_TEMPLATES
checkApprovedStatus(array $template)
getMessageTemplates(string $subject='')
excludeOutdatedTemplates(array $templates=[])
sendTemplate(string $name, string $text, array $examples=[], ?string $langCode=null)
initializeDefaultExternalSender()
replaceNameToTitle(array $templates=[])
extractPlaceholders(string $text)
const CACHE_KEY_TEMPLATES
removeUnsupportedTemplates(array $templates)
validateExamples(string $text, array $examples)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"