Skip to content

Commit

Permalink
Merge pull request #90 from petersem:NextVersion-2-13
Browse files Browse the repository at this point in the history
Added support for Apprise
  • Loading branch information
petersem authored May 24, 2024
2 parents bd89867 + ec68643 commit 77d78b8
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .dev-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ services:
CUSTOM_NTFY_SERVER: $CUSTOM_NTFY_SERVER
NTFY_USER: $NTFY_USER
NTFY_PASS: $NTFY_PASS
ports:
- 1212:8000
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
restart: unless-stopped
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ RUN apk add --update nodejs npm
COPY --from=builder /usr/src/app /app
WORKDIR /app

EXPOSE 3000
HEALTHCHECK --interval=10s --timeout=5s --retries=3 --start-period=5s CMD wget --spider http://localhost:8000 > /dev/null || exit 1
EXPOSE 8000
HEALTHCHECK --interval=10s --timeout=5s --retries=3 --start-period=5s CMD wget --spider http://localhost:8000/status > /dev/null || exit 1
CMD ["node", "index.js"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Slack
- Gotify
- Matrix
- Apprise

## Future Considerations
- Additional messaging platform support
Expand Down Expand Up @@ -70,6 +71,8 @@ services:
> - 'slack@bot_user_oauth_token@your_chat_id'
> - 'gotify@server_url@app_token'
> - 'telegram@your_bot_id@your_chat_id'
> - 'apprise@apprise_url@config_id@tag'
> _(Only one tag is supported, and server_avatar is not currently supported)_
> - 'matrix@https://matrix.org@user:matrix.org@access-token@room-id:matrix.org'
> _(For Matrix, add the userid 'without' the leading @ sign. Values are server, userid, access-token, room-id)_
Expand Down Expand Up @@ -106,6 +109,7 @@ services:
- For Discord: See Discord doco for how to create a webhook and get the url
- For Slack: See [documentation](doco/slack.md) for how to obtain `ID` values.
- 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
- For Apprise: please review official documentation. [here](https://github.com/caronc/apprise)
- 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)
## Thank you!
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ services:
# MESSAGE_PLATFORM: 'pushbullet@your_api_key@your_device_id'
# MESSAGE_PLATFORM: 'pushover@your_user_key@your_app_api_token'
MESSAGE_PLATFORM: 'discord@webhook_url'
# MESSAGE_PLATFORM = apprise@apprise_url@config_id@tag
# MESSAGE_PLATFORM: 'gotify@server@app_token'
# MESSAGE_PLATFORM: 'ntfy@topic_title'
# MESSAGE_PLATFORM: 'slack@bot_user_oauth_token@your_chat_id'
Expand Down
100 changes: 82 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,52 @@ import { NtfyClient } from 'ntfy';
import { WebClient } from '@slack/web-api';
import { gotify } from 'gotify';
import Matrix from "matrix-js-sdk";
import express from 'express';
import axios from 'axios';

process.on('warning', (warning) => {
console.log(warning.stack);
});
// for health check
import http from "http";
const host = 'localhost';
const port = 8000;
const requestListener = function (req, res) {

const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// app.get('/', function(req, res) {
// res.writeHead(200);
// res.end("Monocker is functional!");
// });

app.get('/status', function(req, res) {
const status = {'status': 'healthy', 'messages-sent-since-started': messageCountSinceStart }
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end("Monocker is functional!");
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {});
res.end(JSON.stringify(status) + '\r\n');
});

// Experimental! You must add a "PORTS: - custom_port:8000" entry to your YAML for this to be availlable
app.post('/send', (req, res) => {
const hasValue = (obj, value) => Object.values(obj).includes(value);
const hasKey = (obj, key) => Object.keys(obj).includes(key);
let hasTitle = hasKey(req.body, 'title');
let hasMsg = hasKey(req.body, 'msg');
let title = req.body.title;
let msg = req.body.msg;

if(hasMsg){
// send a message
send(msg, title);
res.writeHead(200);
res.end('Success\n\r');
}
else {
// let's make out there is nothing on this route
res.writeHead(401);
res.end('Page not found\n\r');
}
});

app.listen(8000);

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

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

async function sendGotify(title, message) {
// try {
console.log(msgDetails[1]);
console.log(msgDetails[2]);

try {
await gotify({
server: msgDetails[1],
app: msgDetails[2],
title: title,
message: message,
priority: 5
});
// } catch (e) {
// console.error("** Gotify Exception: " + e.message);
// }
} catch (e) {
console.error("** Gotify Exception: " + e.message);
}
}

async function sendPushover(title, message) {
Expand Down Expand Up @@ -208,9 +238,40 @@ async function sendSlack(title, message) {
}
}

async function send(message) {
async function sendApprise(aTitle, message) {
let url = msgDetails[1] + '/notify/' + msgDetails[2] + '/?tags=' + msgDetails[3];


await axios.post(url, {
title: aTitle,
body: message,
}, {
headers: {
'Content-Type': 'multipart/form-data'
}
}
)
.then(function (response) {
// console.log(response);
})
.catch(function (error) {
console.log("** Apprise execption" + error);
});

}


async function send(message, extTitle) {
let title = "MONOCKER";
if (SERVER_LABEL.length !== 0) title += " (" + SERVER_LABEL + ")";
if(extTitle != null){
title = extTitle
if (SERVER_LABEL.length !== 0) title += " (" + SERVER_LABEL + ")";
}
else{
if (SERVER_LABEL.length !== 0) title += " (" + SERVER_LABEL + ")";
}

messageCountSinceStart+=1

switch (msgDetails[0].toLowerCase()) {
case "telegram":
Expand Down Expand Up @@ -238,6 +299,9 @@ async function send(message) {
case "matrix":
sendMatrix(title, message);
break;
case "apprise":
sendApprise(title, message);
break;
case "default":
// do nothing
break;
Expand Down
18 changes: 14 additions & 4 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monocker",
"version": "2.12.0",
"version": "2.13.0",
"description": "Monitors and alerts for docker containers state changes",
"type": "module",
"main": "index.js",
Expand Down Expand Up @@ -29,6 +29,7 @@
"homepage": "https://github.com/petersem/monocker#readme",
"dependencies": {
"@slack/web-api": "^7.0.4",
"axios": "^1.7.2",
"discord-webhook-node": "^1.1.8",
"dockerode": "^4.0.2",
"express": "^4.19.2",
Expand Down

0 comments on commit 77d78b8

Please sign in to comment.