Views: 1266 (Data available since 06.02.2017)

The most important thing to remember about chatbots is their logic which is typically a reaction to an action performed by a user or system.

The chatbot API includes the six events that cover the required range of responses:

  • ONAPPINSTALL - event to install a chatbot application.
  • ONAPPUPDATE - updates the chatbot application.
  • ONIMJOINCHAT - event initiated when a chatbot is invited to conversation, either by a user's call in a private chat, or when the bot is connected to a group chat.
  • ONIMBOTMESSAGEADD - event initiated when a chatbot is invited to conversation, either by a user's call in a private chat, or when the bot is connected to a group chat.
  • ONIMCOMMANDADD - event initiated after a user sends a command to a bot in a private or group chat (chatbot cannot participate in the chat, if the command is global).
  • ONIMBOTDELETE - event initiated after the chatbot application is deleted. The event is initiated in parallel with OnAppUninstall.

All we have to do to implement the required chatbot logic is to add event handlers:

  1. Register the chatbot when installing it on the portal.
  2. Show a welcome message when the bot is invited to the chat.
  3. Analyze user requests and send something back in return. Where analysis means a simple "command line review", and not the lexical breakdown of natural language.

To fulfill the requirements, the chatbot REST API contains simple methods a developer can use. Only the two of those are needed to get started:

In the ONAPPINSTALL event handler, we are going to call the imbot.register method to add the chatbot to the current portal. When out chatbot receives an invitation via the ONIMJOINCHAT event, a call to the imbot.message.add method will show chatbot reference information. Finally, we will use the ONIMBOTMESSAGEADD method in the imbot.message.add event handler to reply to the user. It could not be easier, could it?

Another treat for a lazy developer: we don't have to implement OAuth 2.0 in our application because all the authentication parameters are passed to event handlers in the $_REQUEST array.

Note: for a full list of Chatbot API events and methods, please use this link.





Courses developed by «Bitrix», Inc.