【问题标题】:Using Bing Speech Recognition API with node.js Bot Framework on Skype在 Skype 上使用 Bing 语音识别 API 和 node.js Bot Framework
【发布时间】:2017-01-13 15:50:41
【问题描述】:

在将 Skype 中的音频附件发送到我的 node.js 聊天机器人时,我想使用 Bing Speech Recognition API 将语音转换为文本。我尝试使用来自BotBuilder-Samples intelligence-SpeechToText 的代码,但是语音识别只在模拟器中有效。在 Skype 中发送音频/波形文件时,机器人根本没有响应,而是“你说:天气怎么样?”。

我怀疑问题可能是由于需要 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";
};

【问题讨论】:

    标签: node.js skype botframework


    【解决方案1】:

    示例中的代码在访问附件时已经考虑使用 Skype(请参阅here)。我认为您遇到的问题是因为示例中的密钥超出了配额。昨天在示例中添加了一个新的 Bing Speech Key,所以我建议您再试一次。

    此外,即将添加示例的更新版本。代码目前在code review下。

    【讨论】:

    • 嗨,Ezequiel,您指的是 Bing Speech API 密钥吗?因为我使用的是从 Azure 获得的密钥,所以配额应该不会导致问题。此外,当我在模拟器中发送附件时,语音识别正在工作。
    • 我刚刚尝试了 Skype 存储库中的 node.js 示例,它运行良好。尝试使用即将发布的版本,看看它是否有效。您粘贴的代码似乎是您的代码,而不是示例中的代码
    • 嗨,Ezequiel...非常感谢!!!我刚刚重新检查了在 Azure 中输入机器人 Web 应用程序的语音 API 密钥……我不小心使用了错误的密钥。我在本地存储的密钥是​​正确的,这就是它在模拟器而不是 Skype 中工作的原因。
    • 很高兴听到这个消息!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多