Javascript 方法sendVoice()电报bot-文件为空
我有以下JavaScript代码:Javascript 方法sendVoice()电报bot-文件为空,javascript,telegram-bot,Javascript,Telegram Bot,我有以下JavaScript代码: bot.sendVoice(chatId, 'C:/Users/a.repetto/Desktop/repettobot/hello_world.ogg', { disable_notification: true, reply_to_message_id: msg.message_id }).then(function () { // reply sent! }); 该文件存在并包含音频,但当我尝试运行整个代码
bot.sendVoice(chatId, 'C:/Users/a.repetto/Desktop/repettobot/hello_world.ogg', {
disable_notification: true,
reply_to_message_id: msg.message_id
}).then(function () {
// reply sent!
});
该文件存在并包含音频,但当我尝试运行整个代码时,出现以下错误:
“错误代码”:400,“说明”:“错误请求:文件必须为非空”
我也尝试过使用sendAudio
,但这也不起作用
编辑1:更具体地说
如果我只使用“sendAudio”或“sendVoice”编写脚本,文件将正确发送到电报。但如果我使用此脚本,文件结果为空:
var Bot = require('node-telegram-bot-api');
var watson = require('watson-developer-cloud');
var request = require('request');
var fs = require('fs');
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
var speech_to_text = watson.speech_to_text({
username: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX, //username from VCAPSERVICES
password: 'XXXXXXXX', //password from VCAP SERVICES
version: 'v1',
url: 'https://stream.watsonplatform.net/speech-to-text/api'
});
var language_translation = watson.language_translator({
username: 'XXXXXXXXXXXXXXXXXX',
password: 'XXXXXXXX',
version: 'v2'
});
var text_to_speech = watson.text_to_speech({
username: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
password: 'XXXXXXXXXX',
version: 'v1'
});
var params = {
model: 'en-US_BroadbandModel', //you can change the language here
content_type: 'audio/ogg;codecs=opus',
continuous: true,
interim_results: false
};
var bot = new Bot('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', {polling: true }); //token generated by the bot you created
bot.on('message', function (msg) {
if(msg['voice']){ return onVoiceMessage(msg); }
});
function onVoiceMessage(msg) {
var chatId = msg.chat.id;
bot.getFileLink(msg.voice.file_id).then(function(link) {
var recognizeStream = speech_to_text.createRecognizeStream(params);
recognizeStream.setEncoding('utf8');
recognizeStream.on('results', function(data) {
var result = data.results[0].alternatives[0].transcript;
var c = result;
console.log("testo: " + result);
bot.sendMessage(chatId, result, {
disable_notification: true,
reply_to_message_id: msg.message_id
}).then(function(){})
language_translation.translate({
text: result,
source: 'en',
target: 'it'
}, function(err,translation) {
if (err) {
console.log(err);
}
else {
var trad = translation.translations[0].translation;
console.log("traduzione: " + trad + "\n")
}
bot.sendMessage(chatId, trad, {
disable_notification: true,
reply_to_message_id: msg.message_id
}).then(function(){})
var params1 = {
text: trad,
voice: 'it-IT_FrancescaVoice',
accept: 'audio/ogg;codecs=opus'
}
bot.sendVoice(chatId, text_to_speech.synthesize(params1), {
disable_notification: true,
reply_to_message_id: msg.message_id
}).then(function(promise){console.log(promise)})
})
})
request(link).pipe(recognizeStream);
})
}
电报无法从您的电脑发送文件。请先上载您的文件,然后用web地址替换本地地址。另一种方法是将文件发送到电报机器人并获取
文件id
(使用getUpdate
方法)并将file\u id
放入您的代码中。您使用哪个库作为电报API?我使用节点电报机器人API谢谢您的回答。为了更清楚,我对问题进行了编辑