notifo-io / notifo

Multi channel notification service for collaboration tools, e-commerce, news service and more.
MIT License
739 stars 69 forks source link

Send email template but custom properties are not filled #240

Closed krissus closed 3 months ago

krissus commented 3 months ago

Hi, I've started using email templates with Notifo.... but I'm facing and issue that the custom properties inside the email template are not replaced with the dynamic value. To check what is going on I've check on the templates what kind of the data I can use like below

      <mj-text>
        List of notifications <br />
        {{ notifications | json }}
      </mj-text>

and I'm getting some information but I can't see properties values which was send by creating an event

Properties from the event

{
  "id": "b44f2a30-9360-4a38-be29-151e2c387414",
  "topic": "users/6606a7653e7f515cdd92d1a0",
  "creatorId": "6606a7653e7f515cdd92d1a0",
  "displayName": "EN subject - ",
  "data": "sdfksdlfk: sdflsklfk; slskflsdkf:32409420",
  "created": "2024-06-11T09:17:14.114Z",
  "formatting": {
    "subject": {
      "en": "EN subject - ",
      "pl": "PL subject - "
    },
    "body": {
      "en": "EN subject - ",
      "pl": "PL subject - "
    },
    "confirmLink": {},
    "confirmText": {},
    "imageSmall": {},
    "imageLarge": {},
    "linkUrl": {},
    "linkText": {},
    "confirmMode": "None"
  },
  "settings": {
    "email": {
      "send": "Send",
      "condition": "Always",
      "required": "Inherit",
      "template": "KK-Test-email-template",
      "properties": {
        "myVariable": "this is test property",
        "email_id": "test@dddddd.pl",
        "support_contact_no": "support@ddddddd.pl",
        "email_title": "This is my title"
      }
    },
    "webpush": {
      "send": "Inherit",
      "condition": "Inherit",
      "required": "Inherit"
    },
    "webhook": {
      "send": "Inherit",
      "condition": "Inherit",
      "required": "Inherit"
    }
  },
  "properties": {
    "myVariable": "this is a test property",
    "email_id": "test@test.pl",
    "support_contact_no": "support@yrdy.pl",
    "email_title": "My title"
  },
  "scheduling": {
    "type": "UTC",
    "time": "00:00:00"
  },
  "counters": {
    "email_handled": 1,
    "web_handled": 1,
    "webpush_handled": 1
  },
  "silent": false
}

And what I see when I try to debug on UI

APP{"Name":"Notifo"}
--
USERS
{"FullName":"Krrissus","EmailAddress":"kr#####@domain.com"}

APP
{"Name":"Notifo"}

List of notifications
[{"TrackSeenUrl":"https://xxxxx/api/tracking/notifications/43242280-5ce1-436e-bec9-cf6ed0d0e074/seen?culture=en\u0026channel=email\u0026configurationId=647bd2ff-d0dd-4162-add4-32e61713cf9c","TrackDeliveredUrl":"https://xxxxx/api/tracking/notifications/43242280-5ce1-436e-bec9-cf6ed0d0e074/delivered?culture=en\u0026channel=email\u0026configurationId=647bd2ff-d0dd-4162-add4-32e61713cf9c","ConfirmUrl":"","Children":[],"Subject":"EN subject - ","Body":"EN subject - "}]

Maybe I've configured system wrongly but I can't present custom properties on email templates. Could you help with this? Currently I've installed versiion from main branch.

SebastianStehle commented 3 months ago

Liquid uses specialized models, e.g. this one: https://github.com/notifo-io/notifo/blob/main/backend/src/Notifo.Domain/Liquid/LiquidNotification.cs

So I think you are true, that custom properties are not part of that yet. But it should be easy to change.

krissus commented 3 months ago

I think that Property options can work, but here https://github.com/notifo-io/notifo/blob/534ec69c09039bd5808641d17b378c0a627a3244/backend/src/Notifo.Domain/Liquid/LiquidContext.cs#L60C1-L60C75

code is converting values from the Properties dictionary to notification.custom.{key} and IMO the Liquid cannot convert variable on template ie. notification.custom.myVariable because it's searching for the object: notification -> custom -> myVariable.

When I've changed from notification.custom.{key} to notification_custom_{key} and in template I'm using ie. {{ notification_custom_myVariable }} I see the variable that I've sent during the event was created.

Request - api/apps/00000000-0000-0000-0000-000000000000/events

{
    "requests": [
        {
            "topic": "users/6606axxxd1a0",
            "creatorId": "6606axxxx2d1a0",
            "settings": {
                "email": {
                    "send": "Send",
                    "condition": "Always",
                    "template": "KK-Test-email-template"
                },
                "webpush": {
                    "send": "Inherit",
                    "condition": "Inherit"
                },
                "webhook": {
                    "send": "Inherit",
                    "condition": "Inherit"
                }
            },
            "preformatted": {
                "subject": {
                    "en": "EN subject - {{ notification.custom.myVariable }}"
                },
                "body": {
                    "en": "EN subject - {{ notification.custom.email_title }}"
                },
                "test":{
                    "en": "To jest test property TEST EN"
                },
                "confirmMode": "None"
            },
            "properties": {
                "myVariable": "This is myVariable 2",
                "email_id": "emailid2@domain.com",
                "support_contact_no": "support-email2@support-contact-no.com",
                "email_title": "This is my Title2"
            },
            "scheduling": {
                "type": "UTC",
                "time": "00:00:00"
            },
            "data":"",
            "silent": false,
            "test": false
        }
    ]
}

Email template

<mj-text>
          EN Dear {{ user.fullname | default: 'User' }} <br />
         <br />

         Custom_MyVariable {{ notification_custom_myVariable }} <br />

         WORKING Section <br />
          OLD Custom_MyVariable {{ notification.custom.myVariable }} <br />
          Custom_MyVariable {{ notification_custom_myVariable }} <br />
          Custom Support Contact Number: {{notification_custom_support_contact_no}} <br />
          Custom Email ID {{notification_custom_email_id}} <br />
          Custom Email Title {{notification_custom_email_title}} <br />

         <br />
         <br />
         <br />
        </mj-text>

Last email what I've received

EN Dear KK

Custom_MyVariable This is myVariable 2
WORKING Section
OLD Custom_MyVariable
Custom_MyVariable This is myVariable 2
Custom Support Contact Number: support-email2@support-contact-no.com
Custom Email ID emailid2@domain.com
Custom Email Title This is my Title2

So I don't know if the switching from . to _ here won't destroy other features. What do you think @SebastianStehle ?

SebastianStehle commented 3 months ago

I think I will have to write a test for that. But you are probably right

SebastianStehle commented 3 months ago

I have published an update for that.