Skip to content

Commit

Permalink
Merge pull request #88 from oliveratgithub/main
Browse files Browse the repository at this point in the history
How-to customize template.js and fully leverages dockerode with customizable connect text
  • Loading branch information
luc-ass authored Dec 13, 2024
2 parents d3d4b29 + e21772c commit 328fd49
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Docker Telegram Notifier
# [![GitHub Workflow Status](https://shields.api-test.nl/github/workflow/status/luc-ass/docker-telegram-notifier/docker-build?logo=github&logoColor=white&style=for-the-badge)](https://github.com/luc-ass/docker-telegram-notifier/actions) [![Docker Pulls](https://img.shields.io/docker/pulls/lorcas/docker-telegram-notifier?logo=docker&style=for-the-badge)](https://hub.docker.com/r/lorcas/docker-telegram-notifier) [![Docker Image Version (latest semver)](https://img.shields.io/docker/v/lorcas/docker-telegram-notifier?logo=docker&style=for-the-badge)](https://github.com/luc-ass/docker-telegram-notifier/releases) [![Renovate](https://img.shields.io/badge/renovate-enabled-green?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjUgNSAzNzAgMzcwIj48Y2lyY2xlIGN4PSIxODkiIGN5PSIxOTAiIHI9IjE4NCIgZmlsbD0iI2ZlMiIvPjxwYXRoIGZpbGw9IiM4YmIiIGQ9Ik0yNTEgMjU2bC0zOC0zOGExNyAxNyAwIDAxMC0yNGw1Ni01NmMyLTIgMi02IDAtN2wtMjAtMjFhNSA1IDAgMDAtNyAwbC0xMyAxMi05LTggMTMtMTNhMTcgMTcgMCAwMTI0IDBsMjEgMjFjNyA3IDcgMTcgMCAyNGwtNTYgNTdhNSA1IDAgMDAwIDdsMzggMzh6Ii8+PHBhdGggZmlsbD0iI2Q1MSIgZD0iTTMwMCAyODhsLTggOGMtNCA0LTExIDQtMTYgMGwtNDYtNDZjLTUtNS01LTEyIDAtMTZsOC04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PC9zdmc+)](https://renovatebot.com)
# Docker Telegram Notifier
# [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/luc-ass/docker-telegram-notifier/docker-image.yml?branch=main&style=for-the-badge)](https://github.com/luc-ass/docker-telegram-notifier/actions) [![Docker Pulls](https://img.shields.io/docker/pulls/lorcas/docker-telegram-notifier?logo=docker&style=for-the-badge)](https://hub.docker.com/r/lorcas/docker-telegram-notifier) [![Docker Image Version (latest semver)](https://img.shields.io/docker/v/lorcas/docker-telegram-notifier?logo=docker&style=for-the-badge)](https://github.com/luc-ass/docker-telegram-notifier/releases) [![Renovate](https://img.shields.io/badge/renovate-enabled-green?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjUgNSAzNzAgMzcwIj48Y2lyY2xlIGN4PSIxODkiIGN5PSIxOTAiIHI9IjE4NCIgZmlsbD0iI2ZlMiIvPjxwYXRoIGZpbGw9IiM4YmIiIGQ9Ik0yNTEgMjU2bC0zOC0zOGExNyAxNyAwIDAxMC0yNGw1Ni01NmMyLTIgMi02IDAtN2wtMjAtMjFhNSA1IDAgMDAtNyAwbC0xMyAxMi05LTggMTMtMTNhMTcgMTcgMCAwMTI0IDBsMjEgMjFjNyA3IDcgMTcgMCAyNGwtNTYgNTdhNSA1IDAgMDAwIDdsMzggMzh6Ii8+PHBhdGggZmlsbD0iI2Q1MSIgZD0iTTMwMCAyODhsLTggOGMtNCA0LTExIDQtMTYgMGwtNDYtNDZjLTUtNS01LTEyIDAtMTZsOC04YzQtNCAxMS00IDE1IDBsNDcgNDdjNCA0IDQgMTEgMCAxNXoiLz48cGF0aCBmaWxsPSIjYjMwIiBkPSJNMjg1IDI1OGw3IDdjNCA0IDQgMTEgMCAxNWwtOCA4Yy00IDQtMTEgNC0xNiAwbC02LTdjNCA1IDExIDUgMTUgMGw4LTdjNC01IDQtMTIgMC0xNnoiLz48cGF0aCBmaWx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MSAwLTE1bDEwMS0xMDFjNS01IDEyLTUgMTYgMHoiLz48cGF0aCBmaWxsPSIjM2VlIiBkPSJNMTM0IDk1bDE4LTE4IDE4IDE4LTE4IDE4em0tNTQgMThsMTgtMTcgMTggMTctMTggMTh6bTU1LTUzbDE4LTE4IDE4IDE4LTE4IDE4em05MyA0OGwtOC04Yy00LTUtMTEtNS0xNiAwTDEwMyAyMDFjLTQgNC00IDExIDAgMTVsOCA4Yy00LTQtNC0xMSAwLTE1bDEwMS0xMDFjNS00IDEyLTQgMTYgMHoiLz48cGF0aCBmaWxsPSIjOWVlIiBkPSJNMjcgMTMxbDE4LTE4IDE4IDE4LTE4IDE4em01NC01M2wxOC0xOCAxOCAxOC0xOCAxOHoiLz48cGF0aCBmaWxsPSIjMGFhIiBkPSJNMjMwIDExMGwxMyAxM2M0IDQgNCAxMSAwIDE2TDE0MiAyNDBjLTQgNC0xMSA0LTE1IDBsLTEzLTEzYzQgNCAxMSA0IDE1IDBsMTAxLTEwMWM1LTUgNS0xMSAwLTE2eiIvPjxwYXRoIGZpbGw9IiMxYWIiIGQ9Ik0xMzQgMjQ4Yy00IDAtOC0yLTExLTVsLTIzLTIzYTE2IDE2IDAgMDEwLTIzTDIwMSA5NmExNiAxNiAwIDAxMjIgMGwyNCAyNGM2IDYgNiAxNiAwIDIyTDE0NiAyNDNjLTMgMy03IDUtMTIgNXptNzgtMTQ3bC00IDItMTAxIDEwMWE2IDYgMCAwMDAgOWwyMyAyM2E2IDYgMCAwMDkgMGwxMDEtMTAxYTYgNiAwIDAwMC05bC0yNC0yMy00LTJ6Ii8+PC9zdmc+)](https://renovatebot.com)


This Docker container provides a Telegram integration to notify you about Docker events. It can notify you when a container starts, stops (including details about exit codes), restarts, and when the healthcheck status of a Docker container changes. You have the flexibility to customize these notifications by modifying the `templates.js` file.
Expand All @@ -13,7 +13,7 @@ If you encounter any issues, please feel free to contribute by fixing them and o
1. __Set up a Telegram bot:__ To get started, [create a Telegram bot](https://core.telegram.org/bots#3-how-do-i-create-a-bot) and obtain the `Bot Token`. Optionally add the bot to a group and allow it to post messages. Finally, [extract the Chat ID](https://stackoverflow.com/a/32572159/882223).

2. __Run the container:__

using `docker run`:
```sh
docker run -d \
Expand Down Expand Up @@ -63,9 +63,57 @@ If you encounter any issues, please feel free to contribute by fixing them and o
retries: 3
```

## Notification messages customization

1. __Download and modify the template:__ Download and modify the message strings in [`templates.js`](./templates.js) to according to your needs.

2. __Bind your customized file to the container:__

using `docker run`:
```sh
docker run -d \
--env TELEGRAM_NOTIFIER_BOT_TOKEN=token \
--env TELEGRAM_NOTIFIER_CHAT_ID=chat_id \
--volume /var/run/docker.sock:/var/run/docker.sock:ro \
--volume /my-template.js:/templates/messages.js:ro \
--hostname my_host \
lorcas/docker-telegram-notifier
```

using `docker-compose.yaml`
```yml
services:
notifier:
image: lorcas/docker-telegram-notifier:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
# Bind customized file to /templates/* in the container:
- ./my-template.js:/templates/messages.js:ro
environment:
# ...
#### Customizing message strings: supported variables
Here are some variables available to customize the notification messages:
* `${e.Actor.Attributes.name}` = Docker container Name
* `${e.Actor.Attributes.container}` = Docker container ID
* `${e.Actor.Attributes.docker.network.internal}` = Docker Network Internal
* `${e.Actor.Attributes.healthcheck.status}` = Docker container Healthcheck Status
* `${e.Actor.Attributes.image}` = Docker container Image used
* `${e.Actor.Attributes.maintainer}` = Docker container Maintainer
* `${e.Actor.Attributes.signal}` = Docker container Exit Signal
* `${e.Actor.Attributes.exitCode` = Docker container Exit Code
\+ These are only available if the container was started using `docker-compose.yaml`
* `${e.Actor.Attributes.com.docker.compose.project}` = Compose Project Name
* `${e.Actor.Attributes.com.docker.compose.service}` = Compose Service Name
* `${e.Actor.Attributes.com.docker.compose.version}` = Compose Version
* `${e.Actor.Attributes.com.docker.compose.container-number}` = Compose container Number
## Blacklist and Whitelist
You can disable notifications from specific containers by adding the label `--label telegram-notifier.monitor=false` to them.
You can disable notifications from specific containers by adding the label `--label telegram-notifier.monitor=false` to them.
Alternatively you can receive notifications only from whitelisted containers by setting `--env ONLY_WHITELIST=true` on the notifier instance, and `--label telegram-notifier.monitor=true` on the containers you want to monitor.
Expand Down
14 changes: 11 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Docker = require('dockerode');
const TelegramClient = require('./telegram');
const os = require('os');
const JSONStream = require('JSONStream');
const templates = require('./templates');

Expand Down Expand Up @@ -52,7 +51,16 @@ async function sendEventStream() {

async function sendVersion() {
const version = await docker.version();
let text = `Connected to docker on ${os.hostname()} (v${version.Version})`;
const info = await docker.info();
let text = templates.connection_message({
hostname: info.Name,
os: info.OperatingSystem,
type: info.OSType,
architecture: info.Architecture,
cpu: info.NCPU,
memory: (info.MemTotal / (1024 * 1024)).toFixed(2) + 'MB',
version: version.Version
});
console.log(text, "\n");
await telegram.send(text);
}
Expand Down Expand Up @@ -92,4 +100,4 @@ if (process.argv.includes("healthcheck")) {
healthcheck();
} else {
main().catch(handleError);
}
}
4 changes: 3 additions & 1 deletion templates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
connection_message: ({hostname, version, os, type, architecture, cpu, memory}) =>
`Connected to docker v${version} on ${hostname}\nOS: ${type}/${architecture} (${os}), ${cpu} CPU, ${memory} RAM`,

container_start: e =>
`&#9654;&#65039; <b>${e.Actor.Attributes.name}</b> started\n${e.Actor.Attributes.image}`,

Expand Down Expand Up @@ -38,4 +41,3 @@ module.exports = {
'container_health_status: unhealthy': e =>
`&#9888; <b>${e.Actor.Attributes.name}</b> unhealthy!\n${e.Actor.Attributes.image}`,
};

0 comments on commit 328fd49

Please sign in to comment.