Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/web-api/src/WebClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs';
import type { ContextActionsBlock } from '@slack/types';
import axios, { type InternalAxiosRequestConfig } from 'axios';
import { assert, expect } from 'chai';
import nock, { type ReplyHeaders } from 'nock';
Expand Down Expand Up @@ -1245,6 +1246,36 @@ describe('WebClient', () => {
});

it('streams a long message', async () => {
const contextActionsBlock: ContextActionsBlock = {
type: 'context_actions',
elements: [
{
type: 'feedback_buttons',
positive_button: {
text: {
type: 'plain_text',
text: 'good',
},
value: '+1',
},
negative_button: {
text: {
type: 'plain_text',
text: 'bad',
},
value: '-1',
},
},
{
type: 'icon_button',
icon: 'trash',
text: {
type: 'plain_text',
text: 'delete',
},
},
],
};
const scope = nock('https://slack.com')
.post('/api/chat.startStream', {
channel: 'C0123456789',
Expand All @@ -1267,6 +1298,7 @@ describe('WebClient', () => {
ok: true,
})
.post('/api/chat.stopStream', {
blocks: JSON.stringify([contextActionsBlock]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Thanks for testing the blocks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mwbrooks This was such a good suggestion! I forget why this might've been skipped this in a first pass...

channel: 'C0123456789',
markdown_text: '**',
token: 'xoxb-updated-2',
Expand Down Expand Up @@ -1296,6 +1328,7 @@ describe('WebClient', () => {
markdown_text: '*',
});
await streamer.stop({
blocks: [contextActionsBlock],
markdown_text: '*',
token: 'xoxb-updated-2',
});
Expand Down
9 changes: 6 additions & 3 deletions packages/web-api/src/chat-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ChatStreamer {
}

/**
* Append to a stream.
* Append to the stream.
*
* @description The "append" method appends to the chat stream being used. This method can be called multiple times. After the stream is stopped this method cannot be called.
* @example
Expand Down Expand Up @@ -106,7 +106,7 @@ export class ChatStreamer {
}

/**
* Stop a stream.
* Stop the stream and finalize the message.
*
* @description The "stop" method stops the chat stream being used. This method can be called once to end the stream. Additional "blocks" and "metadata" can be provided.
*
Expand All @@ -130,6 +130,9 @@ export class ChatStreamer {
if (args?.token) {
this.token = args.token;
}
if (args?.markdown_text) {
this.buffer += args.markdown_text;
}
Comment on lines +133 to +135
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: This is a cleaner and more readable approach!

if (!this.streamTs) {
const response = await this.client.chat.startStream({
...this.streamArgs,
Expand All @@ -146,7 +149,7 @@ export class ChatStreamer {
channel: this.streamArgs.channel,
ts: this.streamTs,
...args,
markdown_text: this.buffer + (args?.markdown_text ?? ''),
markdown_text: this.buffer,
});
this.state = 'completed';
return response;
Expand Down