Views: 19690
Last Modified: 11.09.2024

To illustrate a fully functional, yet simple bot, we are going to create application that informs users of their overdue tasks. Our chatbot will recognize only one command: what's hot. The range of recognized commands can be extended anytime to get any kind of reports directly to the Bitrix24 chat.

Example of a simple chatbot:

<?php
/**
 * Simple and handy reporting chatbot for bitrix24
 */

$appsConfig     = array();
$configFileName = '/config_' . trim(str_replace('.', '_', $_REQUEST['auth']['domain'])) . '.php';
if (file_exists(__DIR__ . $configFileName)) {
   include_once __DIR__ . $configFileName;
}
// receive event "new message for bot"
if ($_REQUEST['event'] == 'ONIMBOTMESSAGEADD') {
   // check the event - register this application or not
   if (!isset($appsConfig[$_REQUEST['auth']['application_token']])) {
      return false;
   }
   // response time
   $arReport = getAnswer($_REQUEST['data']['PARAMS']['MESSAGE'], $_REQUEST['data']['PARAMS']['FROM_USER_ID']);
   $arReport['attach'][] = array("MESSAGE" => 'As soon as you're through with these tasks, just ask me [send=what's hot]What's hot?[/send] again – I'll give you more to have on your plate!');
   
   // send answer message
   $result = restCommand('imbot.message.add', 
      array(
         "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
         "MESSAGE"   => $arReport['title'] . "\n" . $arReport['report'] . "\n",
         "ATTACH"    => array_merge(
            $arReport['attach']
         ),
      ), 
      $_REQUEST["auth"]);

} // receive event "open private dialog with bot" or "join bot to group chat"
else {
   if ($_REQUEST['event'] == 'ONIMBOTJOINCHAT') {
      // check the event - register this application or not
      if (!isset($appsConfig[$_REQUEST['auth']['application_token']])) {
         return false;
      }
      // send help message how to use chat-bot. For private chat and for group chat need send different instructions.
      $result = restCommand('imbot.message.add', array(
         'DIALOG_ID' => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
         'MESSAGE'   => Hello! I'm Reportoo, I can be a pain in the butt because I'm honest and candid.',
         "ATTACH"    => array(
            array('MESSAGE' => '[send=what's hot]What's hot?[/send]'),
         ),
      ), $_REQUEST["auth"]);

   } // receive event "delete chat-bot"
   else {
      if ($_REQUEST['event'] == 'ONIMBOTDELETE') {
         // check the event - register this application or not
         if (!isset($appsConfig[$_REQUEST['auth']['application_token']])) {
            return false;
         }
         // unset application variables
         unset($appsConfig[$_REQUEST['auth']['application_token']]);
         // save params
         saveParams($appsConfig);

      } // receive event "Application install"
      else {
         if ($_REQUEST['event'] == 'ONAPPINSTALL') {
            // handler for events
            $handlerBackUrl = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://' . $_SERVER['SERVER_NAME'] . (in_array($_SERVER['SERVER_PORT'],
               array(80, 443)) ? '' : ':' . $_SERVER['SERVER_PORT']) . $_SERVER['SCRIPT_NAME'];
            // If your application supports different localizations
            // use $_REQUEST['data']['LANGUAGE_ID'] to load correct localization
            // register new bot
            $result = restCommand('imbot.register', array(
               'CODE'                  => 'ReportBot',
               // app unique string bot ID (required)
               'TYPE'                  => 'B',
               // Bot type: B – robot with immediate response; H – human-like bot with a response delay from 2 to 10 seconds
               'EVENT_MESSAGE_ADD'     => $handlerBackUrl,
               // new message from user" event handler (required)
               'EVENT_WELCOME_MESSAGE' => $handlerBackUrl,
               // "bot invited" event handler (required)
               'EVENT_BOT_DELETE'      => $handlerBackUrl,
               // "user deleted bot" event handler (required)
               'PROPERTIES'            => array( // Chatbot personal data (required)
                  'NAME'              => 'Reportoo',
                  // Bot name (NAME either LAST_NAME is required)
                  'LAST_NAME'         => '',
                  // Bot name (NAME either LAST_NAME is required)
                  'COLOR'             => 'AQUA',
                  // Bot color for mobile app: RED, GREEN, MINT, LIGHT_BLUE, DARK_BLUE, PURPLE, AQUA, PINK, LIME, BROWN, AZURE, KHAKI, SAND, MARENGO, GRAY, GRAPHITE
                  'EMAIL'             => 'no@mail.com',
                  // bot e-mail
                  'PERSONAL_BIRTHDAY' => '2016-03-23',
                  // birthday, YYYY-mm-dd
                  'WORK_POSITION'     => 'I'm here to tell you about your tasks',
                  // Position – used as bot description
                  'PERSONAL_WWW'      => '',
                  // site link
                  'PERSONAL_GENDER'   => 'M',
                  // Bot gender, (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wmYrRSa/6UjU7wvW4kM/XfM9XdEMrNSYCKr98/WviqzEGRep4UpdJLVuGPGRWfVrKWVVTsqG5+KLCNWvtPzfAOI42kZvxsE0AAAAAElFTkSuQmCC',
                  // Bot avatar - base64
               ),
            ), $_REQUEST["auth"]);
            // save params
            $appsConfig[$_REQUEST['auth']['application_token']] = array(
               'BOT_ID'      => $result['result'],
               'LANGUAGE_ID' => $_REQUEST['data']['LANGUAGE_ID'],
            );
            saveParams($appsConfig);
            // write debug log
            // writeToLog($result, 'ReportBot register');
         }
      }
   }
}
/**
 * Save application configuration.
 *
 * @param $params
 *
 * @return bool
 */
function saveParams($params) {
   $config = "<?php\n";
   $config .= "\$appsConfig = " . var_export($params, true) . ";\n";
   $config .= "?>";
   $configFileName = '/config_' . trim(str_replace('.', '_', $_REQUEST['auth']['domain'])) . '.php';
   file_put_contents(__DIR__ . $configFileName, $config);
   return true;
}
/**
 * Send rest query to Bitrix24.
 *
 * @param       $method - Rest method, ex: methods
 * @param array $params - Method params, ex: array()
 * @param array $auth   - Authorize data, ex: array('domain' => 'https://test.bitrix24.com', 'access_token' => '7inpwszbuu8vnwr5jmabqa467rqur7u6')
 *
 * @return mixed
 */
function restCommand($method, array $params = array(), array $auth = array()) {
   $queryUrl  = 'https://' . $auth['domain'] . '/rest/' . $method;
   $queryData = http_build_query(array_merge($params, array('auth' => $auth['access_token'])));
   // writeToLog(array('URL' => $queryUrl, 'PARAMS' => array_merge($params, array("auth" => $auth["access_token"]))), 'ReportBot send data');
   $curl = curl_init();
   curl_setopt_array($curl, array(
      CURLOPT_POST           => 1,
      CURLOPT_HEADER         => 0,
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL            => $queryUrl,
      CURLOPT_POSTFIELDS     => $queryData,
   ));
   $result = curl_exec($curl);
   curl_close($curl);
   $result = json_decode($result, 1);
   return $result;
}
/**
 * Write data to log file.
 *
 * @param mixed  $data
 * @param string $title
 *
 * @return bool
 */
function writeToLog($data, $title = '') {
   $log = "\n------------------------\n";
   $log .= date("Y.m.d G:i:s") . "\n";
   $log .= (strlen($title) > 0 ? $title : 'DEBUG') . "\n";
   $log .= print_r($data, 1);
   $log .= "\n------------------------\n";
   file_put_contents(__DIR__ . '/imbot.log', $log, FILE_APPEND);
   return true;
}
/**
 * Command received, create report
 *
 * @param      string $text message sent by user
  * @param      int $user user ID
 *
 * @return     array
 */
function getAnswer($command = '', $user) {

   switch (strtolower($command)) {
       case 'what's hot':
           $arResult = b24BadTasks($user);
           break;
       default:
          $arResult = array(
            'title' => 'I'm stuck',
            'report'  => 'Sorry, what is it you wanted? I can't hear you!',
         );
   }

   return $arResult;
}

function b24BadTasks ($user) {
   $tasks = restCommand('task.item.list', 
      array(
         'ORDER' => array('DEADLINE' => 'desc'),
         'FILTER' => array('RESPONSIBLE_ID' => $user, '<DEADLINE' => 'Y-m-d'),
         'PARAMS' => array(),
         'SELECT' => array()
      ), 
      $_REQUEST["auth"]);
   
   if (count($tasks['result']) > 0) {
      $arTasks = array();
      foreach ($tasks['result'] as $id => $arTask) {
         $arTasks[] = array(
            'LINK' => array(
               'NAME' => $arTask['TITLE'],
               'LINK' => 'https://'.$_REQUEST['auth']['domain'].'/company/personal/user/'.$arTask['RESPONSIBLE_ID'].'/tasks/task/view/'.$arTask['ID'].'/'
            )
         );
         $arTasks[] = array(
            'DELIMITER' => array(
               'SIZE' => 400,
               'COLOR' => '#c6c6c6'
            )
         );
      }
      $arReport = array(
         'title' => 'Oh yeah, we've got trouble. Tackle these ASAP:',
         'report'  => '',
         'attach' => $arTasks
      );
   }
   else {
      $arReport = array(
         'title' => 'You're awesome!',
         'report'  => 'You're no fun, no tasks overdue. Just joking, keep it up!',
      );
   }
   return $arReport;
}

At a first glance, you may find this code frightening. But do not fret. After you have finished this course, you will see that it's actually quite simple.

Users can create a chatbot for personal use, or publish it on Bitrix24.Market.

Note: Chatbot does not require a HTTPS certificate explicitly. However, it is highly recommended due to potential transfer of personal or sensitive information. The application must be saved in UTF-8 encoding.

To get a feel of what a chatbot application is, without publishing it on Applications24, take the example code, save it to your server and run the bot on your account. Bitrix24 has a local application option and here's how to use it:

  • Select Applications > Developer Resources 1:

  • Proceed to Other 2:

    Click image to expand

  • And then, select Local Application 3:

    Click image to expand

  • Choose the Server application type. Specify the bot name as, for example, "Reportoo". Enable the option Script only (no user interface) and assign access permissions for the app:
    - Chat and Notifications (im): no messages can be sent to a user without this permission,
    - Creating and managing Chat bots (imbot) – chat bot cannot be registered without this permission,
    - Tasks (task) and Tasks (extended permissions) (task_extended) – without this permission, application won't be able to generate task reports to inform the user via chat bot:

    Click image to expand

  • Since we have all the event handlers in one file, specify the same app URL in both fields.
  • In case you create a bot for Bitrix24 On-premise, without the option Script only (no user interface), you need to use the Static application type and the mandatory field Archive containing your application (ZIP) to indicate the path to installation file.

  • Actually, we are done: a message saying a new user (Reportoo) has joined the Bitrix24 account should now be visible in the public chat. We can now add it to the chat, then click the "What's hot" link and get a list of overdue tasks.





    Courses developed by Bitrix24