-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMMM-NotificationTrigger.js
More file actions
91 lines (87 loc) · 2.55 KB
/
MMM-NotificationTrigger.js
File metadata and controls
91 lines (87 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* globals Log Module */
Module.register("MMM-NotificationTrigger", {
defaults: {
useWebhook: false,
triggers: [
{
trigger: "SAMPLE_INCOMING_NOTIFICATION",
triggerSenderFilter: () => true,
triggerPayloadFilter: () => true,
fires: [
{
fire: "SAMPLE_OUTGOING_NOTIFICATION",
payload: payload => payload,
delay: 0,
exec: ""
},
],
},
]
},
start: function () {
this.sendSocketNotification("INIT")
},
socketNotificationReceived: function (notification, payload) {
if (notification === "WEBHOOK" && this.config.useWebhook) {
this.notificationReceived(payload.notification, payload.payload, payload.sender)
}
if (notification === "EXEC_RESULT") {
this.sendNotification(`${payload.fire}_RESULT`, payload)
Log.log("[NOTTRG] Execution Result: ", payload)
}
},
notificationReceived: function (notification, payload, sender) {
const { triggers } = this.config
for (const i in triggers) {
const trigger = triggers[i]
if (notification === trigger.trigger) {
const senderFilter = (trigger.triggerSenderFilter)
? trigger.triggerSenderFilter
: this.defaults.triggers[0].triggerSenderFilter
const payloadFilter = (trigger.triggerPayloadFilter)
? trigger.triggerPayloadFilter
: this.defaults.triggers[0].triggerPayloadFilter
if (senderFilter(sender) && payloadFilter(payload)) {
for (const j in trigger.fires) {
const fire = trigger.fires[j]
let payloadResult = payload
if (typeof fire.payload === "function") {
payloadResult = fire.payload(payload)
}
else if (Object.hasOwn(fire, "payload")) {
payloadResult = fire.payload
}
let execResult = fire.exec
if (execResult && typeof execResult === "function") {
execResult = execResult(payload)
}
if (fire.delay) {
setTimeout((fireName, triggerName, payload, exec) => {
this.sendNotification(fireName, payload)
Log.log(`[NOTTRG] ${fireName} is emitted.`)
if (exec) {
this.sendSocketNotification("EXEC", {
trigger: triggerName,
fire: fireName,
exec: exec
})
}
}, fire.delay, fire.fire, trigger.trigger, payloadResult, execResult)
}
else {
this.sendNotification(fire.fire, payloadResult)
Log.log(`[NOTTRG] ${fire.fire} is emitted.`)
if (execResult) {
this.sendSocketNotification("EXEC", {
trigger: trigger.trigger,
fire: fire.fire,
exec: execResult
})
}
}
}
}
}
}
},
})