-
Notifications
You must be signed in to change notification settings - Fork 47
docs: Port "Webhooks" extension page to 7.2 #591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
promptless-for-oss
wants to merge
2
commits into
mautic:7.2
Choose a base branch
from
Promptless:promptless/docs-issue-333-webhooks-7.2
base: 7.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,133 @@ | ||
| Webhooks | ||
| ######## | ||
|
|
||
| Webhooks let Mautic send data to external services through an endpoint URL. Extending them relies on event listeners with two responsibilities: | ||
|
|
||
| #. An event listener that adds Webhook types to the Webhook interface. | ||
| #. An event listener on whichever action should queue a Webhook payload. | ||
|
|
||
| .. vale off | ||
|
|
||
| .. note:: | ||
| In your own bundle, you also need to dispatch your custom event and payload, register your listeners as services, and - in the receiving app - accept the payload. Refer to :ref:`Receiving Webhook payloads<plugin_extensions/webhooks:Receiving Webhook payloads>` for the receiving side. | ||
|
|
||
| .. vale on | ||
|
|
||
| Webhook type listener | ||
| ********************* | ||
|
|
||
| Use the ``WebhookEvents::WEBHOOK_ON_BUILD`` event to add a webhook type to the Webhook interface. Call ``addEvent()`` with your payload event constant and an array containing a label and description: | ||
|
|
||
| .. code-block:: php | ||
|
|
||
| <?php | ||
|
|
||
| namespace MauticPlugin\HelloWorldBundle\EventListener; | ||
|
|
||
| use Mautic\WebhookBundle\Event\WebhookBuilderEvent; | ||
| use Mautic\WebhookBundle\WebhookEvents; | ||
| use MauticPlugin\HelloWorldBundle\HelloWorldEvents; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
|
||
| class WebhookSubscriber implements EventSubscriberInterface | ||
| { | ||
| public static function getSubscribedEvents(): array | ||
| { | ||
| return [ | ||
| WebhookEvents::WEBHOOK_ON_BUILD => ['onWebhookBuild', 0], | ||
| ]; | ||
| } | ||
|
|
||
| public function onWebhookBuild(WebhookBuilderEvent $event): void | ||
| { | ||
| $type = [ | ||
| 'label' => 'mautic.helloworld.webhook.event.type.new', | ||
| 'description' => 'mautic.helloworld.webhook.event.type.new_desc', | ||
| ]; | ||
|
|
||
| $event->addEvent(HelloWorldEvents::ACTION_TO_TRIGGER, $type); | ||
|
|
||
| // You can call addEvent() multiple times to register several types at once. | ||
| } | ||
| } | ||
|
|
||
| ``HelloWorldEvents::ACTION_TO_TRIGGER`` is a constant registered in your bundle. Mautic saves the type in the database and queries for it later, so you must reuse the exact same constant in the payload listener. | ||
|
|
||
| Payload event listener | ||
| ********************** | ||
|
|
||
| Add a listener for the action that should create a payload. Inject ``Mautic\WebhookBundle\Model\WebhookModel`` and call ``queueWebhooksByType()`` with the same event constant, the payload array, and the serialization groups used to format the JSON: | ||
|
|
||
| .. code-block:: php | ||
|
|
||
| <?php | ||
|
|
||
| namespace MauticPlugin\HelloWorldBundle\EventListener; | ||
|
|
||
| use Mautic\LeadBundle\Event\LeadEvent; | ||
| use Mautic\LeadBundle\LeadEvents; | ||
| use Mautic\WebhookBundle\Model\WebhookModel; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
|
||
| class LeadSubscriber implements EventSubscriberInterface | ||
| { | ||
| public function __construct(private WebhookModel $webhookModel) | ||
| { | ||
| } | ||
|
|
||
| public static function getSubscribedEvents(): array | ||
| { | ||
| // Note: this is the same constant registered in the WEBHOOK_ON_BUILD listener. | ||
| return [ | ||
| LeadEvents::LEAD_POST_SAVE => ['onLeadNewUpdate', 0], | ||
| ]; | ||
| } | ||
|
|
||
| public function onLeadNewUpdate(LeadEvent $event): void | ||
| { | ||
| // Only queue a payload when the Contact is new. | ||
| if (!$event->isNew()) { | ||
| return; | ||
| } | ||
|
|
||
| // Serialization groups format the payload JSON; they're defined on your bundle's entity. | ||
| $this->webhookModel->queueWebhooksByType( | ||
| LeadEvents::LEAD_POST_SAVE, | ||
| ['contact' => $event->getLead()], | ||
| ['leadDetails', 'userList', 'publishDetails', 'ipAddress'] | ||
| ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modernized the legacy Source: https://github.com/mautic/mautic/blob/main/app/bundles/WebhookBundle/Model/WebhookModel.php |
||
| } | ||
| } | ||
|
|
||
| The event constant returned in ``getSubscribedEvents()`` must match the type from the type listener exactly, because Mautic uses it in a database query to decide which payloads to include in the POST. Register the listener as a service in your bundle's configuration file. | ||
|
|
||
| .. vale off | ||
|
|
||
| Receiving Webhook payloads | ||
| ************************** | ||
|
adiati98 marked this conversation as resolved.
|
||
|
|
||
| .. vale on | ||
|
|
||
| The content for this page requires a major update. The legacy page contains outdated and potentially inaccurate information. You can still access it in the :xref:`legacy repository`. | ||
| Webhooks send data for Contacts, Points, Email opens, and more to outside applications. Mautic takes the app's endpoint URL and sends a POST request there, including the data relevant to the event that fired. | ||
|
|
||
| If you're interested in helping develop the new content for this page and others, consider joining the documentation efforts. | ||
| To receive the payloads, create a publicly accessible endpoint in your app that accepts a POST request from Mautic. The payload contents vary based on the events the User selects. The following is a sample payload for a new Contact: | ||
|
|
||
| Please read the :xref:`dev docs contributing guidelines` and :xref:`Contributing to Mautic鈥檚 documentation` to get started. | ||
| .. code-block:: json | ||
|
|
||
| .. vale on | ||
| { | ||
| "mautic.lead_post_save_new": [ | ||
| { | ||
| "contact": { | ||
| "id": 1, | ||
| "points": 0, | ||
| "fields": { | ||
| "core": { | ||
| "firstname": { "label": "First Name", "value": "Hello" }, | ||
| "lastname": { "label": "Last Name", "value": "World" }, | ||
| "email": { "label": "Email", "value": "contact@example.com" } | ||
| } | ||
| } | ||
| }, | ||
| "timestamp": "2015-08-18T18:53:33+00:00" | ||
| } | ||
| ] | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ported the page structure and prose (Webhook type listener, payload listener, receiving payloads) from the legacy
_plugin_extending_webhooks.mdsource referenced by porting issues #333/#332.Source: https://github.com/mautic/developer-documentation/blob/main/source/includes/_plugin_extending_webhooks.md