Skip to content

Customization

Custom Incoming Message Webhook Task

When a new email arrives at EmailEngine, a webhook will be sent to the email integration to handle it. The handling is executed in a task that could be overriden by a custom one using Application Config.

Getting Started

  1. Generate a new task in your solution using Lime Project.
  2. Register the task to override the standard incoming webhook task by adding it to the Optional Application Configuration.

Example Webhook Messaage

The incoming message looks something like below.

{
  "id": "AAAAAQAACLY",
  "uid": 2230,
  "path": "INBOX",
  "date": "2024-09-05T12:39:38.000Z",
  "flags": [],
  "unseen": true,
  "size": 66202,
  "subject": "Want to buy a pizza [1234]",
  "from": { "name": "Kim Young", "address": "[email protected]" },
  "to": [
    {
      "name": "Support",
      "address": "[email protected]"
    }
  ],
  "messageId": "<AS8PR08MB89955D313A2F291643AD9E6C9R2D2@AS8PR08MB8995.japrd368.prof.inlook.com>",
  "text": {
    "id": "AAAAAQAACLaTkaExkaEykA",
    "encodedSize": { "plain": 31, "html": 1300 }
  },
  "specialUse": "\\Inbox",
  "messageSpecialUse": "\\Inbox",
  "seemsLikeNew": true
}

Note

id and account_id are the internal EmailEngine ids that needs to be passed to the standard email integration import function. messageId is the global unique Message-ID. The text/html of the message is not included for performance reasons.

Example: Pizza Handler

For the solution "Pizza", a new task called incoming was created. It's purpose is to filter out emails containing the word "pizza" in the subject. These should not result in conversations, but rather just create history notes. If no "pizza" is present in the subject, the standard function import_message() that imports messages will be used instead.

Application Configuration

In the Application Configuration, the following parameter was added:

incoming_message_task: solution_pizza.tasks.tasks.incoming

Task Code

solution-pizza/solution_pizza/tasks/tasks.py

from lime_task import task
from limepkg_email.incoming_message import import_message

import logging


logger = logging.getLogger(__name__)


@task
def incoming(app, message, account_id):
    if "pizza" in message["subject"]:
        logger.info("Found pizza in subject, creating history note")

        history = app.limetypes.history()
        history.properties.type.set_by_key("comment")
        history.properties.note.value = (
            f'{message["from"]["name"]} wants to buy a pizza!'
        )
        uow = app.unit_of_work()
        uow.add(history)
        uow.commit()
    else:
        logger.info("No pizza present in subject, starting normal import")

        import_message(
            app,
            message,
            account_id,
        )

Trying It Out

  1. Restart the task handler after every code change.
  2. Send an email with the word "pizza" in the subject and expect a history note to be created.
  3. Send an email without "pizza" in the subject and expect a conversation message to be created.