├── .gitignore ├── Bot.php ├── BotApp.php ├── Link.php ├── Message.php ├── README.MD └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Composer template 3 | composer.phar 4 | /vendor/ 5 | 6 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 7 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 8 | # composer.lock 9 | ### JetBrains template 10 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 11 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 12 | 13 | # User-specific stuff: 14 | .idea/workspace.xml 15 | .idea/tasks.xml 16 | .idea/dictionaries 17 | .idea/vcs.xml 18 | .idea/jsLibraryMappings.xml 19 | 20 | # Sensitive or high-churn files: 21 | .idea/dataSources.ids 22 | .idea/dataSources.xml 23 | .idea/dataSources.local.xml 24 | .idea/sqlDataSources.xml 25 | .idea/dynamic.xml 26 | .idea/uiDesigner.xml 27 | 28 | # Gradle: 29 | .idea/gradle.xml 30 | .idea/libraries 31 | 32 | # Mongo Explorer plugin: 33 | .idea/mongoSettings.xml 34 | 35 | ## File-based project format: 36 | *.iws 37 | 38 | ## Plugin-specific files: 39 | 40 | # IntelliJ 41 | /out/ 42 | 43 | # mpeltonen/sbt-idea plugin 44 | .idea_modules/ 45 | 46 | # JIRA plugin 47 | atlassian-ide-plugin.xml 48 | 49 | # Crashlytics plugin (for Android Studio and IntelliJ) 50 | com_crashlytics_export_strings.xml 51 | crashlytics.properties 52 | crashlytics-build.properties 53 | fabric.properties 54 | ### OSX template 55 | .DS_Store 56 | .AppleDouble 57 | .LSOverride 58 | 59 | # Icon must end with two \r 60 | Icon 61 | 62 | # Thumbnails 63 | ._* 64 | 65 | # Files that might appear in the root of a volume 66 | .DocumentRevisions-V100 67 | .fseventsd 68 | .Spotlight-V100 69 | .TemporaryItems 70 | .Trashes 71 | .VolumeIcon.icns 72 | 73 | # Directories potentially created on remote AFP share 74 | .AppleDB 75 | .AppleDesktop 76 | Network Trash Folder 77 | Temporary Items 78 | .apdisk 79 | -------------------------------------------------------------------------------- /Bot.php: -------------------------------------------------------------------------------- 1 | code = $code; 162 | $this->type = $type; 163 | $this->event_message_add = $event_message_add; 164 | $this->event_welcome_message = $event_welcome_message; 165 | $this->event_bot_delete = $event_bot_delete; 166 | $this->params = $params; 167 | } 168 | 169 | /** 170 | * Get Bot Data 171 | * 172 | * @return array 173 | */ 174 | public function getData() 175 | { 176 | return [ 177 | 'CODE' => $this->code, 178 | 'TYPE' => $this->type, 179 | 'EVENT_MESSAGE_ADD' => $this->event_message_add, 180 | 'EVENT_WELCOME_MESSAGE' => $this->event_welcome_message, 181 | 'EVENT_BOT_DELETE' => $this->event_bot_delete, 182 | 'PROPERTIES' => $this->params 183 | ]; 184 | } 185 | } -------------------------------------------------------------------------------- /BotApp.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 55 | $this->params = $this->loadParams(); 56 | $this->language = $this->params[$_REQUEST['auth']['application_token']]['LANGUAGE_ID']; 57 | $this->messages = $this->loadMessages(); 58 | } 59 | 60 | /** 61 | * Bot install action 62 | * 63 | * @param Bot $bot 64 | */ 65 | public function install(Bot $bot) 66 | { 67 | $result = $this->call('imbot.register', $bot->getData(), $this->auth); 68 | 69 | $appsConfig = []; 70 | $appsConfig[$_REQUEST['auth']['application_token']] = array( 71 | 'BOT_ID' => $result['result'], 72 | 'LANGUAGE_ID' => $_REQUEST['data']['LANGUAGE_ID'], 73 | ); 74 | 75 | $this->saveParams($appsConfig); 76 | } 77 | 78 | /** 79 | * Bot uninstall action 80 | * 81 | * @return bool 82 | */ 83 | public function uninstall() 84 | { 85 | $configFileName = '/config_' . trim(str_replace('.', '_', $_REQUEST['auth']['domain'])) . '.php'; 86 | 87 | if (file_exists(__DIR__.'/../../../'.$configFileName)) { 88 | unlink($configFileName); 89 | } 90 | 91 | return true; 92 | } 93 | 94 | /** 95 | * Send message action 96 | * 97 | * @param Message $message 98 | * @return mixed 99 | */ 100 | public function send(Message $message) 101 | { 102 | return $this->call('imbot.message.add', $message->getData(), $this->auth); 103 | } 104 | 105 | /** 106 | * Save portal params 107 | * 108 | * @param $params 109 | * @return bool 110 | */ 111 | public function saveParams($params) 112 | { 113 | $config = "language != $this->default_language && !empty($this->messages[$text])) { 130 | return $this->messages[$text]; 131 | } 132 | 133 | return $text; 134 | } 135 | 136 | /** 137 | * API request 138 | * 139 | * @param string $method Method 140 | * @param array $params POST data 141 | * @return mixed 142 | */ 143 | protected function call($method, array $params = array()) 144 | { 145 | $queryUrl = 'https://' . $this->auth['domain'] . '/rest/' . $method; 146 | $queryData = http_build_query(array_merge($params, array('auth' => $this->auth['access_token']))); 147 | 148 | $curl = curl_init(); 149 | curl_setopt_array($curl, array( 150 | CURLOPT_POST => 1, 151 | CURLOPT_HEADER => 0, 152 | CURLOPT_RETURNTRANSFER => 1, 153 | CURLOPT_URL => $queryUrl, 154 | CURLOPT_POSTFIELDS => $queryData, 155 | )); 156 | $result = curl_exec($curl); 157 | curl_close($curl); 158 | $result = json_decode($result, 1); 159 | 160 | return $result; 161 | } 162 | 163 | /** 164 | * Load portal params 165 | * 166 | * @return array 167 | */ 168 | protected function loadParams() 169 | { 170 | $configFileName = '/config_' . trim(str_replace('.', '_', $_REQUEST['auth']['domain'])) . '.php'; 171 | if (file_exists(realpath(__DIR__ . '/../../../' . $configFileName))) { 172 | return include __DIR__ . '/../../../' . $configFileName; 173 | } 174 | 175 | return false; 176 | } 177 | 178 | /** 179 | * Load i18n messages 180 | * 181 | * @return array|mixed 182 | */ 183 | protected function loadMessages() 184 | { 185 | if ($this->language != $this->default_language && file_exists(realpath(__DIR__.'/../../../messages/'.$this->language.'.php'))) { 186 | return include __DIR__.'/../../../messages/'.$this->language.'.php'; 187 | } 188 | 189 | return []; 190 | } 191 | } -------------------------------------------------------------------------------- /Link.php: -------------------------------------------------------------------------------- 1 | dialog_id = $dialog_id; 35 | $this->title = $title; 36 | $this->link = $url; 37 | } 38 | 39 | /** 40 | * Get message data 41 | * 42 | * @return array 43 | */ 44 | public function getData() 45 | { 46 | $return = []; 47 | 48 | if ($this->dialog_id) { 49 | $return['DIALOG_ID'] = $this->dialog_id; 50 | } 51 | 52 | $return['LINK'] = [ 53 | 'LINK' => $this->link, 54 | 'NAME' => $this->title 55 | ]; 56 | 57 | return $return; 58 | } 59 | } -------------------------------------------------------------------------------- /Message.php: -------------------------------------------------------------------------------- 1 | dialog_id = $dialog_id; 42 | $this->message = $message; 43 | $this->attach = $attach; 44 | } 45 | 46 | /** 47 | * Get message data 48 | * 49 | * @return array 50 | */ 51 | public function getData() 52 | { 53 | $return = []; 54 | 55 | if ($this->dialog_id) { 56 | $return['DIALOG_ID'] = $this->dialog_id; 57 | } 58 | 59 | $return['MESSAGE'] = $this->message; 60 | 61 | if ($this->attach){ 62 | $return['ATTACH'] = []; 63 | 64 | foreach ($this->attach as $attach) { 65 | $return['ATTACH'][] = $attach->getData(); 66 | } 67 | } 68 | 69 | return $return; 70 | } 71 | } -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | Bitrix24 Messenger Bot PHP API 2 | ======================== 3 | 4 | This is a PHP implementation for Bitrix24 Messenger Bot API. 5 | 6 | REQUIREMENTS 7 | ------------ 8 | The minimum requirement is that your Web server supports PHP 5.4. 9 | 10 | INSTALLATION 11 | ------------ 12 | 13 | ``` 14 | composer require "pimax/bitrix24-bot-php" "dev-master" 15 | ``` 16 | 17 | BASIC USAGE 18 | ------------ 19 | See this repo - [https://github.com/pimax/bitrix24-bot-php-example](https://github.com/pimax/bitrix24-bot-php-example) 20 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pimax/bitrix24-bot-php", 3 | "description": "Bitrix24 Messenger Bot PHP API", 4 | "homepage": "https://github.com/pimax/bitrix24-bot-php", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Max Pinyugin", 9 | "email": "m.pinyugin@gmail.com", 10 | "homepage": "http://russian-developers.com", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.4.0" 16 | }, 17 | 18 | "autoload": { 19 | "psr-4": { 20 | "pimax\\bitrix24\\": "" 21 | } 22 | } 23 | } --------------------------------------------------------------------------------