Поддержка серверов

Bot::register: метод Битрикс

Постоянная ссылка: http://bxapi.ru/src/?id=365303

//    Bitrix\Im\Bot::register()
//    /bitrix/modules/im/lib/bot.php:31

    public static function register(array $fields)
    {
        $code = isset($fields['CODE'])? $fields['CODE']: '';
        $type = in_array($fields['TYPE'], Array(self::TYPE_HUMAN, self::TYPE_BOT, self::TYPE_SUPERVISOR, self::TYPE_NETWORK, self::TYPE_OPENLINE))? $fields['TYPE']: self::TYPE_BOT;
        $moduleId = $fields['MODULE_ID'];
        $installType = in_array($fields['INSTALL_TYPE'], Array(self::INSTALL_TYPE_SYSTEM, self::INSTALL_TYPE_USER, self::INSTALL_TYPE_SILENT))? $fields['INSTALL_TYPE']: self::INSTALL_TYPE_SILENT;
        $botFields = $fields['PROPERTIES'];
        $language = isset($fields['LANG'])? $fields['LANG']: null;

        /* vars for module install */
        $class = isset($fields['CLASS'])? $fields['CLASS']: '';
        $methodBotDelete = isset($fields['METHOD_BOT_DELETE'])? $fields['METHOD_BOT_DELETE']: '';
        $methodMessageAdd = isset($fields['METHOD_MESSAGE_ADD'])? $fields['METHOD_MESSAGE_ADD']: '';
        $methodMessageUpdate = isset($fields['METHOD_MESSAGE_UPDATE'])? $fields['METHOD_MESSAGE_UPDATE']: '';
        $methodMessageDelete = isset($fields['METHOD_MESSAGE_DELETE'])? $fields['METHOD_MESSAGE_DELETE']: '';
        $methodWelcomeMessage = isset($fields['METHOD_WELCOME_MESSAGE'])? $fields['METHOD_WELCOME_MESSAGE']: '';
        $textPrivateWelcomeMessage = isset($fields['TEXT_PRIVATE_WELCOME_MESSAGE'])? $fields['TEXT_PRIVATE_WELCOME_MESSAGE']: '';
        $textChatWelcomeMessage = isset($fields['TEXT_CHAT_WELCOME_MESSAGE'])? $fields['TEXT_CHAT_WELCOME_MESSAGE']: '';
        $openline = isset($fields['OPENLINE']) && $fields['OPENLINE'] == 'Y'? 'Y': 'N';

        /* rewrite vars for openline type */
        if ($type == self::TYPE_OPENLINE)
        {
            $openline = 'Y';
            $installType = self::INSTALL_TYPE_SILENT;
        }

        /* vars for rest install */
        $appId = isset($fields['APP_ID'])? $fields['APP_ID']: '';
        $verified = isset($fields['VERIFIED']) && $fields['VERIFIED'] == 'Y'? 'Y': 'N';

        if ($moduleId == 'rest')
        {
            if (empty($appId))
            {
                return false;
            }
        }
        else
        {
            if (empty($class) || empty($methodMessageAdd))
            {
                return false;
            }
            if (!(!empty($methodWelcomeMessage) || isset($fields['TEXT_PRIVATE_WELCOME_MESSAGE'])))
            {
                return false;
            }
        }

        $bots = self::getListCache();
        if ($moduleId && $code)
        {
            foreach ($bots as $bot)
            {
                if ($bot['MODULE_ID'] == $moduleId && $bot['CODE'] == $code)
                {
                    return $bot['BOT_ID'];
                }
            }
        }

        $userCode = $code? $moduleId.'_'.$code: $moduleId;

        $color = null;
        if (isset($botFields['COLOR']))
        {
            $color = $botFields['COLOR'];
            unset($botFields['COLOR']);
        }

        $userId = 0;
        if ($installType == self::INSTALL_TYPE_USER)
        {
            if (isset($fields['USER_ID']) && intval($fields['USER_ID']) > 0)
            {
                $userId = intval($fields['USER_ID']);
            }
            else
            {
                global $USER;
                if (is_object($USER))
                {
                    $userId = $USER->GetID() > 0? $USER->GetID(): 0;
                }
            }
            if ($userId <= 0)
            {
                $installType = self::INSTALL_TYPE_SYSTEM;
            }
        }

        if (strlen($moduleId) <= 0)
        {
            return false;
        }

        if (!(isset($botFields['NAME']) || isset($botFields['LAST_NAME'])))
        {
            return false;
        }

        $botFields['LOGIN'] = substr(self::LOGIN_START.$userCode.'_'.randString(5), 0, 50);
        $botFields['PASSWORD'] = md5($botFields['LOGIN'].'|'.rand(1000,9999).'|'.time());
        $botFields['CONFIRM_PASSWORD'] = $botFields['PASSWORD'];
        $botFields['EXTERNAL_AUTH_ID'] = self::EXTERNAL_AUTH_ID;

        unset($botFields['GROUP_ID']);

        $botFields['ACTIVE'] = 'Y';

        if (IsModuleInstalled('intranet'))
        {
            $botFields['UF_DEPARTMENT'] = Array(\Bitrix\Im\Bot\Department::getId());
        }
        else
        {
            unset($botFields['UF_DEPARTMENT']);
        }

        $botFields['WORK_POSITION'] = isset($botFields['WORK_POSITION'])? trim($botFields['WORK_POSITION']): '';
        if (empty($botFields['WORK_POSITION']))
        {
            $botFields['WORK_POSITION'] = Loc::getMessage('BOT_DEFAULT_WORK_POSITION');
        }

        $user = new \CUser;
        $botId = $user->Add($botFields);
        if (!$botId)
        {
            return false;
        }

        $result = \Bitrix\Im\Model\BotTable::add(Array(
            'BOT_ID' => $botId,
            'CODE' => $code? $code: $botId,
            'MODULE_ID' => $moduleId,
            'CLASS' => $class,
            'TYPE' => $type,
            'LANG' => $language? $language: '',
            'METHOD_BOT_DELETE' => $methodBotDelete,
            'METHOD_MESSAGE_ADD' => $methodMessageAdd,
            'METHOD_MESSAGE_UPDATE' => $methodMessageUpdate,
            'METHOD_MESSAGE_DELETE' => $methodMessageDelete,
            'METHOD_WELCOME_MESSAGE' => $methodWelcomeMessage,
            'TEXT_PRIVATE_WELCOME_MESSAGE' => $textPrivateWelcomeMessage,
            'TEXT_CHAT_WELCOME_MESSAGE' => $textChatWelcomeMessage,
            'APP_ID' => $appId,
            'VERIFIED' => $verified,
            'OPENLINE' => $openline
        ));

        $cache = \Bitrix\Main\Data\Cache::createInstance();
        $cache->cleanDir(self::CACHE_PATH);

        if ($result->isSuccess())
        {
            if (\Bitrix\Main\Loader::includeModule('pull'))
            {
                $botForJs = self::getListForJs();

                if ($color)
                {
                    \CIMStatus::SetColor($botId, $color);
                }

                $userData = \CIMContactList::GetUserData(array(
                        'ID' => $botId,
                        'DEPARTMENT' => 'Y',
                        'USE_CACHE' => 'N',
                        'SHOW_ONLINE' => 'N',
                        'PHONES' => 'N'
                    )
                );
                \CPullStack::AddShared(Array(
                    'module_id' => 'im',
                    'command' => 'addBot',
                    'params' => Array(
                        'bot' => $botForJs[$botId],
                        'user' => $userData['users'][$botId],
                        'userInGroup' => $userData['userInGroup'],
                    ),
                    'extra' => \Bitrix\Im\Common::getPullExtra()
                ));
                if ($installType != self::INSTALL_TYPE_SILENT)
                {
                    $message = '';
                    if ($installType == self::INSTALL_TYPE_USER && \Bitrix\Im\User::getInstance($userId)->isExists())
                    {
                        $userName = \Bitrix\Im\User::getInstance($userId)->getFullName();
                        $userName = '[USER='.$userId.']'.$userName.'[/USER]';
                        $userGender = \Bitrix\Im\User::getInstance($userId)->getGender();
                        $message = Loc::getMessage('BOT_MESSAGE_INSTALL_USER'.($userGender == 'F'? '_F':''), Array('#USER_NAME#' => $userName));
                    }
                    if (empty($message))
                    {
                        $message = Loc::getMessage('BOT_MESSAGE_INSTALL_SYSTEM');
                    }

                    $attach = new \CIMMessageParamAttach(null, $color);
                    $attach->AddBot(Array(
                        "NAME" => \Bitrix\Im\User::getInstance($botId)->getFullName(),
                        "AVATAR" => \Bitrix\Im\User::getInstance($botId)->getAvatar(),
                        "BOT_ID" => $botId,
                    ));
                    $attach->addMessage(\Bitrix\Im\User::getInstance($botId)->getWorkPosition());

                    \CIMChat::AddGeneralMessage(Array(
                        'MESSAGE' => $message,
                        'ATTACH' => $attach
                    ));
                }
            }

            \Bitrix\Main\Application::getInstance()->getTaggedCache()->clearByTag("IM_CONTACT_LIST");
        }
        else
        {
            $user->Delete($botId);
            $botId = 0;
        }

        return $botId;
    }
';

Блог разработчика

9 августа 2016 ResizeImageGet и простой водяной знак налету

Сегодня поговорим о том как проще всего воткнуть водяной знак на картинки в Битрикс. Например, на все изображения каталога.

21 июля 2016 Добавление вкладки в CRM коробки Битрикс24

В этом посте я покажу, как легко и просто добавить вкладку в сущность CRM. И не просто добавить, а вывести там свой контент.

21 июля 2016 Как просто обращаться из коробки Битрикс24 на сайт на базе Битрикс

К примеру вам хочется внутренним скриптом дернуть с сайта-магазина информацию, которая там, в свою очередь, закрыта некоторыми правами. Например, админскими. Можно конечно мутить свой REST, или иной формат общения, но проще всего воспользоваться штатным инструментом:

Текст сообщения*
Загрузить изображение