whatsapp-web.js 如何保存 downloadMedia()

时间:2021-06-08 05:03:18

标签: javascript node.js base64 fs

嗨,我是新来的,我有一个让我头疼的问题, 我正在使用 whatsapp-web.js 1.12.6,但是当我下载多媒体文件时,我找不到如何保存它... 我正在尝试使用 fs.writefile,但它抛出了一个错误 如何将该对象保存为文件?

const listenMessage = () => {
      client.on("message", async (inboundMsg) => {
        console.log(inboundMsg);
    
        const { from, to, body, hasMedia, mediaKey } = inboundMsg;
    
        if (hasMedia) {
          const mediafile = await inboundMsg.downloadMedia();
          console.log(
            mediafile.mimetype,
            mediafile.filename,
            mediafile.data.length
          );
            
          //How to save that object as a file? =====================================
    
          fs.writeFile('./upload/', JSON.stringify(mediafile.data), function (err) {
            if (err) {
              console.log(err);
            }
          });
    
          //========================================================================
    
    
          var post = {
            message: body,
            direction: "inbound",
            number: from,
            media: "mymediafile",
          };
          var query = pool.query(
            "INSERT INTO chat SET ?",
            post,
            function (error, results, fields) {
              if (error) throw error;
              console.log(from, to, body, hasMedia);
            }
          );
        } else {
          var post = {
            message: body,
            direction: "inbound",
            number: from,
          };
          var query = pool.query(
            "INSERT INTO chat SET ?",
            post,
            function (error, results, fields) {
              if (error) throw error;
              console.log(from, to, body);
            }
          );
        }
      });
    };

0 个答案:

没有答案