如何通过WhatsApp API下载语音留言?

问题描述 投票:0回答:1

我有一个端点,应该使用媒体 URL 调用 WhatsApp API,就像他们在这里所说的那样:https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media #download-media,然后按照从whatsapp返回的方式返回它,并保存文件以供以后使用。

   exports.downLoadMedia = async (req, res) => {
  const url = req.query.url;

  request.get(
    {
      url: url,
      headers: {
        Authorization: `Bearer ${process.env.AUTH_TOKEN}`,
        Accept: 'audio/ogg',
      },
      encoding: null, // Set encoding to null to receive binary data
    },
    function (err, resp, body) {
      if (err) {
        console.log("Error:", err);
        res.status(500).json({ error: "An error occurred." });
      } else {
          const fileName = 'downloaded_audio.ogg';
          const filePath = path.join(__dirname, fileName); // Construct file path

          fs.writeFile(filePath, body, 'binary', (writeErr) => {
            if (writeErr) {
              console.log("Error writing file:", writeErr);
              res.status(500).json({ error: "An error occurred while saving the audio." });
            } else {
              // Use res.download() to send the saved file as a download response
              res.download(filePath, 'downloaded_audio.ogg', (downloadErr) => {
                if (downloadErr) {
                  console.log("Error sending download:", downloadErr);
                  res.status(500).json({ error: "An error occurred while sending the download." });
                }
              });
            }
          });
      }
    }
  );
};

当我直接使用 WhatsApp Media url 在 Postman 中发出 get 请求时,它会显示如下:

并且音频会按预期播放。

但是,当我向上面的端点发出请求时,它会显示如下:

音频已保存,但无法播放。

基本上,我的端点应该是获取音频/语音笔记文件的代理。

node.js express audio download whatsapp-cloud-api
1个回答
0
投票

同样的问题。我实际上收到的是粘贴在 .ogg 文件中的 HTML,其中包含以下内容https://goonlinetools.com/html-viewer/#ltk64rzrvcjrm18bvbae

使用邮递员时,一切正常 - 真的很难知道到底出了什么问题。标题都是一样的

© www.soinside.com 2019 - 2024. All rights reserved.