@xpr/nestjs-slack 0.7.0 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/decorators.d.ts +1 -0
- package/package.json +1 -36
- package/readme.md +65 -0
- package/slack.js +14 -7
- package/utils.d.ts +1 -1
- package/utils.js +21 -15
package/decorators.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from '@slack/bolt';
|
|
2
2
|
import { type Pattern } from './utils';
|
|
3
|
+
export type { SlackActionMiddlewareArgs, SlackCommandMiddlewareArgs, SlackShortcutMiddlewareArgs, SlackEventMiddlewareArgs, SlackViewMiddlewareArgs, SlackOptionsMiddlewareArgs, AllMiddlewareArgs, } from '@slack/bolt/dist/types';
|
|
3
4
|
export { Pattern };
|
|
4
5
|
/**
|
|
5
6
|
* @see https://tools.slack.dev/bolt-js/reference/
|
package/package.json
CHANGED
|
@@ -1,36 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@xpr/nestjs-slack",
|
|
3
|
-
"version": "0.7.0",
|
|
4
|
-
"description": "NestJS server implementation of the Slack Assistant",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"build": "tsc -p tsconfig-lib.json",
|
|
8
|
-
"postbuild": "cp package.json ../../dist/nestjs-slack/. && cp readme.md ../../dist/nestjs-slack/."
|
|
9
|
-
},
|
|
10
|
-
"author": "Ziv Perry",
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"keywords": [],
|
|
13
|
-
"engines": {
|
|
14
|
-
"node": ">=20.0.0",
|
|
15
|
-
"npm": ">=10.0.0"
|
|
16
|
-
},
|
|
17
|
-
"repository": {
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/ziv/nestjs-slack-assistant.git",
|
|
20
|
-
"directory": "libs/slack-assistant"
|
|
21
|
-
},
|
|
22
|
-
"bugs": {
|
|
23
|
-
"url": "https://github.com/ziv/nestjs-slack-assistant/issues"
|
|
24
|
-
},
|
|
25
|
-
"homepage": "https://github.com/ziv/nestjs-slack-assistant#readme",
|
|
26
|
-
"devDependencies": {
|
|
27
|
-
"@tsconfig/node20": "^20.1.4",
|
|
28
|
-
"@types/node": "^22.10.6"
|
|
29
|
-
},
|
|
30
|
-
"peerDependencies": {
|
|
31
|
-
"@nestjs/common": "^11.0.1",
|
|
32
|
-
"@nestjs/microservices": ">=11.0.13",
|
|
33
|
-
"@slack/bolt": ">=4.2.1",
|
|
34
|
-
"reflect-metadata": ">=0.2.2"
|
|
35
|
-
}
|
|
36
|
-
}
|
|
1
|
+
{"name":"@xpr/nestjs-slack","version":"0.9.3","description":"NestJS server implementation of the Slack Assistant","main":"index.js","scripts":{"build":"tsc -p tsconfig-lib.json","release":"npm run build && npm publish --access public"},"author":"Ziv Perry","license":"MIT","keywords":["slack","nestjs","assistant","ai"],"engines":{"node":">=20.0.0","npm":">=10.0.0"},"repository":{"type":"git","url":"git+https://github.com/ziv/nestjs-slack.git","directory":"packages/nestjs-slack-assistant"},"bugs":{"url":"https://github.com/ziv/nestjs-slack/issues"},"homepage":"https://github.com/ziv/nestjs-slack#readme","devDependencies":{"@tsconfig/node20":"^20.1.4","@types/node":"^22.10.6"},"peerDependencies":{"@nestjs/common":"^11.0.1","@nestjs/microservices":">=11.0.13","@slack/bolt":">=4.2.1","reflect-metadata":">=0.2.2"},"files":["*.js","*.d.ts","readme.md"]}
|
package/readme.md
CHANGED
|
@@ -1 +1,66 @@
|
|
|
1
1
|
# @xpr/nestjs-slack
|
|
2
|
+
|
|
3
|
+
A NestJS microservice transport and decorators for [Slack Bolt Apps](https://github.com/slackapi/bolt-js).
|
|
4
|
+
|
|
5
|
+
### Usage
|
|
6
|
+
|
|
7
|
+
Make sure to install peer dependencies if not already installed (`@slack/bolt`).
|
|
8
|
+
|
|
9
|
+
```shell
|
|
10
|
+
npm i @xpr/nestjs-slack
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Initialize Slack/Bolt application with the `Slack` transport:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
const app = await NestFactory.createMicroservice(MyModule, {
|
|
17
|
+
strategy: new Slack({
|
|
18
|
+
slack: {
|
|
19
|
+
token: env.SLACK_BOT_TOKEN,
|
|
20
|
+
appToken: env.SLACK_APP_TOKEN,
|
|
21
|
+
},
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
await app.listen();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
📃 https://tools.slack.dev/bolt-js/getting-started
|
|
28
|
+
|
|
29
|
+
### Slack Application Controller
|
|
30
|
+
|
|
31
|
+
Example of a Slack controller with listener for action and a command.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import {
|
|
35
|
+
SlackAction,
|
|
36
|
+
SlackCommand,
|
|
37
|
+
SlackController,
|
|
38
|
+
} from '@xpr/nestjs-slack';
|
|
39
|
+
|
|
40
|
+
@SlackController()
|
|
41
|
+
export class MyController {
|
|
42
|
+
|
|
43
|
+
@SlackAction('button-action')
|
|
44
|
+
async onAction({ ack, respond, payload }: SlackActionMiddlewareArgs) {
|
|
45
|
+
await ack();
|
|
46
|
+
await respond(`Button clicked! with payload ${JSON.stringify(payload)}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@SlackCommand('/ping')
|
|
50
|
+
async onPing({ ack, respond }: SlackCommandMiddlewareArgs) {
|
|
51
|
+
await ack();
|
|
52
|
+
await respond({
|
|
53
|
+
text: 'pong!',
|
|
54
|
+
response_type: 'in_channel',
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
📃 https://tools.slack.dev/bolt-js/concepts/event-listening
|
|
61
|
+
|
|
62
|
+
📃 https://tools.slack.dev/bolt-js/concepts/commands
|
|
63
|
+
|
|
64
|
+
## Decorators
|
|
65
|
+
|
|
66
|
+
See [source file](./decorators.ts).
|
package/slack.js
CHANGED
|
@@ -34,20 +34,27 @@ class Slack extends microservices_1.Server {
|
|
|
34
34
|
*/
|
|
35
35
|
register(handler) {
|
|
36
36
|
const { type, event } = handler.extras;
|
|
37
|
-
this.logger.log(`Registering handler of type [${type}] with (${event})`);
|
|
38
37
|
switch (type) {
|
|
39
38
|
case decorators_1.EventTypes.Shortcut:
|
|
40
|
-
|
|
39
|
+
this.app().shortcut(event, handler);
|
|
40
|
+
break;
|
|
41
41
|
case decorators_1.EventTypes.Action:
|
|
42
|
-
|
|
42
|
+
this.app().action(event, handler);
|
|
43
|
+
break;
|
|
43
44
|
case decorators_1.EventTypes.Event:
|
|
44
|
-
|
|
45
|
-
handler);
|
|
45
|
+
// todo improve by providing types for events names
|
|
46
|
+
this.app().event(event, handler);
|
|
47
|
+
break;
|
|
46
48
|
case decorators_1.EventTypes.Command:
|
|
47
|
-
|
|
49
|
+
this.app().command(event, handler);
|
|
50
|
+
break;
|
|
48
51
|
case decorators_1.EventTypes.Option:
|
|
49
|
-
|
|
52
|
+
this.app().options(event, handler);
|
|
53
|
+
break;
|
|
54
|
+
default:
|
|
55
|
+
throw new Error(`Unknown event type ${type}`);
|
|
50
56
|
}
|
|
57
|
+
this.logger.log(`Handler of type [${type}] registered with id(${event})`);
|
|
51
58
|
}
|
|
52
59
|
/**
|
|
53
60
|
* Get/Create Bolt app instance
|
package/utils.d.ts
CHANGED
package/utils.js
CHANGED
|
@@ -6,7 +6,7 @@ exports.adjustLogger = adjustLogger;
|
|
|
6
6
|
const microservices_1 = require("@nestjs/microservices");
|
|
7
7
|
const bolt_1 = require("@slack/bolt");
|
|
8
8
|
const normalizePattern = (pattern) => {
|
|
9
|
-
if (typeof pattern ===
|
|
9
|
+
if (typeof pattern === 'string') {
|
|
10
10
|
return pattern;
|
|
11
11
|
}
|
|
12
12
|
if (pattern instanceof RegExp) {
|
|
@@ -39,27 +39,33 @@ function messageDecorator(type, pattern) {
|
|
|
39
39
|
function adjustLogger(logger) {
|
|
40
40
|
// this patch required to support NestJS logger that not use the standard "info"
|
|
41
41
|
// and use "log" instead
|
|
42
|
-
if (logger[
|
|
43
|
-
logger[
|
|
42
|
+
if (logger['log'] && !logger['info']) {
|
|
43
|
+
logger['info'] = logger['log'].bind(logger);
|
|
44
44
|
}
|
|
45
45
|
// minimal logger interface required by @slack/bolt library
|
|
46
|
-
for (const method of [
|
|
47
|
-
if (typeof logger[method] !==
|
|
46
|
+
for (const method of ['debug', 'info', 'warn', 'error']) {
|
|
47
|
+
if (typeof logger[method] !== 'function') {
|
|
48
48
|
throw new Error(`Logger must implement "${method}" method`);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
// add compatibility with @slack/bolt library
|
|
52
|
+
if (!logger['setLevel']) {
|
|
53
|
+
logger.setLevel = () => {
|
|
53
54
|
// don't allow library to set the level
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
},
|
|
59
|
-
getLevel() {
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (!logger['getLevel']) {
|
|
58
|
+
logger.getLevel = () => {
|
|
60
59
|
// always return the lowest supported level (by library, not real logger)
|
|
61
60
|
// to avoid filtering log messages by library instead of real logger
|
|
62
61
|
return bolt_1.LogLevel.DEBUG;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (!logger['setName']) {
|
|
65
|
+
logger.setName = (name) => {
|
|
66
|
+
// search for all possible setName/setContext methods, use "noop" if none
|
|
67
|
+
(logger.setName ?? logger.setContext ?? ((i) => i))(name);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return logger;
|
|
65
71
|
}
|