mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 05:09:43 +02:00
feat: Telegram bot notifications (#928)
This commit is contained in:
parent
1d2193c381
commit
cc95032e74
7 changed files with 113 additions and 6 deletions
|
@ -67,8 +67,12 @@ services:
|
||||||
|
|
||||||
# - SLACK_BOT_TOKEN=
|
# - SLACK_BOT_TOKEN=
|
||||||
# - SLACK_CHANNEL_ID=
|
# - SLACK_CHANNEL_ID=
|
||||||
|
|
||||||
|
# - TELEGRAM_BOT_TOKEN=
|
||||||
|
# - TELEGRAM_CHAT_ID=
|
||||||
|
# - TELEGRAM_THREAD_ID=
|
||||||
working_dir: /app
|
working_dir: /app
|
||||||
command: ["sh", "-c", "npm run start"]
|
command: ['sh', '-c', 'npm run start']
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
@ -101,7 +105,7 @@ services:
|
||||||
# - DEFAULT_ADMIN_NAME=Demo Demo
|
# - DEFAULT_ADMIN_NAME=Demo Demo
|
||||||
# - DEFAULT_ADMIN_USERNAME=demo
|
# - DEFAULT_ADMIN_USERNAME=demo
|
||||||
working_dir: /app
|
working_dir: /app
|
||||||
command: ["sh", "-c", "npm run db:init"]
|
command: ['sh', '-c', 'npm run db:init']
|
||||||
volumes:
|
volumes:
|
||||||
- ./server:/app
|
- ./server:/app
|
||||||
- /app/node_modules
|
- /app/node_modules
|
||||||
|
@ -118,10 +122,10 @@ services:
|
||||||
POSTGRES_USER: user
|
POSTGRES_USER: user
|
||||||
POSTGRES_PASSWORD: password
|
POSTGRES_PASSWORD: password
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- '5432:5432'
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U user -d planka_db"]
|
test: ['CMD-SHELL', 'pg_isready -U user -d planka_db']
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
@ -129,7 +133,7 @@ services:
|
||||||
proxy:
|
proxy:
|
||||||
image: nginx:alpine
|
image: nginx:alpine
|
||||||
ports:
|
ports:
|
||||||
- "3000:80"
|
- '3000:80'
|
||||||
volumes:
|
volumes:
|
||||||
- ./config/development/nginx.conf:/etc/nginx/nginx.conf
|
- ./config/development/nginx.conf:/etc/nginx/nginx.conf
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|
|
@ -75,6 +75,10 @@ services:
|
||||||
# - SLACK_BOT_TOKEN=
|
# - SLACK_BOT_TOKEN=
|
||||||
# - SLACK_CHANNEL_ID=
|
# - SLACK_CHANNEL_ID=
|
||||||
# - GOOGLE_CHAT_WEBHOOK_URL=
|
# - GOOGLE_CHAT_WEBHOOK_URL=
|
||||||
|
|
||||||
|
# - TELEGRAM_BOT_TOKEN=
|
||||||
|
# - TELEGRAM_CHAT_ID=
|
||||||
|
# - TELEGRAM_THREAD_ID=
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
@ -88,7 +92,7 @@ services:
|
||||||
- POSTGRES_DB=planka
|
- POSTGRES_DB=planka
|
||||||
- POSTGRES_HOST_AUTH_METHOD=trust
|
- POSTGRES_HOST_AUTH_METHOD=trust
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U postgres -d planka"]
|
test: ['CMD-SHELL', 'pg_isready -U postgres -d planka']
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
|
|
@ -67,6 +67,11 @@ SECRET_KEY=notsecretkey
|
||||||
# SLACK_CHANNEL_ID=
|
# SLACK_CHANNEL_ID=
|
||||||
# GOOGLE_CHAT_WEBHOOK_URL=
|
# GOOGLE_CHAT_WEBHOOK_URL=
|
||||||
|
|
||||||
|
# TELEGRAM_BOT_TOKEN=
|
||||||
|
# TELEGRAM_CHAT_ID=
|
||||||
|
# TELEGRAM_THREAD_ID=
|
||||||
|
|
||||||
|
|
||||||
## Do not edit this
|
## Do not edit this
|
||||||
|
|
||||||
TZ=UTC
|
TZ=UTC
|
||||||
|
|
|
@ -38,6 +38,32 @@ const buildAndSendMessage = async (card, action, actorUser, send) => {
|
||||||
await send(markdown);
|
await send(markdown);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const buildAndSendMessageForTelegramBot = async (card, action, actorUser, send) => {
|
||||||
|
const cardLink = `<a href="${sails.config.custom.baseUrl}/cards/${card.id}">${card.name}</a>`;
|
||||||
|
|
||||||
|
let html;
|
||||||
|
switch (action.type) {
|
||||||
|
case Action.Types.CREATE_CARD:
|
||||||
|
html = `${cardLink} was created by ${actorUser.name} in <b>${action.data.list.name}</b>`;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case Action.Types.MOVE_CARD:
|
||||||
|
html = `${cardLink} was moved by ${actorUser.name} to <b>${action.data.toList.name}</b>`;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case Action.Types.COMMENT_CARD: {
|
||||||
|
const commentedText =
|
||||||
|
action.data.text.length > 30 ? `${action.data.text.substring(0, 30)}...` : action.data.text;
|
||||||
|
html = `<b>${actorUser.name}</b> commented on ${cardLink}: <i>${commentedText}</i>`;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await send(html);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
inputs: {
|
inputs: {
|
||||||
values: {
|
values: {
|
||||||
|
@ -119,6 +145,15 @@ module.exports = {
|
||||||
buildAndSendMessage(values.card, action, values.user, sails.helpers.utils.sendSlackMessage);
|
buildAndSendMessage(values.card, action, values.user, sails.helpers.utils.sendSlackMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sails.config.custom.telegramChatId) {
|
||||||
|
buildAndSendMessageForTelegramBot(
|
||||||
|
values.card,
|
||||||
|
action,
|
||||||
|
values.user,
|
||||||
|
sails.helpers.utils.sendTelegramMessage,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (sails.config.custom.googleChatWebhookUrl) {
|
if (sails.config.custom.googleChatWebhookUrl) {
|
||||||
buildAndSendMessage(
|
buildAndSendMessage(
|
||||||
values.card,
|
values.card,
|
||||||
|
|
|
@ -2,6 +2,10 @@ const buildAndSendMessage = async (card, actorUser, send) => {
|
||||||
await send(`*${card.name}* was deleted by ${actorUser.name}`);
|
await send(`*${card.name}* was deleted by ${actorUser.name}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const buildAndSendMessageForTelegramBot = async (card, actorUser, send) => {
|
||||||
|
await send(`<b>${card.name}</b> was deleted by ${actorUser.name}`);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
inputs: {
|
inputs: {
|
||||||
record: {
|
record: {
|
||||||
|
@ -59,6 +63,14 @@ module.exports = {
|
||||||
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage);
|
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sails.config.custom.telegramChatId) {
|
||||||
|
buildAndSendMessageForTelegramBot(
|
||||||
|
card,
|
||||||
|
inputs.actorUser,
|
||||||
|
sails.helpers.utils.sendTelegramMessage,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (sails.config.custom.googleChatWebhookUrl) {
|
if (sails.config.custom.googleChatWebhookUrl) {
|
||||||
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendGoogleChatMessage);
|
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendGoogleChatMessage);
|
||||||
}
|
}
|
||||||
|
|
43
server/api/helpers/utils/send-telegram-message.js
Normal file
43
server/api/helpers/utils/send-telegram-message.js
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
const POST_MESSAGE_API_URL = (telegramBotToken) =>
|
||||||
|
`https://api.telegram.org/bot${telegramBotToken}/sendMessage`;
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
inputs: {
|
||||||
|
html: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async fn(inputs) {
|
||||||
|
const headers = {
|
||||||
|
'Content-Type': 'application/json; charset=utf-8',
|
||||||
|
};
|
||||||
|
|
||||||
|
const body = {
|
||||||
|
chat_id: sails.config.custom.telegramChatId,
|
||||||
|
text: inputs.html,
|
||||||
|
parse_mode: 'HTML',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (sails.config.custom.telegramThreadId) {
|
||||||
|
body.message_thread_id = sails.config.custom.telegramThreadId;
|
||||||
|
}
|
||||||
|
|
||||||
|
let response;
|
||||||
|
try {
|
||||||
|
response = await fetch(POST_MESSAGE_API_URL(sails.config.custom.telegramBotToken), {
|
||||||
|
headers,
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
sails.log.error(`Error sending to Telegram: ${error}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const responseErrorJson = await response.json();
|
||||||
|
sails.log.error(`Error sending to Telegram: ${responseErrorJson.description}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
|
@ -83,4 +83,8 @@ module.exports.custom = {
|
||||||
slackBotToken: process.env.SLACK_BOT_TOKEN,
|
slackBotToken: process.env.SLACK_BOT_TOKEN,
|
||||||
slackChannelId: process.env.SLACK_CHANNEL_ID,
|
slackChannelId: process.env.SLACK_CHANNEL_ID,
|
||||||
googleChatWebhookUrl: process.env.GOOGLE_CHAT_WEBHOOK_URL,
|
googleChatWebhookUrl: process.env.GOOGLE_CHAT_WEBHOOK_URL,
|
||||||
|
|
||||||
|
telegramBotToken: process.env.TELEGRAM_BOT_TOKEN,
|
||||||
|
telegramChatId: process.env.TELEGRAM_CHAT_ID,
|
||||||
|
telegramThreadId: process.env.TELEGRAM_THREAD_ID,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue