|
@@ -1,17 +1,20 @@
|
|
|
-import { IncomingWebhook } from '@slack/webhook';
|
|
|
|
|
|
|
+import type { ChatPostMessageArguments } from '@slack/web-api';
|
|
|
import { WebClient } from '@slack/web-api';
|
|
import { WebClient } from '@slack/web-api';
|
|
|
|
|
+import { IncomingWebhook, type IncomingWebhookSendArguments } from '@slack/webhook';
|
|
|
|
|
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
const logger = loggerFactory('growi:util:slack-legacy');
|
|
const logger = loggerFactory('growi:util:slack-legacy');
|
|
|
|
|
|
|
|
-module.exports = function(crowi) {
|
|
|
|
|
|
|
|
|
|
- const { configManager } = crowi;
|
|
|
|
|
|
|
+interface SlackLegacyUtil {
|
|
|
|
|
+ postMessage(messageObj: IncomingWebhookSendArguments | ChatPostMessageArguments): Promise<void>,
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- const slackUtilLegacy = {};
|
|
|
|
|
|
|
+// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
|
+export const slackLegacyUtilFactory = (configManager: any): SlackLegacyUtil => {
|
|
|
|
|
|
|
|
- const postWithIwh = async(messageObj) => {
|
|
|
|
|
|
|
+ const postWithIwh = async(messageObj: IncomingWebhookSendArguments) => {
|
|
|
const webhook = new IncomingWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
|
|
const webhook = new IncomingWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
|
|
|
try {
|
|
try {
|
|
|
await webhook.send(messageObj);
|
|
await webhook.send(messageObj);
|
|
@@ -23,7 +26,7 @@ module.exports = function(crowi) {
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const postWithWebApi = async(messageObj) => {
|
|
|
|
|
|
|
+ const postWithWebApi = async(messageObj?: ChatPostMessageArguments) => {
|
|
|
const client = new WebClient(configManager.getConfig('notification', 'slack:token'));
|
|
const client = new WebClient(configManager.getConfig('notification', 'slack:token'));
|
|
|
try {
|
|
try {
|
|
|
await client.chat.postMessage(messageObj);
|
|
await client.chat.postMessage(messageObj);
|
|
@@ -35,30 +38,30 @@ module.exports = function(crowi) {
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- slackUtilLegacy.postMessage = async(messageObj) => {
|
|
|
|
|
- // when incoming Webhooks is prioritized
|
|
|
|
|
- if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
|
|
|
|
|
- if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
|
|
|
|
|
- logger.debug('posting message with IncomingWebhook');
|
|
|
|
|
- return postWithIwh(messageObj);
|
|
|
|
|
|
|
+ return {
|
|
|
|
|
+ postMessage: async(messageObj) => {
|
|
|
|
|
+ // when incoming Webhooks is prioritized
|
|
|
|
|
+ if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
|
|
|
|
|
+ if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
|
|
|
|
|
+ logger.debug('posting message with IncomingWebhook');
|
|
|
|
|
+ return postWithIwh(messageObj as IncomingWebhookSendArguments);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (configManager.getConfig('notification', 'slack:token')) {
|
|
|
|
|
+ logger.debug('posting message with Web API');
|
|
|
|
|
+ return postWithWebApi(messageObj as ChatPostMessageArguments);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- if (configManager.getConfig('notification', 'slack:token')) {
|
|
|
|
|
- logger.debug('posting message with Web API');
|
|
|
|
|
- return postWithWebApi(messageObj);
|
|
|
|
|
|
|
+ // else
|
|
|
|
|
+ else {
|
|
|
|
|
+ if (configManager.getConfig('notification', 'slack:token')) {
|
|
|
|
|
+ logger.debug('posting message with Web API');
|
|
|
|
|
+ return postWithWebApi(messageObj as ChatPostMessageArguments);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
|
|
|
|
|
+ logger.debug('posting message with IncomingWebhook');
|
|
|
|
|
+ return postWithIwh(messageObj as IncomingWebhookSendArguments);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- // else
|
|
|
|
|
- else {
|
|
|
|
|
- if (configManager.getConfig('notification', 'slack:token')) {
|
|
|
|
|
- logger.debug('posting message with Web API');
|
|
|
|
|
- return postWithWebApi(messageObj);
|
|
|
|
|
- }
|
|
|
|
|
- if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
|
|
|
|
|
- logger.debug('posting message with IncomingWebhook');
|
|
|
|
|
- return postWithIwh(messageObj);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ },
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
- return slackUtilLegacy;
|
|
|
|
|
};
|
|
};
|