Skip to content

Commit

Permalink
allow container specific chat-id/topic-id/thread-id
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-ass committed Dec 13, 2024
1 parent 2819ac8 commit 5ccfb9d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ By default notifier connects to a local docker instance (don't forget to specify
Notifier accepts usual `DOCKER_HOST` and `DOCKER_CERT_PATH` environment variables to specify remote instance. For http endpoint you need to specify only `--env DOCKER_HOST=tcp://example.com:2375` (make sure to keep such instances behind the firewall). For https, you'll also need to mount a volume with https certificates that contains `ca.pem`, `cert.pem`, and `key.pem`: `--env DOCKER_HOST=tcp://example.com:2376 --env DOCKER_CERT_PATH=/certs --volume $(pwd):/certs`
Tutorial on how to generate docker certs can be found [here](https://docs.docker.com/engine/security/https/)

## Container-Specific Notifications

You can configure different Telegram channels and threads/topics for specific containers using Docker labels:

```yaml
services:
example:
image: hello-world
labels:
# Monitor control
telegram-notifier.monitor: true
# Channel override (optional)
telegram-notifier.chat-id: "-100123456789"
# Thread/Topic override (optional - use only one)
telegram-notifier.topic-id: "12345"
telegram-notifier.thread-id: "12345"
```

If these labels are not specified, the container will use the global settings from the notifier's environment variables.
## Credits
Expand Down
29 changes: 25 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,36 @@ const docker = new Docker();
const telegram = new TelegramClient();

async function sendEvent(event) {
// console.debug(event);
const template = templates[`${event.Type}_${event.Action}`];
if (template) {
const label = event.Actor && event.Actor.Attributes && event.Actor.Attributes['telegram-notifier.monitor'];
const shouldMonitor = label === undefined ? undefined : label.toLowerCase().trim() !== 'false';
const attributes = event.Actor?.Attributes || {};

// Check monitoring status
const monitorLabel = attributes['telegram-notifier.monitor'];
const shouldMonitor = monitorLabel === undefined ?
undefined :
monitorLabel.toLowerCase().trim() !== 'false';

if (shouldMonitor || !ONLY_WHITELIST && shouldMonitor !== false) {
// Get container-specific channel settings
const overrides = {};

// Only add chatId if explicitly set via label
const labelChatId = attributes['telegram-notifier.chat-id'];
if (labelChatId) {
overrides.chatId = labelChatId;
}

// Only add threadId if explicitly set via label
const labelThreadId = attributes['telegram-notifier.topic-id'] ||
attributes['telegram-notifier.thread-id'];
if (labelThreadId) {
overrides.threadId = labelThreadId;
}

const attachment = template(event);
console.log(attachment, "\n");
await telegram.send(attachment)
await telegram.send(attachment, overrides);
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,38 @@ class TelegramClient {
null;
}

send(message) {
async send(message, overrides = {}) {
const options = {
parse_mode: 'HTML',
disable_web_page_preview: true
};

if (this.threadId) {
options.message_thread_id = parseInt(this.threadId);
const threadId = overrides.threadId || this.threadId;
if (threadId) {
options.message_thread_id = parseInt(threadId);
}

const chatId = overrides.chatId || process.env.TELEGRAM_NOTIFIER_CHAT_ID;

return this.telegram.sendMessage(
process.env.TELEGRAM_NOTIFIER_CHAT_ID,
chatId,
message,
options
);
}

sendError(e) {
async sendError(e, overrides = {}) {
const options = {};
if (this.threadId) {
options.message_thread_id = parseInt(this.threadId);

const threadId = overrides.threadId || this.threadId;
if (threadId) {
options.message_thread_id = parseInt(threadId);
}

const chatId = overrides.chatId || process.env.TELEGRAM_NOTIFIER_CHAT_ID;

return this.telegram.sendMessage(
process.env.TELEGRAM_NOTIFIER_CHAT_ID,
chatId,
`Error: ${e}`,
options
);
Expand Down

0 comments on commit 5ccfb9d

Please sign in to comment.