Creating a plugin for processing amoCRM webhooks in Joomla

WT Amo CRM library - Documentation
Category: Libraries

How to process amoCRM webhook data in Joomla? Creating a Joomla plugin for processing notifications and data from amoCRM.

If the WT amoCRM library is configured correctly, amoCRM webhooks are sent to a single Joomla entry point in the form <ваш_сайт>/index.php?option=com_ajax&plugin=wt_amocrm&group=system&format=raw&action=webhook&action_type=external&token=. After that, the data is cleared from the GET parameters of the entry point URL: option, plugin, group, format, action, action_type, token.

To process the cleaned data, you need to create your own plugin that will listen to the onAmocrmIncomingWebhook event. Plugin groups are imported before the call:

  1. system
  2. user
  3. amocrm

It is recommended to create plugins in the amocrm group in order not to overload the Joomla event system. In the general Joomla event manager, system plugins are always called before the group plugins. Thus, the system plugin that processes the incoming webhook will be called every time the page is generated and will participate in all Joomla processes, which in this particular case is completely redundant.

Creating the amoCRM webhook processing plugin in Joomla

The process of creating a Joomla plugin has been described many times:

  1. Creating plugins based on the new structure of Joomla 4 (on this site, in Russian)
  2. Creating plugins based on the new structure of Joomla 4 (on Habr, in Russian)
  3. Official Joomla documentation for Developers manual.joomla.org
  4. The book by the Greek Joomla developer Nicholas Dionisopoulos "Joomla Extensions development"
  5. How to trigger events for plugins in the manner of Joomla 5+?

Therefore, the nuances of working with the library will be described here.

Namespace of the plugin

For example, the class name of your plugin customamocrmprocess. Then in the XML manifest of the plugin we specify:

<namespace path="src">Joomla\Plugin\Amocrm\Customamocrmprocess</namespace>

In the plugin class, which will be located in plugins/amocrm/customamocrmprocess/src/Extension/Customamocrmprocess.php specify

<?php

namespace Joomla\Plugin\Amocrm\Customamocrmprocess\Extension;

use Webtolk\Amocrm\Event\WebhookEvent;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Uri\Uri;
use Joomla\Event\SubscriberInterface;

\defined('_JEXEC') or die;

final class Customamocrmprocess extends CMSPlugin implements SubscriberInterface
{

    /**
     * Returns an array of events this subscriber will listen to.
     *
     * @return array
     *
     * @since   5.3.0
     */
    public static function getSubscribedEvents(): array
    {
        return [
            'onAmocrmIncomingWebhook' => 'onAmocrmIncomingWebhook',
        ];
    }

    /**
     * Set as required the passwords fields when mail to user is set to No
     *
     * @param   PrepareFormEvent $event  The event instance.
     *
     * @return  void
     *
     * @since   4.0.0
     */
    public function onAmocrmIncomingWebhook(WebhookEvent $event): void
    {
        // working with data
    }
}

It is assumed that the onAmocrmIncomingWebhook method does not return any data (void), since in response to the notification we can only execute our API request or simply process the data.

The $event argument of onAmocrmIncomingWebhook

The argument $event of the onAmocrmIncomingWebhook method is an object of the \Webtolk\Amocrm\Event\WebhookEvent class located in libraries/Webtolk/Amocrm/src/Event/WebhookEvent.php. This class provides an opportunity to get a php array with data from a webhook. If there is no data, an empty array will be returned.

<?php
// all the webhook raw data if exists
$data = $event->getData();

// Account data if exists
$account = $event->getAccount();

// Contacts/companies data if exists
$contacts = $event->getContacts();

// Leads data if exists
$leads = $event->getLeads();

// Tasks data if exists
$tasks = $event->getTasks();

// Unsorted data if exists
$unsorted = $event->getUnsorted();

// Messages data if exists
$messages = $event->getMessages();

Sample code for your plugin.

<?php

use Webtolk\Amocrm\Event\WebhookEvent;

/**
 * @param   WebhookEvent  $event
 *
 * @return  void
 *
 * @throws  AmocrmClientException
 * @see     WebhookEvent
 * @since   1.3.0
 */
public function onAmocrmIncomingWebhook(WebhookEvent $event): void
{
  
    /** @var array $contacts Array of contacts from webhook if exists */
    $contacts = $event->getContacts();

    if (empty($contacts)) {
        return;
    }
    
    // Do anything with contacts data
}

Of course, no one prevents you from getting data from the Input Joomla object yourself.

<?php
$app = $this->getApplication();

$any_data = $app->getInput()->getString('any_data', 'default_value_if_empty');

 

 

WebTolk Joomla Extensions

96 Extensions
12 Categories
492 Versions released
563060 Downloads
Cart
Cart is empty