简体   繁体   English

无法将文件转换为二进制格式以使用node.js发送到wit.ai api

[英]Unable to convert file to binary format for sending to wit.ai api using node.js

I am facing an issue in converting Audio file to Binary format. 我在将音频文件转换为二进制格式时遇到问题。 I need to send it to Wit.AI api which is expecting the data in that format. 我需要将其发送到Wit.AI api,该api希望使用该格式的数据。 I am using node.js. 我正在使用node.js。 In my front-end I am recording the user voice using Mic-recorder Module. 在前端,我正在使用麦克风记录器模块记录用户语音。 Any suggestions are welcome. 欢迎任何建议。

My front end code: 我的前端代码:

var recorder;
function startRecording() {
    recorder = new MicRecorder({
        bitRate: 128
    });
    recorder.start()
}

function stopRecording() {
    recorder.stop().getMp3().then(([buffer, blob]) => {
        console.log(buffer, blob);
        const file = new File(buffer, 'music.mp3', {
            type: blob.type,
            lastModified: Date.now()
        })
        console.log(file)
        axios({
            method: 'post',
            url: `${appUrl}/open_api/voice/send?data=${buffer}`
        }).then(function (res) {
            console.log(res)
            if (res.data.success) {
                console.log('done',res)
            } else {
                console.log(res.data)
            }
        })
    })
};

After recording Successfully, I want to send the file to my api in order to call wit.ai /speech api. 成功录制后,我想将文件发送到我的api以便调用wit.ai / speech api。

My back end code is: 我的后端代码是:

router.post('/voice/send',                                //chatbot response api
    async (req, res, next) => {
        let thread_id = '99-99-99-99'
        let audioBinary = req.query.data
        console.log(audioBinary)
        let appId = "5d07621d6b79be66a73f4005"
        let sessionId ="10-10-10-10"
        let accessToken = await db.model('ChatBotApp').findOne({
            _id: req.query.key
        }, {
            access_token: 1
        }).lean() 
        var options = {

            method: 'POST',
            uri: 'https://api.wit.ai/speech?v=20190513',
            body : audioBinary,
            encoding: null,
            headers: {
                'Authorization': 'Bearer ' + "HY3ZWSUGPBPD5LWZLRSZ3QJCDC27M6EW",
                'Content-Type': 'audio/mpeg',
            },
            // json: true // Automatically stringifies the body to JSON
        };
        rp(options)
        .then(async function (parsedBody) {
            console.log('this called',parsedBody)
            return
            // let response = await firstEntityValue(parsedBody, appId, message, thread_id)
            // events.emit('Chats', appId, thread_id, message, sessionId, response);
            return res.apiOk(response)
        })
        .catch(function (err) {
            console.log(err)
            return res.apiError('Issue while creating app!', err);
        })


    }
)
 var recorder

   function startRecording() {
     recorder = new MicRecorder({
       bitRate: 128
     });
     recorder.start()
   }

   function stopRecording() {
     recorder.stop().getMp3().then(([buffer, blob]) => {
       console.log(buffer, blob);

       const file = new File(buffer, 'music.mp3', {
         type: blob.type,
         lastModified: Date.now()
       })
       var bodyFormData = new FormData();
       bodyFormData.append('file', file);
       console.log(file)

       axios({
         method: 'post',
         url: `${appUrl}/open_api/voice/send`,
         headers: {
           'Content-Type': 'multipart/form-data'
         },
         data: bodyFormData
       }).then(function (res) {
         console.log(res)
         if (res.data.success) {
           console.log('done', res)
         } else {
           console.log(res.data)
         }
       })
     })
   };


API 
router.post('/voice/send',upload.single('file'), //chatbot response api
        async (req, res, next) => {

            console.log(req.file)
            let thread_id = '99-99-99-99'
            let audioBinary = req.file.buffer
            let appId = "5d07621d6b79be66a73f4005"
            let sessionId = "10-10-10-10"
            let accessToken = await db.model('ChatBotApp').findOne({
                _id: req.query.key
            }, {
                access_token: 1
            }).lean()
            var options = {

                method: 'POST',
                uri: 'https://api.wit.ai/speech?v=20190513',
                headers: {
                    'Authorization': 'Bearer ' + "HY3ZWSUGPBPD5LWZLRSZ3QJCDC27M6EW",
                    'Content-Type': 'audio/mpeg',
                },
                body: audioBinary

                // json: true // Automatically stringifies the body to JSON
            };
            rp(options)
                .then(async function (parsedBody) {
                    console.log('this called', parsedBody)
                    return
                    // let response = await firstEntityValue(parsedBody, appId, message, thread_id)
                    // events.emit('Chats', appId, thread_id, message, sessionId, response);
                    return res.apiOk(response)
                })
                .catch(function (err) {
                    console.log(err)
                    return res.apiError('Issue while creating app!', err);
                })
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM