Skip to content

Commit 77d78b8

Browse files
authored
Merge pull request #90 from petersem:NextVersion-2-13
Added support for Apprise
2 parents bd89867 + ec68643 commit 77d78b8

8 files changed

+123
-30
lines changed

.dev-docker-compose.yml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ services:
1919
CUSTOM_NTFY_SERVER: $CUSTOM_NTFY_SERVER
2020
NTFY_USER: $NTFY_USER
2121
NTFY_PASS: $NTFY_PASS
22+
ports:
23+
- 1212:8000
2224
volumes:
2325
- /var/run/docker.sock:/var/run/docker.sock:ro
2426
restart: unless-stopped

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ RUN apk add --update nodejs npm
1111
COPY --from=builder /usr/src/app /app
1212
WORKDIR /app
1313

14-
EXPOSE 3000
15-
HEALTHCHECK --interval=10s --timeout=5s --retries=3 --start-period=5s CMD wget --spider http://localhost:8000 > /dev/null || exit 1
14+
EXPOSE 8000
15+
HEALTHCHECK --interval=10s --timeout=5s --retries=3 --start-period=5s CMD wget --spider http://localhost:8000/status > /dev/null || exit 1
1616
CMD ["node", "index.js"]

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Slack
1818
- Gotify
1919
- Matrix
20+
- Apprise
2021

2122
## Future Considerations
2223
- Additional messaging platform support
@@ -70,6 +71,8 @@ services:
7071
> - 'slack@bot_user_oauth_token@your_chat_id'
7172
> - 'gotify@server_url@app_token'
7273
> - 'telegram@your_bot_id@your_chat_id'
74+
> - 'apprise@apprise_url@config_id@tag'
75+
> _(Only one tag is supported, and server_avatar is not currently supported)_
7376
> - 'matrix@https://matrix.org@user:matrix.org@access-token@room-id:matrix.org'
7477
> _(For Matrix, add the userid 'without' the leading @ sign. Values are server, userid, access-token, room-id)_
7578
@@ -106,6 +109,7 @@ services:
106109
- For Discord: See Discord doco for how to create a webhook and get the url
107110
- For Slack: See [documentation](doco/slack.md) for how to obtain `ID` values.
108111
- For Ntfy: create a new topic on https://ntfy.sh/app (or your own server), use the name of the topic as follows: ntfy@MY_TOPIC_TITLE
112+
- For Apprise: please review official documentation. [here](https://github.com/caronc/apprise)
109113
- For Matrix, review these images for how to get [userID](https://github.com/petersem/monocker/blob/master/doco/matrix-user-id.png?raw=true), [roomID](https://github.com/petersem/monocker/blob/master/doco/matrix-room-id.png?raw=true), and [Access token](https://github.com/petersem/monocker/blob/master/doco/matrix-access-token.png?raw=true)
110114
111115
## Thank you!

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ services:
1919
# MESSAGE_PLATFORM: 'pushbullet@your_api_key@your_device_id'
2020
# MESSAGE_PLATFORM: 'pushover@your_user_key@your_app_api_token'
2121
MESSAGE_PLATFORM: 'discord@webhook_url'
22+
# MESSAGE_PLATFORM = apprise@apprise_url@config_id@tag
2223
# MESSAGE_PLATFORM: 'gotify@server@app_token'
2324
# MESSAGE_PLATFORM: 'ntfy@topic_title'
2425
# MESSAGE_PLATFORM: 'slack@bot_user_oauth_token@your_chat_id'

index.js

+82-18
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,52 @@ import { NtfyClient } from 'ntfy';
99
import { WebClient } from '@slack/web-api';
1010
import { gotify } from 'gotify';
1111
import Matrix from "matrix-js-sdk";
12+
import express from 'express';
13+
import axios from 'axios';
1214

1315
process.on('warning', (warning) => {
1416
console.log(warning.stack);
1517
});
16-
// for health check
17-
import http from "http";
18-
const host = 'localhost';
19-
const port = 8000;
20-
const requestListener = function (req, res) {
18+
19+
const app = express();
20+
app.use(express.json());
21+
app.use(express.urlencoded({ extended: true }));
22+
23+
// app.get('/', function(req, res) {
24+
// res.writeHead(200);
25+
// res.end("Monocker is functional!");
26+
// });
27+
28+
app.get('/status', function(req, res) {
29+
const status = {'status': 'healthy', 'messages-sent-since-started': messageCountSinceStart }
30+
res.setHeader('Content-Type', 'application/json');
2131
res.writeHead(200);
22-
res.end("Monocker is functional!");
23-
};
24-
const server = http.createServer(requestListener);
25-
server.listen(port, host, () => {});
32+
res.end(JSON.stringify(status) + '\r\n');
33+
});
34+
35+
// Experimental! You must add a "PORTS: - custom_port:8000" entry to your YAML for this to be availlable
36+
app.post('/send', (req, res) => {
37+
const hasValue = (obj, value) => Object.values(obj).includes(value);
38+
const hasKey = (obj, key) => Object.keys(obj).includes(key);
39+
let hasTitle = hasKey(req.body, 'title');
40+
let hasMsg = hasKey(req.body, 'msg');
41+
let title = req.body.title;
42+
let msg = req.body.msg;
43+
44+
if(hasMsg){
45+
// send a message
46+
send(msg, title);
47+
res.writeHead(200);
48+
res.end('Success\n\r');
49+
}
50+
else {
51+
// let's make out there is nothing on this route
52+
res.writeHead(401);
53+
res.end('Page not found\n\r');
54+
}
55+
});
56+
57+
app.listen(8000);
2658

2759
// main program
2860
let docker = new Docker({socketPath: '/var/run/docker.sock'});
@@ -38,6 +70,7 @@ const SHA = process.env.SHA || 'false';
3870
if(process.env.PERIOD == "" || process.env.PERIOD === undefined || process.env.PERIOD < 10) {process.env.PERIOD = 10;}
3971
const PERIOD = process.env.PERIOD;
4072
const DISABLE_STARTUP_MSG = process.env.DISABLE_STARTUP_MSG || 'false';
73+
let messageCountSinceStart = 0
4174

4275
// NTFY settings
4376
const CUSTOM_NTFY_SERVER = process.env.CUSTOM_NTFY_SERVER || null;
@@ -93,20 +126,17 @@ async function sendPushbullet(title, message) {
93126
}
94127

95128
async function sendGotify(title, message) {
96-
// try {
97-
console.log(msgDetails[1]);
98-
console.log(msgDetails[2]);
99-
129+
try {
100130
await gotify({
101131
server: msgDetails[1],
102132
app: msgDetails[2],
103133
title: title,
104134
message: message,
105135
priority: 5
106136
});
107-
// } catch (e) {
108-
// console.error("** Gotify Exception: " + e.message);
109-
// }
137+
} catch (e) {
138+
console.error("** Gotify Exception: " + e.message);
139+
}
110140
}
111141

112142
async function sendPushover(title, message) {
@@ -208,9 +238,40 @@ async function sendSlack(title, message) {
208238
}
209239
}
210240

211-
async function send(message) {
241+
async function sendApprise(aTitle, message) {
242+
let url = msgDetails[1] + '/notify/' + msgDetails[2] + '/?tags=' + msgDetails[3];
243+
244+
245+
await axios.post(url, {
246+
title: aTitle,
247+
body: message,
248+
}, {
249+
headers: {
250+
'Content-Type': 'multipart/form-data'
251+
}
252+
}
253+
)
254+
.then(function (response) {
255+
// console.log(response);
256+
})
257+
.catch(function (error) {
258+
console.log("** Apprise execption" + error);
259+
});
260+
261+
}
262+
263+
264+
async function send(message, extTitle) {
212265
let title = "MONOCKER";
213-
if (SERVER_LABEL.length !== 0) title += " (" + SERVER_LABEL + ")";
266+
if(extTitle != null){
267+
title = extTitle
268+
if (SERVER_LABEL.length !== 0) title += " (" + SERVER_LABEL + ")";
269+
}
270+
else{
271+
if (SERVER_LABEL.length !== 0) title += " (" + SERVER_LABEL + ")";
272+
}
273+
274+
messageCountSinceStart+=1
214275

215276
switch (msgDetails[0].toLowerCase()) {
216277
case "telegram":
@@ -238,6 +299,9 @@ async function send(message) {
238299
case "matrix":
239300
sendMatrix(title, message);
240301
break;
302+
case "apprise":
303+
sendApprise(title, message);
304+
break;
241305
case "default":
242306
// do nothing
243307
break;

node_modules/.package-lock.json

+14-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+16-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monocker",
3-
"version": "2.12.0",
3+
"version": "2.13.0",
44
"description": "Monitors and alerts for docker containers state changes",
55
"type": "module",
66
"main": "index.js",
@@ -29,6 +29,7 @@
2929
"homepage": "https://github.com/petersem/monocker#readme",
3030
"dependencies": {
3131
"@slack/web-api": "^7.0.4",
32+
"axios": "^1.7.2",
3233
"discord-webhook-node": "^1.1.8",
3334
"dockerode": "^4.0.2",
3435
"express": "^4.19.2",

0 commit comments

Comments
 (0)