1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 15:49:46 +02:00

feat: Telegram bot notifications (#928)

This commit is contained in:
Elllone 2024-10-31 00:11:52 +03:00 committed by GitHub
parent 1d2193c381
commit cc95032e74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 113 additions and 6 deletions

View file

@ -38,6 +38,32 @@ const buildAndSendMessage = async (card, action, actorUser, send) => {
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 = {
inputs: {
values: {
@ -119,6 +145,15 @@ module.exports = {
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) {
buildAndSendMessage(
values.card,

View file

@ -2,6 +2,10 @@ const buildAndSendMessage = async (card, actorUser, send) => {
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 = {
inputs: {
record: {
@ -59,6 +63,14 @@ module.exports = {
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) {
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendGoogleChatMessage);
}

View 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}`);
}
},
};