【问题标题】:Problems with creatWriteStream for .js.js 的 creatWriteStream 问题
【发布时间】:2016-09-09 10:54:31
【问题描述】:

我对 javascript 中的 fs.createWriteStream 有问题:

我正在尝试使用 Watson 服务(语音到文本、翻译、文本到语音)构建一个电报机器人,它在输入中接收语音消息,将其翻译成文本,将文本作为消息发送给机器人;然后文本将被翻译成意大利语,翻译将作为消息发送给机器人;最后,翻译将作为文本到语音的输入传递,并将转换为音频 .ogg 文件,然后发送给机器人。前两个步骤没有错,但是当我必须将音频文件发送到机器人时,问题就来了。

以下是imo有问题的代码部分:

text_to_speech.synthesize(params1).pipe(fs.createWriteStream('hello.ogg'));
      bot.sendVoice(chatId, 'hello.ogg', {
      disable_notification: true,
      reply_to_message_id: msg.message_id
     })

控制台说:

Unhandled rejection Error: 400 {"ok":false,"error_code":400,"description":"Bad Request: File must be non-empty"}

但该文件在目录中,并且有效地包含了我期望的音频。

我什至尝试使用 .on() 方法和我创建的 fileStream 的“完成”事件,但这没有帮助。我使用“node-telegram-bot-api”作为 bot 的 API。

感谢您的帮助

更新

非常感谢您的回答。没想到那个解决方案。我已经实施了您的建议,结果是:

bot.sendVoice(chatId, text_to_speech.synthesize(params1), {
        disable_notification: true,
        reply_to_message_id: msg.message_id
      }).then(function(promise){console.log(promise)}) 

应用程序运行过程中未发生错误,但未向机器人发送任何内容。其他提示? (对不起我的菜鸟,但我是 javascript 新手)

上!

【问题讨论】:

  • 根据电报模块API,它也可以接受voice参数的流,那么为什么不直接传递流而不是先将其写入文件呢?例如:bot.sendVoice(chatId, text_to_speech.synthesize(params1), { ... });。您还需要正确处理 sendVoice() 返回的 Promise。

标签: javascript node.js filestream telegram-bot ibm-watson


【解决方案1】:

您需要使用 multipart/form-data 使用 multer、busboy 或 restler 模块发送文件

npm install restler

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2021-05-19
    • 2019-10-13
    • 2018-12-04
    • 2011-08-29
    相关资源
    最近更新 更多