bun icon indicating copy to clipboard operation
bun copied to clipboard

@slack/web-api bug, not resolving promise for web.chat.postMessage()

Open seanwessmith opened this issue 1 year ago • 5 comments

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

seanwessmith avatar Jan 20 '24 23:01 seanwessmith

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.

jozan avatar Feb 15 '24 14:02 jozan

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 avatar Mar 25 '24 08:03 0xVinCal

@0xVinCal do you have an example of this raw http call?

seanwessmith avatar Mar 25 '24 13:03 seanwessmith

seems like the slack web api is using axios

this line leads me to think this is caused by issue #4871

RJWadley avatar Apr 02 '24 17:04 RJWadley

@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;
}


0xVinCal avatar Apr 05 '24 22:04 0xVinCal

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.

jozan avatar Apr 24 '24 16:04 jozan

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.

nektro avatar May 24 '24 03:05 nektro

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);
});

seanwessmith avatar May 24 '24 04:05 seanwessmith

thanks yall

seanwessmith avatar May 24 '24 04:05 seanwessmith

tested my repro case and this works in bun 1.1.10

jozan avatar May 27 '24 14:05 jozan