Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
seanwessmith opened this issue Jan 20, 2024 · 10 comments
Closed
Assignees
Labels
bug Something isn't working needs investigate Needs to be investigated to find the root cause

Comments

@seanwessmith
Copy link

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

@jozan
Copy link

jozan commented Feb 15, 2024

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.

@0xVinCal
Copy link

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.

@seanwessmith
Copy link
Author

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

@RJWadley
Copy link

RJWadley commented Apr 2, 2024

seems like the slack web api is using axios

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

@0xVinCal
Copy link

0xVinCal commented Apr 5, 2024

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

@jozan
Copy link

jozan commented Apr 24, 2024

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.

@nektro nektro self-assigned this May 24, 2024
@nektro
Copy link
Contributor

nektro commented May 24, 2024

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.

@seanwessmith
Copy link
Author

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
Copy link
Author

thanks yall

@jozan
Copy link

jozan commented May 27, 2024

tested my repro case and this works in bun 1.1.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs investigate Needs to be investigated to find the root cause
Projects
None yet
Development

No branches or pull requests

6 participants