Skip to content

Custom event handler

You can create a custom event handler to listen to events from Lime CRM, both the built in core events, and events that you define and publish yourself from a custom endpoint or a custom limeobject.

To add a custom event handler to your package, run the following from the root of your package directory:

lime-project generate event-handler

This adds a custom event handler that listens for changes to companies and posts information to a URL if it sees that the name of the company has changed:

import logging
import requests

logger = logging.getLogger(__name__)


def company_renamed(worker, body, message):
    """Summarize your event handlers's functionality here"""
    logger.info('Received message: {}'.format(body))

    if 'name' in body['original_values']:
        logger.info('Company renamed, calling external API')
        data = dict(
            old_name=body['original_values']['name'],
            new_name=body['values']['name'])
        requests.post('https://some.api/company_renamed', json=data)

    message.ack()


def register_event_handlers(worker, config):
    worker.register_event_handler(
        handler_func=company_renamed,
        key='core.limeobject.company.update.v1',
        queue_name='company_renamed')

The register_event_handlers function starts listening to core events that are sent whenever a company is updated, and will call our handler function whenever it receives one.

The comapny_renamed function checks to see if the body of the event received indicates that the name of the company has changed. If so, it extracts the old and new name of the company and post that information to a URL.

Back to top