【问题标题】:Use Slack api (Bolt for JavaScript) to upload a streamed response使用 Slack api (Bolt for JavaScript) 上传流式响应
【发布时间】:2022-10-19 00:53:17
【问题描述】:

我正在尝试构建一个应用程序,该应用程序在服务器上创建一个文件,然后将该文件的内容流式传输到客户端。

我在客户端有如下代码:

app.event('app_home_opened', async ({ event, client, context }) => {
  try {
    const response: AxiosResponse<fs.ReadStream> = await axios({
      method: 'post',
      url: `${process.env.SOME_URL}/create`,
      data: {},
      headers: {},
      responseType: 'stream',
    });

    app.client.files.upload({
      file: response.data,
      channels: event.channel,
    });

  } catch (e) {
    console.error(e);
  }
});

在服务器上:

router.post(
  '/create',
  async (
    req: Request<any, any, any>,
    res: Response<Stream>,
    next: NextFunction
  ) => {
    try {
      const stream = fs.createReadStream('path/to/some/file.csv');

      stream
        .on('data', function (chunk: any) {
          console.log(chunk);
        })
        .on('end', function () {
          stream.pipe(res);
          next();
        })

        .on('close', function (err: any) {
          console.log('Stream has been Closed');
        });
    } catch (e) {
      next(e);
    }
  }

);

发布请求返回此错误:

(node:70966) UnhandledPromiseRejectionWarning: Error: An API error occurred: no_file_data

Typescript 编译得很好,所以我不明白我做错了什么。任何帮助将非常感激。

【问题讨论】:

    标签: file stream upload slack bolt


    【解决方案1】:

    Slack 客户端的上传方法是上传文件,但您似乎已经使用路径/create 上传文件。我猜您希望将文件保存在您的服务器和 Slack 存储中。

    在这种情况下,您需要将文件流传递给client.upload,因此您的异步调用应该返回它或文件路径;

    这可能是一个可能的实现:

    try{
      const response = await post.create('/create', params);
      if(response.data.filePath)
      {
        const stream = fs.createReadStream(response.data.filePath);
        app.client.files.upload({
          file: stream,
          channels: event.channel,
        });
      }
    }catch(error){
      console.error(error);
    }
    

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 2020-08-26
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 2021-11-04
      • 2012-04-02
      • 2018-03-29
      相关资源
      最近更新 更多