bun
bun copied to clipboard
@slack/web-api bug, not resolving promise for web.chat.postMessage()
What version of Bun is running?
1.0.24+6fa35839c
What platform is your computer?
Darwin 23.2.0 arm64 arm
What steps can reproduce the bug?
calling the web.chat.postMessage function will not resolve a promise however the message does successfully get posted to Slack. When running this function in Node the promise resolves as expected.
import { WebClient } from "@slack/web-api";
const main = async () => {
const web = new WebClient(Bun.env.SLACK_TOKEN);
try {
console.log("start slack test");
const response = await web.chat.postMessage({
channel: Bun.env.SLACK_CHANNEL,
text: "test",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "test",
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: "test",
},
},
],
});
console.log("response from slack test", response);
} catch (error) {
console.log("error from slack test", error);
}
};
main();
What is the expected behavior?
to receive a JSON reply like so
{ ok: true, channel: 'XXXXXX', ts: '1705793121.715479', message: { bot_id: 'XXXXXX', type: 'message', text: 'test', user: 'XXXXXX', ts: '1705793121.715479', app_id: 'XXXXXX', blocks: [ [Object], [Object] ], team: 'XXXXXX', bot_profile: { id: 'XXXXXX', app_id: 'XXXXXX', name: 'Test Bot', icons: [Object], deleted: false, updated: 1692580490, team_id: 'XXXXX' } }, response_metadata: { scopes: [ 'chat:write' ], acceptedScopes: [ 'chat:write' ] } }
What do you see instead?
no response is received.
Additional information
No response
i encountered another bug related to the same library (issue #8924).
for me web.chat.postMessage()
works in bun but some other api calls do not.
With bun, the web.chat.postMessage()
works, but the script is killed just after, without any error...
The same script works perfectly with ts-node
.
I don't understand which part of this library kill bun.
Decided to implement directly raw http call to slack API, and not use their web-client
.
@0xVinCal do you have an example of this raw http call?
seems like the slack web api is using axios
this line leads me to think this is caused by issue #4871
@seanwessmith
import type { ChatPostMessageResponse } from '@slack/web-api';
import axios from 'axios';
import { config } from '../../config';
/**
* Send a message to a Slack channel
*/
export async function sendSlackMessage(channel: string, text: string): Promise<ChatPostMessageResponse> {
const result = await axios.post(
`https://slack.com/api/chat.postMessage`,
{
channel: channel,
text: text,
},
{
headers: {
Authorization: `Bearer ${config.slack.apiToken}`,
Accept: 'application/json',
'Accept-Encoding': 'identity',
},
},
);
if (!result.data.ok) {
throw new Error(`Failed to send Slack message: ${result.data.error}`);
}
return result.data;
}
i took a look at this again. when setting maxRedirects
to 1
or default by removing the property altogether, i hit another issue: bun doesn't support brotli compression. keep an eye on https://bun.sh/docs/runtime/nodejs-apis#node-zlib.
does this still reproduce for you? brotli support was added in Bun 1.1.8 and some http improvements made it into Bun 1.1.9.
this is now working in Bun 1.1.9!
import type { ChatPostMessageResponse } from "@slack/web-api";
import axios from "axios";
/**
* Send a message to a Slack channel
*/
export async function sendSlackMessage(
channel: string,
text: string
): Promise<ChatPostMessageResponse> {
const result = await axios.post(
`https://slack.com/api/chat.postMessage`,
{
channel: channel,
text: text,
},
{
headers: {
Authorization: `Bearer ${process.env.SLACK_BOT_TOKEN}`,
Accept: "application/json",
"Accept-Encoding": "identity",
},
}
);
if (!result.data.ok) {
throw new Error(`Failed to send Slack message: ${result.data.error}`);
}
return result.data;
}
sendSlackMessage("orders", "Hello, World!").then((response) => {
console.log(response);
});
thanks yall
tested my repro case and this works in bun 1.1.10