Skip to content

Commit

Permalink
fix: trim messages longer than 3000 chars (#8219)
Browse files Browse the repository at this point in the history
## About the changes
When trying to send messages longer than 3000 chars we get this error:
```
[ERROR]  web-api:WebClient:0 failed to match all allowed schemas [json-pointer:/blocks/0/text]
[ERROR]  web-api:WebClient:0 must be less than 3001 characters [json-pointer:/blocks/0/text/text]
[2024-09-23T10:10:15.676] [WARN] addon/slack-app - All (1) Slack client calls failed with the following errors: A platform error occurred: {"ok":false,"error":"invalid_blocks","errors":["failed to match all allowed schemas [json-pointer:/blocks/0/text]","must be less than 3001 characters [json-pointer:/blocks/0/text/text]"],"response_metadata":{"messages":["[ERROR] failed to match all allowed schemas [json-pointer:/blocks/0/text]","[ERROR] must be less than 3001 characters [json-pointer:/blocks/0/text/text]"],"scopes":["incoming-webhook","users:read","channels:read","groups:read","mpim:read","im:read","users:read.email","chat:write"],"acceptedScopes":["chat:write"]}}
```

This PR trims the text length to 3000 chars.

We also upgrade slack API due to some security fixes:
https://github.com/slackapi/node-slack-sdk/releases/tag/%40slack%2Fweb-api%407.3.4

After checking the migration guide to v7 it seems that none of the
breaking changes affect us:
https://github.com/slackapi/node-slack-sdk/wiki/Migration-Guide-for-web%E2%80%90api-v7

## Testing
I did manual test this integration and the fix. The way to reproduce is
adding a very long strategy name and sending that as an update on Slack:

![image](https://github.com/user-attachments/assets/81df9554-f59b-4fa4-96c8-77dbf7a56257)

Now the event succeeds and we notice on the integration event log that
the message was trimmed:

![image](https://github.com/user-attachments/assets/79efc536-3dd3-4090-99f3-5fd4c7a88715)

---------

Co-authored-by: Nuno Góis <[email protected]>
  • Loading branch information
gastonfournier and nunogois authored Sep 24, 2024
1 parent 7b686ed commit 7d916e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 66 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
]
},
"dependencies": {
"@slack/web-api": "^6.10.0",
"@slack/web-api": "^7.3.4",
"@wesleytodd/openapi": "^1.1.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
Expand Down
7 changes: 5 additions & 2 deletions src/lib/addons/slack-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ export default class SlackAppAddon extends Addon {
this.accessToken = accessToken;
}

const { text, url } = this.msgFormatter.format(event);
message = text;
const { text: formattedMessage, url } =
this.msgFormatter.format(event);
const maxLength = 3000;
const text = formattedMessage.substring(0, maxLength);
message = `${formattedMessage}${text.length < formattedMessage.length ? ` (trimmed to ${maxLength} characters)` : ''}`;

const blocks: (Block | KnownBlock)[] = [
{
Expand Down
5 changes: 3 additions & 2 deletions src/lib/addons/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ export default class SlackAddon extends Addon {
}
}

const { text, url: featureLink } = this.msgFormatter.format(event);

const { text: formattedMessage, url: featureLink } =
this.msgFormatter.format(event);
const text = formattedMessage.substring(0, 3000);
const requests = slackChannels.map((channel) => {
const body = {
username,
Expand Down
98 changes: 37 additions & 61 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1616,38 +1616,39 @@ __metadata:
languageName: node
linkType: hard

"@slack/logger@npm:^3.0.0":
version: 3.0.0
resolution: "@slack/logger@npm:3.0.0"
"@slack/logger@npm:^4.0.0":
version: 4.0.0
resolution: "@slack/logger@npm:4.0.0"
dependencies:
"@types/node": "npm:>=12.0.0"
checksum: 10c0/af6394486be633ec86660b0cc980222f8371fc79f15a649aebc4df358fd6da35ef98c088c709626262c26d67d039e98a34fb52d3739f91817e3aae0099e491be
"@types/node": "npm:>=18.0.0"
checksum: 10c0/32c4b1f3b4a832a506b7661855d1da88eae307334563916b7513748171545a120abaaca5146a8beed1130b44c9e92f37511010b1205710baa778f5626cc2f7fa
languageName: node
linkType: hard

"@slack/types@npm:^2.11.0":
version: 2.11.0
resolution: "@slack/types@npm:2.11.0"
checksum: 10c0/b93b36b17b40d737f0c1c7da504dd5bab8ef84ff1b59378a72bcfc0d58ccf1f0f82ea22a3c3850cbbe1d8572e0724aa96282a390b794834d7a65d763ae13e5e1
"@slack/types@npm:^2.9.0":
version: 2.14.0
resolution: "@slack/types@npm:2.14.0"
checksum: 10c0/2d45b36ee128e202b8b864fa35f5752c9f811a433d598461de83c47bf5fc5c1f45678825b4185007d8151ebb8b0ce00e6541780f4f3af5698855aebfc6550fb8
languageName: node
linkType: hard

"@slack/web-api@npm:^6.10.0":
version: 6.12.1
resolution: "@slack/web-api@npm:6.12.1"
"@slack/web-api@npm:^7.3.4":
version: 7.5.0
resolution: "@slack/web-api@npm:7.5.0"
dependencies:
"@slack/logger": "npm:^3.0.0"
"@slack/types": "npm:^2.11.0"
"@types/is-stream": "npm:^1.1.0"
"@types/node": "npm:>=12.0.0"
"@slack/logger": "npm:^4.0.0"
"@slack/types": "npm:^2.9.0"
"@types/node": "npm:>=18.0.0"
"@types/retry": "npm:0.12.0"
axios: "npm:^1.7.4"
eventemitter3: "npm:^3.1.0"
form-data: "npm:^2.5.0"
eventemitter3: "npm:^5.0.1"
form-data: "npm:^4.0.0"
is-electron: "npm:2.2.2"
is-stream: "npm:^1.1.0"
p-queue: "npm:^6.6.1"
p-retry: "npm:^4.0.0"
checksum: 10c0/43301aa33cd2cb062b3e725ba1af836521e747d43bb713280e3b00b645b7a9d04bfa997836e1a40170db2b4356e627a22c6dd84c897df8712d87bd515047160a
is-stream: "npm:^2"
p-queue: "npm:^6"
p-retry: "npm:^4"
retry: "npm:^0.13.1"
checksum: 10c0/067af68cb4aa823a13c328ae6db60ad22e41a97cdc2b69836176980a3075026c563f131b62f50ca9176481a0f6a4667e969fef783c63ea9cc1f206104fd20177
languageName: node
linkType: hard

Expand Down Expand Up @@ -1992,15 +1993,6 @@ __metadata:
languageName: node
linkType: hard

"@types/is-stream@npm:^1.1.0":
version: 1.1.0
resolution: "@types/is-stream@npm:1.1.0"
dependencies:
"@types/node": "npm:*"
checksum: 10c0/88e3ec1868b3568576f1a194beda9a5a2d5fff24824d6793e316d7efe64dc597eb48d2f0b110512752c1248ceae806305a9d52308cf053f2170d0fd74f7dee8a
languageName: node
linkType: hard

"@types/istanbul-lib-coverage@npm:*, @types/istanbul-lib-coverage@npm:^2.0.0, @types/istanbul-lib-coverage@npm:^2.0.1":
version: 2.0.4
resolution: "@types/istanbul-lib-coverage@npm:2.0.4"
Expand Down Expand Up @@ -2138,7 +2130,7 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:*, @types/node@npm:>=12.0.0":
"@types/node@npm:*":
version: 20.11.17
resolution: "@types/node@npm:20.11.17"
dependencies:
Expand All @@ -2156,6 +2148,15 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:>=18.0.0":
version: 22.5.5
resolution: "@types/node@npm:22.5.5"
dependencies:
undici-types: "npm:~6.19.2"
checksum: 10c0/ead9495cfc6b1da5e7025856dcce2591e9bae635357410c0d2dd619fce797d2a1d402887580ca4b336cb78168b195224869967de370a23f61663cf1e4836121c
languageName: node
linkType: hard

"@types/nodemailer@npm:6.4.15":
version: 6.4.15
resolution: "@types/nodemailer@npm:6.4.15"
Expand Down Expand Up @@ -4110,13 +4111,6 @@ __metadata:
languageName: node
linkType: hard

"eventemitter3@npm:^3.1.0":
version: 3.1.2
resolution: "eventemitter3@npm:3.1.2"
checksum: 10c0/c67262eccbf85848b7cc6d4abb6c6e34155e15686db2a01c57669fd0d44441a574a19d44d25948b442929e065774cbe5003d8e77eed47674fbf876ac77887793
languageName: node
linkType: hard

"eventemitter3@npm:^4.0.4":
version: 4.0.7
resolution: "eventemitter3@npm:4.0.7"
Expand Down Expand Up @@ -4525,17 +4519,6 @@ __metadata:
languageName: node
linkType: hard

"form-data@npm:^2.5.0":
version: 2.5.1
resolution: "form-data@npm:2.5.1"
dependencies:
asynckit: "npm:^0.4.0"
combined-stream: "npm:^1.0.6"
mime-types: "npm:^2.1.12"
checksum: 10c0/7e8fb913b84a7ac04074781a18d0f94735bbe82815ff35348803331f6480956ff0035db5bcf15826edee09fe01e665cfac664678f1526646a6374ee13f960e56
languageName: node
linkType: hard

"form-data@npm:^3.0.0":
version: 3.0.1
resolution: "form-data@npm:3.0.1"
Expand Down Expand Up @@ -5329,14 +5312,7 @@ __metadata:
languageName: node
linkType: hard

"is-stream@npm:^1.1.0":
version: 1.1.0
resolution: "is-stream@npm:1.1.0"
checksum: 10c0/b8ae7971e78d2e8488d15f804229c6eed7ed36a28f8807a1815938771f4adff0e705218b7dab968270433f67103e4fef98062a0beea55d64835f705ee72c7002
languageName: node
linkType: hard

"is-stream@npm:^2.0.0":
"is-stream@npm:^2, is-stream@npm:^2.0.0":
version: 2.0.1
resolution: "is-stream@npm:2.0.1"
checksum: 10c0/7c284241313fc6efc329b8d7f08e16c0efeb6baab1b4cd0ba579eb78e5af1aa5da11e68559896a2067cd6c526bd29241dda4eb1225e627d5aa1a89a76d4635a5
Expand Down Expand Up @@ -7296,7 +7272,7 @@ __metadata:
languageName: node
linkType: hard

"p-queue@npm:^6.6.1":
"p-queue@npm:^6":
version: 6.6.2
resolution: "p-queue@npm:6.6.2"
dependencies:
Expand All @@ -7306,7 +7282,7 @@ __metadata:
languageName: node
linkType: hard

"p-retry@npm:^4.0.0":
"p-retry@npm:^4":
version: 4.6.2
resolution: "p-retry@npm:4.6.2"
dependencies:
Expand Down Expand Up @@ -9427,7 +9403,7 @@ __metadata:
"@babel/core": "npm:7.25.2"
"@biomejs/biome": "npm:^1.8.3"
"@cyclonedx/yarn-plugin-cyclonedx": "npm:^1.0.0-rc.7"
"@slack/web-api": "npm:^6.10.0"
"@slack/web-api": "npm:^7.3.4"
"@swc/core": "npm:1.7.26"
"@swc/jest": "npm:0.2.36"
"@types/bcryptjs": "npm:2.4.6"
Expand Down

0 comments on commit 7d916e9

Please sign in to comment.