How to make your own chatbot

Lesson 12 out of 29

Bitrix24 boasts its own internal messaging platform, in addition to Open Lines for communicating with clients. Both can benefit from utilizing chatbots.

How to make your own chatbot

4 min

Let's navigate to the "Add Chatbot" section and select the "Notify employees in the chat" template.

Our first step is to integrate a bot into our Bitrix24 by assigning it a name and providing the URL of the listener, where messages will be received from employees.

Accessibility from an external network

It is imperative that the handler's URL is accessible from the external network, avoiding localhost and self-signed SSL certificates, among others. It is advisable to confirm the accessibility of your URL using a third-party service, rather than solely relying on the success of your browser opening the address.

Next, we will choose the type of chatbot. The interface offers four options:

  • An internal messaging chatbot that will respond in real-time,
  • An internal messaging chatbot that will simulate typing a response,
  • A chatbot for Open Lines,
  • And finally, a special type of chatbot commonly utilized as a chatbot shuffler in group chats.

For today's demonstration, let's opt for the first option and press the "Create" button.

After creating the bot, we will encounter new fields, with the most significant being the bot's CLIENT_ID, which will be required for REST API calls on behalf of our chatbot in the future.

We can verify the chatbot's functionality now. The Request Builder section already includes all necessary parameters to send a message to the user from the chatbot using the imbot.message.add method, with the user ID specified in the DIALOG_ID parameter. Executing the request confirms that, as the current user, we have received a message from the chatbot.

We can now download the example from the lesson, upload it to the server in the same manner as before, and insert the code of our current webhook into settings.php.

The downloaded example, along with the standard files of our SDK, will include the handler.php file containing the chatbot's logic, which will be further explained later.

Let us navigate back to the Bitrix24 interface and converse with our chatbot. We initiate the dialogue by sending the greeting, "Hi! How are you?". Our chatbot promptly responds. Perfect! Now it is time to delve into the workings of our chatbot.

To do this, we must examine the code within the file handler.php, as this is the location where messages from users are received.The messages come in the form of POST requests, containing data of a structured format. The log file created by the example code allows us to observe the content of these requests.

As you can see, it is quite simple. Bitrix24 informs us of the event that triggered the handler, in this case, a new message being sent to our chatbot from a portal user. The documentation outlines other events chatbots can receive.

Next, we determine the dialogue in which the communication took place and the user who sent the message to the chatbot. Since in our scenario, the communication takes place in an individual chat, rather than a group chat, the dialog ID is equal to the user ID of the portal.

Finally, we peruse the message we just sent to the chatbot. We can then process that message and prepare an adequate response. In our example, we simply reply to the user with the text of their own message, utilizing the well-known imbot.message.add method.

The code contains the necessary parameters: the chatbot ID, the client_id of the bot, and the dialog number, which is extracted from the incoming data (as there may be multiple portal users communicating with the chatbot in the future). The response text is generated based on the user's message.

In practical applications, you can analyze user messages, dynamically fetch data from external systems to construct the response, and much more. Chatbots provide the ability to automate many routine operations, making them easily accessible to Bitrix24 users.

Resources

Lesson materials: