A
A
Alexander2021-08-12 18:09:01
Bitrix24
Alexander, 2021-08-12 18:09:01

How to create a webhook for your custom handler in Bitrix24?

I have a boxed version of the Bitrix24 portal. I need to implement a search by Bitrix24 infoblock elements (lists) so that I can send a webhook request with parameters from a third-party site to my Bitrix24 and get the required list of elements from Bitrix24 infoblocks. The problem is that in the standard REST API there is no way to search by the fields or properties of the infoblock elements, and through the standard REST FILTER the result is returned

incorrectly. through the Bitrix API and then return the result as json.
Next, I want to make a webhook that will be attached to this handler.

But I don't know how to make such a webhook correctly, help me figure it out!

There is an instruction on how to make a custom REST handler

. I made a handler according to this instruction and attached a webhook to it, but the webhook returns the following message: {"error":"ERROR_METHOD_NOT_FOUND","error_description":"Method not found!"}

Here is the handler code:

public static function OnRestServiceBuildDescription()
    {
        return array(
            'apitest' => array(
                'api.test.list' => array(
                    'callback' => array(__CLASS__, 'getList'),
                    'options' => array(),
                ),
            )
        );
    }

    public static function getList($query, $nav, \CRestServer $server)
    {
        $navData = static::getNavData($nav, true);

        $res = \Bitrix\Main\UserTable::getList(
            [
                'filter' => $query['filter']?:[],
                'select' => $query['select']?:['*'],
                'order' => $query['order']?:['ID' => 'ASC'],
                'limit' => $navData['limit'],
                'offset' => $navData['offset'],
                'count_total' => true,
            ]
        );

        $result = array();
        while($user = $res->fetch())
        {
            $result[] = $user;
        }

        return static::setNavData(
            $result,
            array(
                "count" => $res->getCount(),
                "offset" => $navData['offset']
            )
        );
    }
}

AddEventHandler('rest', 'OnRestServiceBuildDescription', array('\ApiTest', 'OnRestServiceBuildDescription'));


In the instruction to the method, an array of users is returned. In my course, does the web hook lead to this method at all, or am I doing something right?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
Julia Bedrosova, 2021-08-12
@Bedrosova

Don't know. It works like this for me: https://gist.github.com/BedrosovaYulia/0aa8800b704...
just try to write a simpler example first, so that it stupidly returns some constant, then screw it up. And you did not forget to connect the file with your class in the Internet?

K
Kudis, 2021-08-13
@kudis

It is ideal to make a module and register the handler once during installation

EventManager::getInstance()->registerEventHandler(
    'rest',
    'OnRestServiceBuildDescription',
    $this->MODULE_ID,
    'ApiTest',
    'OnRestServiceBuildDescription'
);

Or register it every time in `/local/php_interface/init.php` via:
AddEventHandler(
    'rest',
    'OnRestServiceBuildDescription',
    ['ApiTest', 'OnRestServiceBuildDescription'],
    false,
    $_SERVER['DOCUMENT_ROOT'] . '/habr/ApiTest.php'
);

just do not forget to write the full path to the class (in the example: `/home/bitrix/www/habr/ApiTest.php`)
And then add your scope (you name it `apitest`) to the permissions of the hook.

V
VadimGeg, 2021-08-20
@VadimGeg

For some reason, my methods are not displayed in the webhook settings, and they are not displayed in scope rights either, I did it according to the instructions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question