Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions server/notification-providers/aliyun-sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,33 @@ class AliyunSMS extends NotificationProvider {

try {
if (heartbeatJSON != null) {
const liquid = require("liquidjs");
const engine = new liquid.Liquid();

let statusText = this.statusToString(heartbeatJSON["status"]);
let msgText = heartbeatJSON["msg"];

if (notification.enableCustomTemplate) {
if (notification.statusTemplate) {
statusText = await engine.parseAndRender(
notification.statusTemplate,
{ status: this.statusToString(heartbeatJSON["status"]) }
);
}

if (notification.msgTemplate) {
msgText = await engine.parseAndRender(
notification.msgTemplate,
{ status: this.statusToString(heartbeatJSON["status"]) }
);
}
}

let msgBody = JSON.stringify({
name: monitorJSON["name"],
name: notification.customName || monitorJSON["name"],
time: heartbeatJSON["localDateTime"],
status: this.statusToString(heartbeatJSON["status"]),
msg: heartbeatJSON["msg"],
status: statusText,
msg: msgText,
});
if (await this.sendSms(notification, msgBody)) {
return okMsg;
Expand Down
18 changes: 18 additions & 0 deletions src/components/notifications/AliyunSms.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
<label for="signName" class="form-label">{{ $t("SignName") }}<span style="color: red;"><sup>*</sup></span></label>
<input id="signName" v-model="$parent.notification.signName" type="text" class="form-control" required>

<div class="form-check form-switch mb-3">
<input id="enableCustomTemplate" v-model="$parent.notification.enableCustomTemplate" class="form-check-input" type="checkbox">
<label for="enableCustomTemplate" class="form-check-label">{{ $t("Enable Custom Template") }}</label>
</div>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a quick helptext to explain in which circumstances this setting is usefull?
I think currently a lot of people would not know when to use this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, I will add it later.


<div v-if="$parent.notification.enableCustomTemplate" class="mb-3">
<label for="customName" class="form-label">{{ $t("Custom Name") }}</label>
<input id="customName" v-model="$parent.notification.customName" type="text" class="form-control">
</div>

<div v-if="$parent.notification.enableCustomTemplate" class="mb-3">
<label for="statusTemplate" class="form-label">{{ $t("${status} Template") }}</label>
<textarea id="statusTemplate" v-model="$parent.notification.statusTemplate" class="form-control" rows="2" placeholder="{% if status == 'UP' %}[在线]{% else %}[故障]{% endif %}"></textarea>
Comment on lines +29 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess with the added context that you cannot send a full template, I don't think this makes that much sense anymore to template the status.

Given that aliyun seems to be quite focussed on china, I think simplifying this to just respond with chinese (no translation) is fair.
Alternatively, just providing 3 inputs (up,down,message) are likely better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as only 4 variables can be sent to aliyun API, my initial solution was to forcefully overwrite the original English fields, allowing users to set them to any content. The content of the SMS is mainly to remind operation and maintenance personnel that there is a serious system failure, prompting them to pay attention in a timely manner.


<label for="msgTemplate" class="form-label">{{ $t("${msg} Template") }}</label>
<textarea id="msgTemplate" v-model="$parent.notification.msgTemplate" class="form-control" rows="2" placeholder="{% if status == 'UP' %}请查看钉钉{% else %}请及时处理{% endif %}"></textarea>
Comment on lines +32 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't re-invent the weel here.

Use renderTemplate in the backend and TemplatedTextarea in the frontend.
See the PR I linked previously for an usage example

</div>

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can explain the limitation that templates are fixed and only the content can be changed more clearly.

<div class="form-text">
<p>{{ $t("Sms template must contain parameters: ") }}<br> <code>${name} ${time} ${status} ${msg}</code></p>
<i18n-t tag="p" keypath="Read more:">
Expand Down
Loading