Implement template engine for message [On Hold for #760]
#751
Open
deefdragon wants to merge 6 commits from deefdragon/Template-Engine
into master
pull from: deefdragon/Template-Engine
merge into: topaLE:master
topaLE:1.6.X
topaLE:1.7.X
topaLE:1.9.X
topaLE:Mikhail5555/feature/remote-header-auth
topaLE:Nuckerr/master
topaLE:Saibamen/fix_871
topaLE:WillianRod/feat/add-favicon-badges
topaLE:andreasbrett/logging
topaLE:andreasbrett/securepush
topaLE:bertyhell/bugfix/heartbeat-bar-animation
topaLE:bertyhell/feature/monitor-checks
topaLE:bertyhell/feature/translations-extraction-script
topaLE:cert-notification
topaLE:chakflying/settings-redesign
topaLE:debian-docker
topaLE:deefdragon/notif-tests
topaLE:e2e-test
topaLE:fdcastel/push-api-tags
topaLE:free-disk-space
topaLE:ivanbratovic/http-basicauth
topaLE:ivanbratovic/improve-translatables
topaLE:k8s-unofficial
topaLE:lucasra1/overall_status
topaLE:master
topaLE:mhkarimi1383/master
topaLE:mrphuongbn/master
topaLE:no-need-build
topaLE:philippdormann/feature/release-management
topaLE:proffalken/feature/680_add_labels_to_prometheus_metrics
topaLE:proffalken/feature/auto_build_and_release
topaLE:rebasesoftware/feature/request-with-http-proxy
topaLE:restructure-status-page
topaLE:sqlite-upgrade-prebuilt
topaLE:tarun7singh/master
topaLE:thomasleveil/feature/565-duplicate-monitor
topaLE:thomasleveil/ux/add-group-at-the-top
Reviewers
Request review
No reviewers
Labels
Something isn't working dependencies
Pull requests that update a dependency file discussion doc
Improvements or additions to documentation duplicate
This issue or pull request already exists feature-request
New feature or request good first issue
Good for newcomers hacktoberfest hacktoberfest-accepted help help wanted
Extra attention is needed High
High Priority impossible invalid
This doesn't seem right investigating k8s Low
Low Priority Medium
Medium Priority News prerelease bug question
Further information is requested resolved Unknown wontfix
This will not be worked on
Apply labels
Clear labels
bug
Something isn't working dependencies
Pull requests that update a dependency file discussion doc
Improvements or additions to documentation duplicate
This issue or pull request already exists feature-request
New feature or request good first issue
Good for newcomers hacktoberfest hacktoberfest-accepted help help wanted
Extra attention is needed High
High Priority impossible invalid
This doesn't seem right investigating k8s Low
Low Priority Medium
Medium Priority News prerelease bug question
Further information is requested resolved Unknown wontfix
This will not be worked on
No Label
bug
dependencies
discussion
doc
duplicate
feature-request
good first issue
hacktoberfest
hacktoberfest-accepted
help
help wanted
High
impossible
invalid
investigating
k8s
Low
Medium
News
prerelease bug
question
resolved
Unknown
wontfix
Milestone
Set milestone
Clear milestone
No items
No Milestone
Projects
Clear projects
No project
Assignees
Assign users
Clear assignees
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.
No due date set.
Dependencies
This pull request currently doesn't have any dependencies.
Reference in new issue
There is no content yet.
Delete Branch 'deefdragon/Template-Engine'
Deleting a branch is permanent. It CANNOT be undone. Continue?
No
Yes
Template engine implementation for #646.
The current method of implementation only shows the template options when it has been specifically enabled for that notification type. This is to allow testing of templates for each notification method individually.
For all existing notifications, because the option is empty, it uses the "low detail" template, which is the current message received from most notifications. This is also the default value in the drop-down for newly created templates.
I realized while writing this that it might be best to pass the templates in via the front-end instead of just their name as that would allow them to easily be added to the translation files, and make it easier to modify the templates available when based on the notification (adding specific templates for discord detail levels for example). The current templates are also only starting points, and I feel not the best matches to their namesakes, so I welcome input there.
The testing of a notification now passes in a test heartbeat and monitor, but it is not perfect. They should ideally loop through different values so that the user can test complex templates easily.
The generation of the template argument really should be pulled out to it's own function. Specifically to have the most common keys such as
name
andhealth
to not requiremonitor.name
andmonitor.health
etc. as well as to add data for the names used in #627 for compatibility. This would also move the "healthy/unhealthy" to a set location.What the UI looks like adding the template, Added before the notification specific options.

The method signature should not be changed.
As I said in: https://github.com/louislam/uptime-kuma/issues/646#issuecomment-939725564
This is used for general message.
Thanks, haven't checked it in detail, but remember to enable eslint autofix to fix the format issues.
I have been wondering about that. There were only two calls to
send
, and now the test call has example monitors and heartbeats. As such, there are no calls for a general message. Can you expand on what you plan on using that for beyond the test message?I wonder especially given the number of special cases that pop up around the more complicated notifiers, if it would not be better to split out those calls explicitly instead of having one function with logic for both cases.
Notification.notify(notification,monitor,heartbeat)
for notification dataNotification.message(notification,message)
for simple messagesIt would make it a larger change, but given I am already going to have to go through basically every notification type to verify/test the templates and notifications anyway, I don't think that it would be all that much more.
You mentioned that it would be impossible to write tests of the different notifications. Could you expand on why?
Are you referring to just e2e tests? I believe it will be easy enough to write unit tests for the notifications.
Planning for more notification such as domain renewal, cert renewal remind.
IMO if the function name is
send
, then it should really be as accommodating as possible with respect to what can be sent, so yeah it would be a problem if in the future there are other things to send. Keep extendingsend()
with more parameters doesn't seem like a good practice anyway. I haven't really thought about this, but is it possible that send is justsend(notification, httpBody)
, then we can have a method for each type of notification which wraps thesend()
call, maybe something likesendDown()
sendUp()
sendCertExp()
etc?So I strongly recommend that we should think and write a complete test first before implementing this function.
That is completely fair. I am working on writing some tests for the SMTP notification. I think that I am close to an example. If you think it works, Ill start on the other notifications. Ill throw another PR out with it shortly for examination.
While I am looking at it, question: what do we want the response to be in the case of a template build/parse error?
Just return that template error?
Attempt to build the default message, and then send it to the notification, returning that "error" (or lack of)?
Attempt to build&send default, then combine the errors if one shows up?
Putting on hold for implementation of the notification tests: #760