JS SDK

JS methods and objects for working with amoCRM

This section describes functions and objects that serve to more easily access the environment (information about the widget, the authorized user, etc.), as well as for calling some interface elements.

Pop-up notification

The system has the ability to display a window in the lower right corner with a notification. As an example of use, you can call an incoming call notification by the called telephony.

To implement this function, you can use the provided object. In the example, the function created to work with it.

  1. self.add_call_notify = function(mess){
  2.     var w_name = self.i18n('widget').name,
  3.         date_now = Math.ceil(Date.now()/1000),
  4.         lang = self.i18n('settings'),
  5.         n_data = {
  6.             from: mess.from,
  7.             to: mess.to,
  8.             duration: mess.duration,
  9.             link: mess.link,
  10.             text: w_name + ': ' + mess.text,
  11.             date: date_now
  12.         };
  13.  
  14.     if (mess.element && mess.element.id && mess.element.type){
  15.         n_data.element = mess.element;
  16.     }
  17.    
  18.     AMOCRM.notifications.add_call(n_data);
  19. };
  20.  
  21. /*---------------------------------------*/
  22. var notify_data={};
  23. notify_data.from = '+7 (999) 111 22 33';
  24. notify_data.to = 'User Name';
  25. notify_data.duration = 65;
  26. notify_data.link = 'https://example.com/dialog.mp3';
  27. notify_data.text = 'Widget text';
  28. notify_data.element = { id: 1003619, type: "contact" };
  29.  
  30. self.add_call_notify(notify_data);

The example not only calls the notification box, but also links the link.

Custom search when filling the field type "legal entity"

In the system the opportunity is realized to use custom search of legal entities when filling a field of type legal entity

To implement this function, you can use the provided method. To do this, put in the AMOCRM.widgets.legal_handlers array a function that will return ajax. Ajax should return an array of objects of the following structure:

  1. {
  2.     name: ''QSOFT LLC',
  3.     inn: '5009051111',
  4.     kpp: '500901001',
  5.     ogrn: '1065009000187',
  6.     type: '1',
  7.     address: '222 COLUMBUS AVE SUITE 407 SAN FRANCISCO, CA',
  8. }

Implementation example:

  1. AMOCRM.widgets.legal_handlers = [function(data) {
  2.     var def = $.Deferred();
  3.  
  4.     $.ajax({
  5.         type: 'POST',
  6.         url: 'http://www.example.com/',
  7.         dataType: 'json',
  8.         data: data
  9.     }).done(_.bind(function (response) {
  10.         var res;
  11.        
  12.        res = _.map(response._embedded.items, function(item) {
  13.            return {
  14.                name: item.name,
  15.                inn: item.inn,
  16.                kpp: item.kpp,
  17.                ogrn: item.ogrn,
  18.                type: item.type,
  19.                address: item.address
  20.            };
  21.        });
  22.  
  23.        this.def.resolve(res);
  24.     }, {def: def}))
  25.     .fail(_.bind(def.reject, def, []));;
  26.  
  27.     return def.promise();
  28. }];

Error Notification

We recommend using such notifications if JS on the page performs some background actions (called hidden from the user, not by his call). In such cases, you can notify the user that something went wrong and what actions he needs to take.

For example, you develop a telephony widget and connect to the server in the background to wait for events about incoming calls. At some point you realized that you can not connect to your server and the expected functionality will not work. It is better to notify the user of this error message (with a description of the reasons) with the phone number of technical support.

From the detail of the development of such messages depends on how many calls about incomprehensible user problems will come in technical support. If you display a message to the user that he entered an incorrect password, then the probability of a call with an incomprehensible error is smaller.

The object is very similar to the above notification, but displays an error message. In this case, the closing function of the window behaves differently, it remembers the closing event in the user COOKIE and does not show it any more.

  1. var  errors = AMOCRM.notifications,
  2.     date_now = Math.ceil(Date.now()/1000),
  3.     header = self.get_settings().widget_code,
  4.     text = 'error'
  5.     var n_data = {
  6.                 header: header, // widget code  
  7.                 text:'<p>'+text+'</p>',// error message text
  8.                 date: date_now // /date
  9.             },
  10.     callbacks = { done: function(){console.log('done');}, //successfully added and saved AJAX done
  11.                   fail: function(){console.log('fail');}, //AJAX fail
  12.                   always: function(){console.log('always');} // always called
  13.                 };
  14.  
  15.     errors.add_error(n_data,callbacks);

Consider the parameters in more detail:

  • header - (string) the name of the widget will be displayed in the title

  • text - (string) error message

  • date - date

  • callbacks object callback functions. When you add a new message or AJAX error, the request is sent to the server, which returns the number of this message if the data is successfully saved, depending on the success of the request, one of the transferred functions of this object

The Modal object for working with the modal window

To work with it, it must be connected in the script.js file

This example shows how to use the modal window object Modal

A separate example is given below.

  1. define(['jquery','lib/components/base/modal'], function($, Modal){
  2. var CustomWidget = function () {
  3.   this.callbacks = {
  4.     // . . .
  5.     bind_actions: function(){
  6.     //.  .  .
  7.     var data ='<h1>Test</h1><p>Some text</p>';
  8.   modal = new Modal({
  9.   class_name: 'modal-window',
  10.    init: function ($modal_body) {
  11.    var $this = $(this);
  12.    $modal_body
  13.       .trigger('modal:loaded') // launches a modal window
  14.       .html(data)
  15.       .trigger('modal:centrify')  // configures the modal window
  16.       .append('<span class="modal-body__close"><span class="icon icon-modal-    close"></span></span>');
  17.      },
  18.          destroy: function () {
  19.       }
  20.          });
  21.     //.  .  .
  22.                 return true;
  23.            }
  24.     }
  25.         }
  26.   return CustomWidget;
  27. });

To work with a modal window object, you need to connect it via require (define at the beginning of script.js) and pass parameters: class_name, init (), destroy (). In init, the data to display in the modal window and the trigger events are passed in order to start the methods of the Modal object and display the modal window in the DOM.

Method for obtaining the status of online users

This method allows you to obtain information about the online status of users. The status can be true (if the user is online) or false (if the user is offline).

Getting online status for all users
  1. AMOCRM.sdk.showUserStatus() // object with all user id and status
  2. // Example response:
  3. {
  4.  {
  5.     id: 123456,
  6.    online: true
  7.   },
  8.   {
  9.     id: 123456,
  10.     online: false
  11.   }, ...
  12. }

To get all users and their online statuses, the method is called without a flag and returns all users and their online statuses in the id object.

Getting the id of all users on the network
  1. AMOCRM.sdk.showUserStatus('online')// array of all id users online
  2. // Example response:
  3. [123456, 123457...]

To get a list of all users who are on the network, this method must be called with the flag "online". In this case, it will return an array containing the id of the users on the network.

Getting the status of an online user by his id
  1. var id_user = 123456; // Unique account ID
  2. var status_user = AMOCRM.sdk.showUserStatus(id_user) ; // online user status (true or false)

To get the status of a user by its id, you must call the method with the unique account identifier. This method returns true (if the user is on the network) or false (if the user is not on the network)

Error processing
  1. AMOCRM.notifications.show_user_status(‘someString’) // object with all users id and status

If an incorrect user id was entered, an error was made while writing the flag, an incorrect flag was given, then the function will work as well as without parameters, returning the object with all id and their online statuses.