55 $res = IntegrationTable::getList(
70 if ($integration = $res->fetch())
73 if ($integration[
'USER_ID'] === $USER->GetID() || \CRestUtil::isAdmin())
76 '=INTEGRATION_ID' => $integration[
'ID']
78 if ($integration[
'BOT_ID'] > 0 && Loader::includeModule(
'im'))
80 $res = BotTable::getList(
83 '=BOT_ID' => $integration[
'BOT_ID'],
87 if ($bot = $res->fetch())
94 '=APPLICATION_TOKEN' => $bot[
'APP_ID'],
99 'BOT_ID' => $integration[
'BOT_ID'],
100 'MODULE_ID' =>
'rest'
105 if (($integration[
'PASSWORD_ID'] > 0) && !static::deleteWebHook($integration[
'PASSWORD_ID']))
107 $errorList[] =
Loc::getMessage(
'INTEGRATION_PRESET_PROVIDER_ERROR_DELETE_WEBHOOK');
109 $resEvent = EventTable::getList(
111 'filter' => $filterEvent,
117 while ($event = $resEvent->fetch())
119 $res = EventTable::delete($event[
'ID']);
120 if (!$res->isSuccess())
122 $errorList[] = $res->getErrorMessages();
125 $res = AppTable::getList(
128 '=ID' => $integration[
'APP_ID']
135 if ($app = $res->fetch())
137 $resPlacement = PlacementTable::getList(
140 '=APP_ID' => $app[
'ID']
147 while ($placement = $resPlacement->fetch())
149 $res = PlacementTable::delete($placement[
'ID']);
150 if (!$res->isSuccess())
152 $errorList[] = $res->getErrorMessages();
155 $res = AppTable::delete($app[
'ID']);
156 if (!$res->isSuccess())
158 $errorList[] = $res->getErrorMessages();
162 if (empty($errorList))
164 $res = IntegrationTable::delete($integration[
'ID']);
165 if (!$res->isSuccess())
167 $errorList[] = $res->getErrorMessages();
173 $errorList[] =
Loc::getMessage(
'INTEGRATION_PRESET_PROVIDER_ERROR_ACCESS_DENIED');
177 if (!empty($errorList))
179 $result[
'result'] =
'error';
180 $result[
'errors'] = $errorList;
183 \Bitrix\Rest\Engine\Access::getActiveEntity(
true);
206 '=ref.ID' =>
'this.APP_ID'
209 'join_type' =>
'LEFT'
214 PasswordTable::class,
216 '=ref.ID' =>
'this.PASSWORD_ID'
219 'join_type' =>
'LEFT'
225 'APPLICATION_DATA_' =>
'APPLICATION_DATA',
226 'PASSWORD_DATA_' =>
'PASSWORD_DATA'
228 if (Loader::includeModule(
'im'))
230 $runtime[] =
new ReferenceField(
234 '=ref.BOT_ID' =>
'this.BOT_ID'
237 'join_type' =>
'LEFT'
240 $select[
'BOT_DATA_'] =
'BOT_DATA';
242 $runtime[] =
new ReferenceField(
246 '=ref.ID' =>
'this.BOT_ID'
249 'join_type' =>
'LEFT'
252 $select[
'BOT_ACCOUNT_NAME'] =
'BOT_ACCOUNT.NAME';
255 $res = IntegrationTable::getList(
262 'runtime' => $runtime
265 if ($item = $res->fetch())
267 if ($item[
'PASSWORD_DATA_ID'] > 0)
269 $item[
'PASSWORD_DATA_URL'] = \CRestUtil::getWebhookEndpoint(
270 $item[
'PASSWORD_DATA_PASSWORD'],
271 $item[
'PASSWORD_DATA_USER_ID']
275 if (!empty($item[
'BOT_DATA_APP_ID']) && mb_strpos($item[
'BOT_DATA_APP_ID'],
'custom') === 0)
278 $item[
'BOT_DATA_APP_ID'] = mb_substr($item[
'BOT_DATA_APP_ID'], 6);
281 if ($item[
'APP_ID'] > 0 && $item[
'APPLICATION_ONLY_API'] ==
'N')
283 $resLang = AppLangTable::getList(
286 '=APP_ID' => $item[
'APP_ID']
290 while ($lang = $resLang->fetch())
292 $item[
'APPLICATION_LANG_DATA'][$lang[
'LANGUAGE_ID']] = $lang[
'MENU_NAME'];
296 if ($item[
'APP_ID'] > 0 && !empty($item[
'WIDGET_LIST']))
298 $resLang = PlacementTable::getList(
301 '=APP_ID' => $item[
'APP_ID'],
309 foreach ($resLang->fetchCollection() as $placement)
311 if (!is_null($placement->getLangAll()))
313 foreach ($placement->getLangAll() as $lang)
315 $item[
'WIDGET_LANG_LIST'][$lang->getLanguageId()] = [
316 'TITLE' => $lang->getTitle(),
317 'DESCRIPTION' => $lang->getDescription(),
318 'GROUP_NAME' => $lang->getGroupName(),
350 $id = (isset($requestData[
'ID']) && intVal($requestData[
'ID']) > 0) ? intVal($requestData[
'ID']) : $id;
351 $userId =
$GLOBALS[
'USER']->getID();
352 $isAdmin = \CRestUtil::isAdmin();
354 $presetData = Element::get($elementCode);
360 $presetData[
'ADMIN_ONLY'] ===
'Y'
361 || $presetData[
'OPTIONS'][
'WIDGET_NEEDED'] !==
'D'
362 || $presetData[
'OPTIONS'][
'APPLICATION_NEEDED'] !==
'D'
366 $result[
'status'] =
false;
367 $result[
'errors'][] =
Loc::getMessage(
'INTEGRATION_PRESET_PROVIDER_ERROR_ACCESS_DENIED');
380 $result[
'status'] =
false;
381 $result[
'errors'][] = $e->getCode() .
': ' . $e->getMessage();
386 $presetData = $presetData[
'OPTIONS'];
389 'ELEMENT_CODE' => $elementCode,
390 'USER_ID' => $USER->GetID(),
391 'TITLE' => $requestData[
'TITLE'],
392 'SCOPE' => is_array($requestData[
'SCOPE']) ? $requestData[
'SCOPE'] : [],
393 'QUERY' => $requestData[
'QUERY'],
394 'OUTGOING_HANDLER_URL' => trim($requestData[
'OUTGOING_HANDLER_URL'] ??
null),
395 'OUTGOING_EVENTS' => is_array($requestData[
'OUTGOING_EVENTS']) ? $requestData[
'OUTGOING_EVENTS'] : [],
396 'APPLICATION_ONLY_API' => (isset($requestData[
'APPLICATION_ONLY_API']) && $requestData[
'APPLICATION_ONLY_API'] ===
'Y') ?
'Y' :
'N',
397 'APPLICATION_NEEDED' => (isset($requestData[
'APPLICATION_NEEDED']) && $requestData[
'APPLICATION_NEEDED'] ===
'Y') ?
'Y' :
'N',
398 'APPLICATION_EVENTS' => (isset($requestData[
'APPLICATION_EVENTS']) && is_array($requestData[
'APPLICATION_EVENTS'])) ? $requestData[
'APPLICATION_EVENTS'] : [],
399 'OUTGOING_NEEDED' => ($requestData[
'OUTGOING_NEEDED'] ===
'Y') ?
'Y' :
'N',
400 'WIDGET_NEEDED' => ($requestData[
'WIDGET_NEEDED'] ===
'Y') ?
'Y' :
'N',
401 'WIDGET_HANDLER_URL' => trim($requestData[
'WIDGET_HANDLER_URL'] ??
null),
402 'WIDGET_LIST' => $requestData[
'WIDGET_LIST'],
403 'WIDGET_LANG_LIST' => (isset($requestData[
'WIDGET_LANG_LIST']) && is_array($requestData[
'WIDGET_LANG_LIST'])) ? $requestData[
'WIDGET_LANG_LIST'] : [],
404 'BOT_HANDLER_URL' => trim($requestData[
'BOT_HANDLER_URL'] ??
null)
409 $itemsEvent = EventTable::getList(
412 '=INTEGRATION_ID' => $id
418 if (!empty($itemsEvent[
'0'][
'APPLICATION_TOKEN']))
420 $saveData[
'APPLICATION_TOKEN'] = $itemsEvent[
'0'][
'APPLICATION_TOKEN'];
424 $saveData[
'APPLICATION_TOKEN'] = Random::getString(32);
430 $saveResult = IntegrationTable::add($saveData);
432 $id = $saveResult->getId();
433 if (!$result[
'status'] = $saveResult->isSuccess())
435 $errorList = $saveResult->getErrorMessages();
439 if ($id > 0 || $result[
'status'])
442 if (!empty($saveData[
'TITLE']))
444 $title = $saveData[
'TITLE'];
448 $title =
Loc::getMessage(
'INTEGRATION_PRESET_PROVIDER_TITLE_PREFIX', [
'#ID#' => $id]);
453 $resIntegration = IntegrationTable::getList(
460 if ($integrationData = $resIntegration->fetch())
462 if (!$isAdmin && $integrationData[
'USER_ID'] != $userId)
464 $result[
'status'] =
false;
465 $result[
'errors'][] =
Loc::getMessage(
'INTEGRATION_PRESET_PROVIDER_ERROR_ACCESS_DENIED');
468 if ($integrationData[
'PASSWORD_ID'] > 0 && ($requestData[
'MODE'] ===
'GEN_SAVE' || $integrationData[
'USER_ID'] != $userId))
472 'integration' . $integrationData[
'ID'],
473 $integrationData[
'ELEMENT_CODE'],
476 if (static::deleteWebHook($integrationData[
'PASSWORD_ID']))
478 $integrationData[
'PASSWORD_ID'] = 0;
482 $result[
'status'] =
false;
483 $errorList[] =
Loc::getMessage(
'INTEGRATION_PRESET_PROVIDER_ERROR_DELETE_WEBHOOK');
488 $saveData = array_merge($integrationData, $saveData);
491 if ($presetData[
'BOT_NEEDED'] !==
'D' && Loader::includeModule(
'im'))
493 if (!empty($requestData[
'BOT_NAME']) && !empty($requestData[
'BOT_HANDLER_URL']))
500 'onImBotMessageUpdate',
501 'onImBotMessageDelete'
504 'TYPE' => $requestData[
'BOT_TYPE'],
505 'EVENT_MESSAGE_ADD' => $saveData[
'BOT_HANDLER_URL'],
506 'EVENT_WELCOME_MESSAGE' => $saveData[
'BOT_HANDLER_URL'],
507 'EVENT_BOT_DELETE' => $saveData[
'BOT_HANDLER_URL'],
508 'MODULE_ID' =>
'rest',
510 'NAME' => $requestData[
'BOT_NAME']
514 if ($integrationData[
'BOT_ID'] > 0)
516 $res = BotTable::getList(
519 '=BOT_ID' => $integrationData[
'BOT_ID'],
523 if ($bot = $res->fetch())
525 $params = array_merge($bot, $params);
528 $clientId = $params[
'APP_ID'];
529 if (!$params[
'APP_ID'])
531 $clientId = Random::getString(32);
532 $params[
'APP_ID'] =
'custom' . $clientId;
533 $params[
'CODE'] = Random::getString(16);
535 elseif (mb_stripos($clientId,
'custom') === 0)
537 $clientId = mb_substr($clientId, 6);
540 $uriWithClientId = $saveData[
'BOT_HANDLER_URL']
541 . (mb_strpos($saveData[
'BOT_HANDLER_URL'],
'?') ===
false ?
'?' :
'&')
542 .
'CLIENT_ID=' . $clientId;
544 'onImBotMessageAdd' => $uriWithClientId,
545 'onImBotJoinChat' => $uriWithClientId,
546 'onImBotDelete' => $uriWithClientId,
548 if (in_array($requestData[
'BOT_TYPE'], [
'S',
'O']))
550 $events[
'onImBotMessageUpdate'] = $uriWithClientId;
551 $events[
'onImBotMessageDelete'] = $uriWithClientId;
552 $params[
'EVENT_MESSAGE_UPDATE'] = $uriWithClientId;
553 $params[
'EVENT_MESSAGE_DELETE'] = $uriWithClientId;
556 if ($integrationData[
'BOT_ID'] > 0)
558 if (
Bot::update([
'BOT_ID' => $integrationData[
'BOT_ID']], $params) ===
true)
560 $botId = $integrationData[
'BOT_ID'];
570 $allEvents = array_change_key_case(array_combine($allEvents, $allEvents), CASE_UPPER);
571 $saveData[
'BOT_ID'] = $botId;
572 if ($integrationData[
'BOT_ID'] > 0)
574 $res = EventTable::getList(
578 '=EVENT_NAME' => array_keys($allEvents),
579 '=APPLICATION_TOKEN' => [
591 while ($event = $res->fetch())
593 if (isset($events[$allEvents[$event[
'EVENT_NAME']]]) &&
594 $event[
'EVENT_HANDLER'] === $events[$allEvents[$event[
'EVENT_NAME']]])
596 unset($events[$allEvents[$event[
'EVENT_NAME']]]);
600 EventTable::delete($event[
'ID']);
604 if ($params[
'APP_ID'])
606 foreach ($events as $event => $eventHandler)
608 $res = EventTable::add(
611 'EVENT_NAME' => toUpper($event),
612 'EVENT_HANDLER' => $eventHandler,
613 'APPLICATION_TOKEN' => $clientId,
617 if ($result[
'status'] = $res->isSuccess())
619 Sender::bind(
'im', $event);
623 $errors = $res->getErrorMessages();
624 if (is_array($errors))
626 $errorList = array_merge($errorList, $errors);
635 $errorList[] =
Loc::getMessage(
'INTEGRATION_PRESET_PROVIDER_ERROR_BOT_CREATE');
641 'INTEGRATION_PRESET_PROVIDER_ERROR_BOT_REQUIRED_FIELD_EMPTY'
647 if (!isset($presetData[
'QUERY_NEEDED']) || $presetData[
'QUERY_NEEDED'] !==
'D')
649 $webhook = static::getWebHook($saveData[
'SCOPE'], $saveData[
'PASSWORD_ID'] ??
null, $title);
650 $saveData[
'PASSWORD_ID'] = $webhook[
'ID'];
654 $presetData[
'OUTGOING_NEEDED'] !==
'D'
655 && !empty($saveData[
'OUTGOING_HANDLER_URL'])
656 && !empty($saveData[
'OUTGOING_EVENTS'])
657 && $saveData[
'OUTGOING_NEEDED'] ==
'Y'
661 $eventUpdateList = [];
662 if (!empty($itemsEvent))
664 $itemsEvent = array_column($itemsEvent,
null,
'EVENT_NAME');
667 foreach ($saveData[
'OUTGOING_EVENTS'] as $event)
669 if ($itemsEvent[$event])
671 $eventUpdateList[] = $itemsEvent[$event][
'ID'];
672 unset($itemsEvent[$event]);
678 'EVENT_NAME' => $event,
679 'EVENT_HANDLER' => $saveData[
'OUTGOING_HANDLER_URL'],
680 'USER_ID' => $userId,
682 'APPLICATION_TOKEN' => $saveData[
'APPLICATION_TOKEN'],
683 'INTEGRATION_ID' => $saveData[
'ID']
688 foreach ($itemsEvent as $event)
690 EventTable::delete($event[
'ID']);
693 foreach ($eventAddList as $item)
695 $res = EventTable::add($item);
696 if (!$result[
'status'] = $res->isSuccess())
698 $errors = $res->getErrorMessages();
699 if (is_array($errors))
701 $errorList = array_merge($errorList, $errors);
707 if ($result[
'status'] && !empty($eventUpdateList))
709 $res = EventTable::updateMulti(
713 'EVENT_HANDLER' => $saveData[
'OUTGOING_HANDLER_URL'],
714 'USER_ID' => $userId,
715 'APPLICATION_TOKEN' => $saveData[
'APPLICATION_TOKEN'],
718 if (!$result[
'status'] = $res->isSuccess())
720 $errors = $res->getErrorMessages();
721 if (is_array($errors))
723 $errorList = array_merge($errorList, $errors);
730 foreach ($itemsEvent as $event)
732 EventTable::delete($event[
'ID']);
737 $presetData[
'WIDGET_NEEDED'] !==
'D'
738 && $saveData[
'WIDGET_NEEDED'] ==
'Y'
739 && !empty($saveData[
'WIDGET_HANDLER_URL'])
740 && is_array($saveData[
'WIDGET_LIST'])
743 $app = static::saveApp(
745 'ID' => $saveData[
'APP_ID'],
747 'URL' => $saveData[
'WIDGET_HANDLER_URL'],
748 'URL_INSTALL' => $saveData[
'WIDGET_HANDLER_URL'],
749 'SCOPE' => $saveData[
'SCOPE'],
752 'APP_NAME' => $saveData[
'TITLE'],
754 'PLACEMENTS' => $saveData[
'WIDGET_LIST'],
755 'PLACEMENTS_LANG_LIST' => $saveData[
'WIDGET_LANG_LIST'],
756 'PLACEMENT_HANDLER_URL' => $saveData[
'WIDGET_HANDLER_URL'],
757 'INTEGRATION_CODE' => $saveData[
'ELEMENT_CODE'],
758 'INTEGRATION_ID' => $saveData[
'ID']
763 $saveData[
'APP_ID'] = $app[
'ID'];
766 if (!empty($app[
'errors']))
768 $errorList = array_merge($errorList, $app[
'errors']);
772 $presetData[
'APPLICATION_NEEDED'] !==
'D'
773 && $saveData[
'APPLICATION_NEEDED'] ==
'Y'
776 $app = static::saveApp(
778 'ID' => $saveData[
'APP_ID'],
780 'URL' => trim($requestData[
'APPLICATION_URL_HANDLER']),
781 'URL_INSTALL' => trim($requestData[
'APPLICATION_URL_INSTALL']),
782 'SCOPE' => $saveData[
'SCOPE'],
783 'ONLY_API' => ($saveData[
'APPLICATION_ONLY_API'] ==
'Y') ?
'Y' :
'N',
784 'MOBILE' => ($saveData[
'APPLICATION_ONLY_API'] !=
'Y'
785 && $requestData[
'APPLICATION_MOBILE'] ===
'Y') ?
'Y' :
'N',
786 'APP_NAME' => $saveData[
'TITLE'],
788 'LANG_NAME' => ($saveData[
'APPLICATION_ONLY_API'] !=
'Y' && is_array($requestData[
'APPLICATION_LANG_NAME'])) ?
789 $requestData[
'APPLICATION_LANG_NAME']
792 'INTEGRATION_CODE' => $saveData[
'ELEMENT_CODE'],
793 'INTEGRATION_ID' => $saveData[
'ID']
799 $saveData[
'APP_ID'] = $app[
'ID'];
802 if (!empty($app[
'errors']))
804 $errorList = array_merge($errorList, $app[
'errors']);
807 elseif (isset($saveData[
'APP_ID']) && $saveData[
'APP_ID'] > 0)
809 $res = AppTable::getList(
812 '=ID' => $saveData[
'APP_ID']
816 if ($app = $res->fetch())
818 AppTable::delete($app[
'ID']);
820 $placementList = PlacementTable::getList(
823 '=APP_ID' => $app[
'ID']
827 if (is_array($placementList))
829 foreach ($placementList as $placement)
831 PlacementTable::delete($placement[
'ID']);
836 $resultSave = IntegrationTable::update($id, $saveData);
837 if (!$resultSave->isSuccess())
839 $errors = $resultSave->getErrorMessages();
840 if (is_array($errors))
842 $errorList = array_merge($errorList, $errors);
850 if (!empty($errorList))
852 $result[
'status'] =
false;
853 $result[
'errors'] = $errorList;
855 \Bitrix\Rest\Engine\Access::getActiveEntity(
true);