1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00

feat: Google Chat notifications (#867)

This commit is contained in:
Maël Gangloff 2024-09-04 15:33:43 +02:00 committed by GitHub
parent 9699fbe76a
commit 8fd0f682d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 65 additions and 9 deletions

View file

@ -72,6 +72,7 @@ services:
# - SLACK_BOT_TOKEN=
# - SLACK_CHANNEL_ID=
# - GOOGLE_CHAT_WEBHOOK_URL=
depends_on:
postgres:
condition: service_healthy

View file

@ -61,6 +61,7 @@ SECRET_KEY=notsecretkey
# SLACK_BOT_TOKEN=
# SLACK_CHANNEL_ID=
# GOOGLE_CHAT_WEBHOOK_URL=
## Do not edit this

View file

@ -14,7 +14,7 @@ const valuesValidator = (value) => {
return true;
};
const buildAndSendSlackMessage = async (card, action, actorUser) => {
const buildAndSendMessage = async (card, action, actorUser, send) => {
const cardLink = `<${sails.config.custom.baseUrl}/cards/${card.id}|${card.name}>`;
let markdown;
@ -35,7 +35,7 @@ const buildAndSendSlackMessage = async (card, action, actorUser) => {
return;
}
await sails.helpers.utils.sendSlackMessage(markdown);
await send(markdown);
};
module.exports = {
@ -94,10 +94,6 @@ module.exports = {
user: values.user,
});
if (sails.config.custom.slackBotToken) {
buildAndSendSlackMessage(values.card, action, values.user);
}
const subscriptionUserIds = await sails.helpers.cards.getSubscriptionUserIds(
action.cardId,
action.userId,
@ -119,6 +115,18 @@ module.exports = {
),
);
if (sails.config.custom.slackBotToken) {
buildAndSendMessage(values.card, action, values.user, sails.helpers.utils.sendSlackMessage);
}
if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMessage(
values.card,
action,
values.user,
sails.helpers.utils.sendGoogleChatMessage,
);
}
return action;
},
};

View file

@ -1,5 +1,5 @@
const buildAndSendSlackMessage = async (card, actorUser) => {
await sails.helpers.utils.sendSlackMessage(`*${card.name}* was deleted by ${actorUser.name}`);
const buildAndSendMessage = async (card, actorUser, send) => {
await send(`*${card.name}* was deleted by ${actorUser.name}`);
};
module.exports = {
@ -56,7 +56,11 @@ module.exports = {
});
if (sails.config.custom.slackBotToken) {
buildAndSendSlackMessage(card, inputs.actorUser);
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage);
}
if (sails.config.custom.googleChatWebhookUrl) {
buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendGoogleChatMessage);
}
}

View file

@ -0,0 +1,41 @@
module.exports = {
inputs: {
markdown: {
type: 'string',
required: true,
},
},
async fn(inputs) {
const headers = {
'Content-Type': 'application/json; charset=utf-8',
};
const body = {
text: inputs.markdown,
};
let response;
try {
response = await fetch(sails.config.custom.googleChatWebhookUrl, {
headers,
method: 'POST',
body: JSON.stringify(body),
});
} catch (error) {
sails.log.error(`Error sending to Google Chat: ${error}`);
return;
}
if (!response.ok) {
sails.log.error(`Error sending to Google Chat: ${response.error}`);
return;
}
const responseJson = await response.json();
if (!responseJson.ok) {
sails.log.error(`Error sending to Google Chat: ${responseJson.error}`);
}
},
};

View file

@ -77,4 +77,5 @@ module.exports.custom = {
slackBotToken: process.env.SLACK_BOT_TOKEN,
slackChannelId: process.env.SLACK_CHANNEL_ID,
googleChatWebhookUrl: process.env.GOOGLE_CHAT_WEBHOOK_URL,
};