mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
feat: Slack bot notifications (#676)
This commit is contained in:
parent
89aba92b01
commit
7cb13f2ab3
9 changed files with 129 additions and 24 deletions
|
@ -31,14 +31,6 @@ services:
|
||||||
# - DEFAULT_ADMIN_NAME=Demo Demo
|
# - DEFAULT_ADMIN_NAME=Demo Demo
|
||||||
# - DEFAULT_ADMIN_USERNAME=demo
|
# - DEFAULT_ADMIN_USERNAME=demo
|
||||||
|
|
||||||
# Email Notifications (https://nodemailer.com/smtp/)
|
|
||||||
# - SMTP_HOST=
|
|
||||||
# - SMTP_PORT=587
|
|
||||||
# - SMTP_SECURE=true
|
|
||||||
# - SMTP_USER=
|
|
||||||
# - SMTP_PASSWORD=
|
|
||||||
# - SMTP_FROM="Demo Demo" <demo@demo.demo>
|
|
||||||
|
|
||||||
# - OIDC_ISSUER=
|
# - OIDC_ISSUER=
|
||||||
# - OIDC_CLIENT_ID=
|
# - OIDC_CLIENT_ID=
|
||||||
# - OIDC_CLIENT_SECRET=
|
# - OIDC_CLIENT_SECRET=
|
||||||
|
@ -51,6 +43,17 @@ services:
|
||||||
# - OIDC_IGNORE_USERNAME=true
|
# - OIDC_IGNORE_USERNAME=true
|
||||||
# - OIDC_IGNORE_ROLES=true
|
# - OIDC_IGNORE_ROLES=true
|
||||||
# - OIDC_ENFORCED=true
|
# - OIDC_ENFORCED=true
|
||||||
|
|
||||||
|
# Email Notifications (https://nodemailer.com/smtp/)
|
||||||
|
# - SMTP_HOST=
|
||||||
|
# - SMTP_PORT=587
|
||||||
|
# - SMTP_SECURE=true
|
||||||
|
# - SMTP_USER=
|
||||||
|
# - SMTP_PASSWORD=
|
||||||
|
# - SMTP_FROM="Demo Demo" <demo@demo.demo>
|
||||||
|
|
||||||
|
# - SLACK_BOT_TOKEN=
|
||||||
|
# - SLACK_CHANNEL_ID=
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|
|
@ -22,14 +22,6 @@ SECRET_KEY=notsecretkey
|
||||||
# DEFAULT_ADMIN_NAME=Demo Demo
|
# DEFAULT_ADMIN_NAME=Demo Demo
|
||||||
# DEFAULT_ADMIN_USERNAME=demo
|
# DEFAULT_ADMIN_USERNAME=demo
|
||||||
|
|
||||||
# Email Notifications (https://nodemailer.com/smtp/)
|
|
||||||
# SMTP_HOST=
|
|
||||||
# SMTP_PORT=587
|
|
||||||
# SMTP_SECURE=true
|
|
||||||
# SMTP_USER=
|
|
||||||
# SMTP_PASSWORD=
|
|
||||||
# SMTP_FROM="Demo Demo" <demo@demo.demo>
|
|
||||||
|
|
||||||
# OIDC_ISSUER=
|
# OIDC_ISSUER=
|
||||||
# OIDC_CLIENT_ID=
|
# OIDC_CLIENT_ID=
|
||||||
# OIDC_CLIENT_SECRET=
|
# OIDC_CLIENT_SECRET=
|
||||||
|
@ -43,6 +35,17 @@ SECRET_KEY=notsecretkey
|
||||||
# OIDC_IGNORE_ROLES=true
|
# OIDC_IGNORE_ROLES=true
|
||||||
# OIDC_ENFORCED=true
|
# OIDC_ENFORCED=true
|
||||||
|
|
||||||
|
# Email Notifications (https://nodemailer.com/smtp/)
|
||||||
|
# SMTP_HOST=
|
||||||
|
# SMTP_PORT=587
|
||||||
|
# SMTP_SECURE=true
|
||||||
|
# SMTP_USER=
|
||||||
|
# SMTP_PASSWORD=
|
||||||
|
# SMTP_FROM="Demo Demo" <demo@demo.demo>
|
||||||
|
|
||||||
|
# SLACK_BOT_TOKEN=
|
||||||
|
# SLACK_CHANNEL_ID=
|
||||||
|
|
||||||
## Do not edit this
|
## Do not edit this
|
||||||
|
|
||||||
TZ=UTC
|
TZ=UTC
|
||||||
|
|
|
@ -47,6 +47,7 @@ module.exports = {
|
||||||
|
|
||||||
card = await sails.helpers.cards.deleteOne.with({
|
card = await sails.helpers.cards.deleteOne.with({
|
||||||
record: card,
|
record: card,
|
||||||
|
user: currentUser,
|
||||||
request: this.req,
|
request: this.req,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,30 @@ const valuesValidator = (value) => {
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const buildAndSendSlackMessage = async (user, card, action) => {
|
||||||
|
const cardLink = `<${sails.config.custom.baseUrl}/cards/${card.id}|${card.name}>`;
|
||||||
|
|
||||||
|
let markdown;
|
||||||
|
switch (action.type) {
|
||||||
|
case Action.Types.CREATE_CARD:
|
||||||
|
markdown = `${cardLink} was created by ${user.name} in *${action.data.list.name}*`;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case Action.Types.MOVE_CARD:
|
||||||
|
markdown = `${cardLink} was moved by ${user.name} to *${action.data.toList.name}*`;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case Action.Types.COMMENT_CARD:
|
||||||
|
markdown = `*${user.name}* commented on ${cardLink}:\n>${action.data.text}`;
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await sails.helpers.utils.sendSlackMessage(markdown);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
inputs: {
|
inputs: {
|
||||||
values: {
|
values: {
|
||||||
|
@ -67,6 +91,10 @@ module.exports = {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (sails.config.custom.slackBotToken) {
|
||||||
|
buildAndSendSlackMessage(values.user, values.card, action);
|
||||||
|
}
|
||||||
|
|
||||||
return action;
|
return action;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
|
const buildAndSendSlackMessage = async (user, card) => {
|
||||||
|
await sails.helpers.utils.sendSlackMessage(`*${card.name}* was deleted by ${user.name}`);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
inputs: {
|
inputs: {
|
||||||
record: {
|
record: {
|
||||||
type: 'ref',
|
type: 'ref',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
user: {
|
||||||
|
type: 'ref',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
request: {
|
request: {
|
||||||
type: 'ref',
|
type: 'ref',
|
||||||
},
|
},
|
||||||
|
@ -21,6 +29,10 @@ module.exports = {
|
||||||
},
|
},
|
||||||
inputs.request,
|
inputs.request,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (sails.config.custom.slackBotToken) {
|
||||||
|
buildAndSendSlackMessage(inputs.user, card);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return card;
|
return card;
|
||||||
|
|
|
@ -27,6 +27,7 @@ const buildAndSendEmail = async (user, board, card, action, notifiableUser) => {
|
||||||
`from ${action.data.fromList.name} to ${action.data.toList.name} ` +
|
`from ${action.data.fromList.name} to ${action.data.toList.name} ` +
|
||||||
`on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>`,
|
`on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>`,
|
||||||
};
|
};
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case Action.Types.COMMENT_CARD:
|
case Action.Types.COMMENT_CARD:
|
||||||
emailData = {
|
emailData = {
|
||||||
|
@ -37,6 +38,7 @@ const buildAndSendEmail = async (user, board, card, action, notifiableUser) => {
|
||||||
`on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>` +
|
`on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>` +
|
||||||
`<p>${action.data.text}</p>`,
|
`<p>${action.data.text}</p>`,
|
||||||
};
|
};
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -25,7 +25,7 @@ module.exports = {
|
||||||
|
|
||||||
sails.log.info('Email sent: %s', info.messageId);
|
sails.log.info('Email sent: %s', info.messageId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
sails.log.error(error);
|
sails.log.error(error); // TODO: provide description text?
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
53
server/api/helpers/utils/send-slack-message.js
Normal file
53
server/api/helpers/utils/send-slack-message.js
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
const POST_MESSAGE_API_URL = 'https://slack.com/api/chat.postMessage';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
inputs: {
|
||||||
|
markdown: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
async fn(inputs) {
|
||||||
|
const headers = {
|
||||||
|
Authorization: `Bearer ${sails.config.custom.slackBotToken}`,
|
||||||
|
'Content-Type': 'application/json; charset=utf-8',
|
||||||
|
};
|
||||||
|
|
||||||
|
const body = {
|
||||||
|
blocks: [
|
||||||
|
{
|
||||||
|
type: 'section',
|
||||||
|
text: {
|
||||||
|
type: 'mrkdwn',
|
||||||
|
text: inputs.markdown,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
channel: sails.config.custom.slackChannelId,
|
||||||
|
};
|
||||||
|
|
||||||
|
let response;
|
||||||
|
try {
|
||||||
|
response = await fetch(POST_MESSAGE_API_URL, {
|
||||||
|
headers,
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
sails.log.error(error); // TODO: provide description text?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
sails.log.error('Error sending to Slack: %s', response.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseJson = await response.json();
|
||||||
|
|
||||||
|
if (!responseJson.ok) {
|
||||||
|
sails.log.error('Error sending to Slack: %s', responseJson.error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
|
@ -34,13 +34,6 @@ module.exports.custom = {
|
||||||
defaultAdminEmail:
|
defaultAdminEmail:
|
||||||
process.env.DEFAULT_ADMIN_EMAIL && process.env.DEFAULT_ADMIN_EMAIL.toLowerCase(),
|
process.env.DEFAULT_ADMIN_EMAIL && process.env.DEFAULT_ADMIN_EMAIL.toLowerCase(),
|
||||||
|
|
||||||
smtpHost: process.env.SMTP_HOST,
|
|
||||||
smtpPort: process.env.SMTP_PORT || 587,
|
|
||||||
smtpSecure: process.env.SMTP_SECURE === 'true',
|
|
||||||
smtpUser: process.env.SMTP_USER,
|
|
||||||
smtpPassword: process.env.SMTP_PASSWORD,
|
|
||||||
smtpFrom: process.env.SMTP_FROM,
|
|
||||||
|
|
||||||
oidcIssuer: process.env.OIDC_ISSUER,
|
oidcIssuer: process.env.OIDC_ISSUER,
|
||||||
oidcClientId: process.env.OIDC_CLIENT_ID,
|
oidcClientId: process.env.OIDC_CLIENT_ID,
|
||||||
oidcClientSecret: process.env.OIDC_CLIENT_SECRET,
|
oidcClientSecret: process.env.OIDC_CLIENT_SECRET,
|
||||||
|
@ -58,4 +51,14 @@ module.exports.custom = {
|
||||||
oidcRedirectUri: `${
|
oidcRedirectUri: `${
|
||||||
sails.config.environment === 'production' ? process.env.BASE_URL : 'http://localhost:3000'
|
sails.config.environment === 'production' ? process.env.BASE_URL : 'http://localhost:3000'
|
||||||
}/oidc-callback`,
|
}/oidc-callback`,
|
||||||
|
|
||||||
|
smtpHost: process.env.SMTP_HOST,
|
||||||
|
smtpPort: process.env.SMTP_PORT || 587,
|
||||||
|
smtpSecure: process.env.SMTP_SECURE === 'true',
|
||||||
|
smtpUser: process.env.SMTP_USER,
|
||||||
|
smtpPassword: process.env.SMTP_PASSWORD,
|
||||||
|
smtpFrom: process.env.SMTP_FROM,
|
||||||
|
|
||||||
|
slackBotToken: process.env.SLACK_BOT_TOKEN,
|
||||||
|
slackChannelId: process.env.SLACK_CHANNEL_ID,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue