2017-01-13 90 views
0

我想在使用Skype將音頻附件發送到我的node.js chatbot時,使用Bing Speech Recognition API將語音轉換爲文本。我嘗試過使用BotBuilder-Samples intelligence-SpeechToText的代碼,但語音識別只能在模擬器中使用。在Skype中發送音頻/波形文件時,機器人完全不響應,而不是「你說:天氣怎麼樣?」。在Skype上使用Bing語音識別API和node.js Bot Framework

我懷疑這個問題可能是由於JWT令牌需要訪問Skype中的附件。因此,我嘗試使用BotBuilder-Samples core-ReceiveAttachment中的代碼訪問Skype中的音頻附件,該代碼使用request-promise而不是needle來發出HTTP請求。但是,來自request-promise的結果不是流,不能由函數getTextFromAudioStream()處理。

我想問一下如何讓語音識別與Skype中的音頻附件配合使用。

謝謝,最好的問候!

// Add your requirements 
 
var restify = require("restify"); 
 
var builder = require("botbuilder"); 
 
var fs = require("fs"); 
 
var needle = require("needle"); 
 
var request = require("request"); 
 
var speechService = require("./speech-service.js"); 
 
var Promise = require('bluebird'); 
 
var request = require('request-promise').defaults({ encoding: null }); 
 

 
//========================================================= 
 
// Bot Setup 
 
//========================================================= 
 

 
// Setup Restify Server 
 
var server = restify.createServer(); 
 
server.listen(process.env.PORT || 3000, function() { 
 
    console.log("%s listening to %s", server.name, server.url); 
 
}); 
 

 
// Create chat bot 
 
var connector = new builder.ChatConnector ({ 
 
    appId: process.env.MICROSOFT_APP_ID, 
 
    appPassword: process.env.MICROSOFT_APP_PASSWORD 
 
}); 
 

 
server.post("/api/messages", connector.listen()); 
 

 
var bot = new builder.UniversalBot(connector); 
 

 
//========================================================= 
 
// Bots Middleware 
 
//========================================================= 
 

 
// Anytime the major version is incremented any existing conversations will be restarted. 
 
bot.use(builder.Middleware.dialogVersion({ version: 1.0, resetCommand: /^reset/i })); 
 

 
//========================================================= 
 
// Bots Dialogs 
 
//========================================================= 
 

 
bot.dialog("/", [ 
 
    function (session, results, next) { 
 
     var msg = session.message; 
 

 
     if (hasAudioAttachment(msg)) { 
 
      // Message with attachment, proceed to download it. 
 
      // Skype attachment URLs are secured by a JwtToken, so we need to pass the token from our bot. 
 
      var attachment = msg.attachments[0]; 
 
      var fileDownload = isSkypeMessage(msg) 
 
       ? requestWithToken(attachment.contentUrl) 
 
       : request(attachment.contentUrl); 
 

 
      fileDownload.then(
 
       function (response) { 
 
        // Send reply with attachment type & size 
 
        var reply = new builder.Message(session) 
 
         .text('Attachment from %s of %s type and size of %s bytes received.', msg.source, attachment.contentType, response.length); 
 
        session.send(reply); 
 
       }).catch(function (err) { 
 
        console.log('Error downloading attachment:', { statusCode: err.statusCode, message: err.response.statusMessage }); 
 
      }); 
 

 
      var stream = isSkypeMessage(msg) 
 
       ? getAudioStreamWithToken(attachment) 
 
       : getAudioStream(attachment); 
 

 
      speechService.getTextFromAudioStream(stream) 
 
       .then(text => { 
 
        session.send("You said: " + text); 
 
       }) 
 
       .catch(error => { 
 
        session.send("Oops! Something went wrong. Try again later."); 
 
        console.error(error); 
 
       }); 
 
     } 
 
     else { 
 
      session.send("Did you upload an audio file? I'm more of an audible person. Try sending me a wav file"); 
 
     } 
 
    } 
 
]); 
 

 
function getAudioStream(attachment) { 
 
    return needle.get(attachment.contentUrl, { headers: {'Content-Type': "audio/wav"} }); 
 
} 
 

 
function getAudioStreamWithToken(attachment) { 
 
    var headers = {}; 
 

 
    connector.getAccessToken((error, token) => { 
 
     headers['Authorization'] = 'Bearer ' + token; 
 
    }); 
 

 
    headers['Content-Type'] = attachment.contentType; 
 

 
    return needle.get(attachment.contentUrl, { headers: headers }); 
 
} 
 

 
// Request file with Authentication Header 
 
function requestWithToken(url) { 
 
    return obtainToken().then(function (token) { 
 
     return request({ 
 
      url: url, 
 
      headers: { 
 
       'Authorization': 'Bearer ' + token, 
 
       'Content-Type': 'application/octet-stream' 
 
      } 
 
     }); 
 
    }); 
 
}; 
 

 
// Promise for obtaining JWT Token (requested once) 
 
var obtainToken = Promise.promisify(connector.getAccessToken.bind(connector)); 
 

 
function isSkypeMessage(message) { 
 
    return message.source === "skype"; 
 
};

回答

0

樣品中的代碼訪問的附件(見here)當已經在考慮的Skype。我認爲你遇到的問題是因爲樣本中的關鍵字超過了配額。昨天,一個新的Bing Speech Key被添加到樣本中,所以我建議你再試一次。

此外,樣本的更新版本即將添加。該代碼目前在code review之下。

+0

嗨Ezequiel,你指的是Bing Speech API密鑰?因爲我正在使用從Azure獲得的密鑰,所以配額不應引起問題。另外,當我在模擬器中發送附件時,語音識別功能正在工作。 – DevastatingCritical

+0

我剛剛嘗試了在Skype中的存儲庫中的node.js示例,它工作正常。試試即將推出的版本,看看它是否工作。你粘貼的代碼似乎是你的代碼,而不是樣本中的代碼 –

+0

嗨Ezequiel ...非常感謝!我剛剛重新檢查了我在Azure中的機器人網絡應用程序中輸入的Speech API密鑰......我意外地使用了錯誤的密鑰。我在本地存儲的密鑰是​​正確的,這就是爲什麼它在模擬器中而不是在Skype中工作。 – DevastatingCritical