OnImConnectorMessageAdd Event

Scope: imconnector

Who can subscribe: any user

The OnImConnectorMessageAdd event is triggered when a message is sent from an open line in Bitrix24 to an external system channel via a custom connector.

The event does not trigger:

  • when receiving an incoming message from an external system channel
  • for built-in connectors

After processing the event, call the imconnector.send.status.delivery method to mark the message as delivered in the external system channel.

Events will not be sent to the application until the installation is complete. Check the application installation

What the Handler Receives

Data is transmitted as a POST request

{
            "event": "ONIMCONNECTORMESSAGEADD",
            "event_handler_id": 555,
            "data": {
                "CONNECTOR": "myconnector",
                "LINE": 107,
                "MESSAGES": [
                    {
                        "im": {
                            "chat_id": 1807,
                            "message_id": 86497
                        },
                        "message": {
                            "user_id": 27,
                            "text": "[b]Samantha Johnson:[/b] [br]Good afternoon!"
                        },
                        "chat": {
                            "id": "channel-123"
                        }
                    }
                ]
            },
            "ts": 1773759161,
            "auth": {
                "access_token": "c978b990071b0008440001b0895f697a",
                "expires": 1773762761,
                "expires_in": 3600,
                "scope": "imconnector, imopenlines",
                "domain": "some-domain.bitrix24.com",
                "server_endpoint": "https://oauth.bitrix24.tech/rest/",
                "status": "F",
                "client_endpoint": "https://some-domain.bitrix24.com/rest/",
                "member_id": "bac1cd5c8940947a75e0d71b1a84e348",
                "user_id": 27,
                "application_token": "831c76b092f9f135d9b6b36c3a720757"
            }
        }
        

Parameter
type

Description

event
string

Symbolic event code.

In this case - ONIMCONNECTORMESSAGEADD

event_handler_id
integer

Event handler identifier

data
object

Object with event parameters.

The structure is described below

ts
timestamp

Date and time of the event sent from the event queue

auth
object

Object with authorization parameters and information about the account where the event occurred.

The structure is described below

Data Parameter

Parameter
type

Description

CONNECTOR
string

Connector identifier

LINE
integer

Open line identifier

MESSAGES
object[]

Array of messages.

The structure of the array element is described below

MESSAGES Array Element

Parameter
type

Description

im
object

Object with message identifiers in Bitrix24.

The structure is described below

message
object

Object with message text data.

The structure is described below

chat
object

Chat data in the external system.

The structure is described below

im Parameter

Parameter
type

Description

chat_id
integer

Identifier of the open line chat in Bitrix24

message_id
integer

Identifier of the message in Bitrix24

message Parameter

Parameter
type

Description

user_id
integer

Identifier of the user on whose behalf the message was created

text
string

Message text in the format provided in the event

chat Parameter

Parameter
type

Description

id
string

Identifier of the chat in the external system

auth Parameter

Required parameters are marked with *

Name
type

Description

access_token
string

Authorization token OAuth 2.0

expires_in
integer

Time in seconds until the token expires

scope*
string

Scope under which the event occurred

domain*
string

Address of Bitrix24 where the event occurred

server_endpoint*
string

Address of the Bitrix24 authorization server needed to refresh OAuth 2.0 tokens

status*
string

Status of the application that subscribed to this event:

client_endpoint*
string

Common path for REST API method calls for Bitrix24 where the event occurred

member_id*
string

Identifier of Bitrix24 where the event occurred

refresh_token
string

Token for extending authorization OAuth 2.0

application_token*
string

Token for secure event handling

Authorization tokens are not always passed to the event handler. If the hit that initiated the event could not be linked to a specific Bitrix24 user, the tokens are not passed. Always check the contents of the auth key in the code.

It is recommended to store tokens obtained earlier during the application installation. Use them when working with the application interface in the form of embeds, widgets, and so on.

Continue Learning