forked from snxraven/LinkUp-P2P-Chat
Adding proper support for audio messages via bot
This commit is contained in:
parent
2cf819553a
commit
487bc13970
10
chatBot/commands/test-audio.js
Normal file
10
chatBot/commands/test-audio.js
Normal file
@ -0,0 +1,10 @@
|
||||
export default {
|
||||
handler: function(bot, args, message) {
|
||||
// Specify the path to the audio file you want to send
|
||||
const audioFilePath = '/Users/raven/discord-linux.mp3'; // Replace with the actual audio file path
|
||||
const audioType = 'audio'; // Specify the correct audio file type
|
||||
|
||||
// Send the audio message using the bot instance
|
||||
bot.sendAudioMessage(audioFilePath, audioType);
|
||||
}
|
||||
};
|
@ -114,7 +114,7 @@ class Client extends EventEmitter {
|
||||
|
||||
if (msgType === "audio") {
|
||||
const audioBuffer = await this.drive.get(`/audio/${messageObj.audioName}`);
|
||||
this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, `http://localhost:${this.servePort}/audio/${messageObj.audioName}`, messageObj.audioType));
|
||||
this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, `http://localhost:${this.servePort}/audio/${messageObj.audioName}`, messageObj.audioType, messageObj.audioData));
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -163,6 +163,20 @@ class Client extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
async sendAudioMessage(filePath, audioType) {
|
||||
try {
|
||||
await this.drive.ready();
|
||||
const audioBuffer = fs.readFileSync(filePath);
|
||||
const audioName = path.basename(filePath);
|
||||
await this.drive.put(`/audio/${audioName}`, audioBuffer);
|
||||
const audioUrl = `http://localhost:${this.servePort}/audio/${audioName}`;
|
||||
const audioMessage = AudioMessage.new(this, audioUrl, audioType, audioBuffer); // Pass audioBuffer to the new method
|
||||
this.sendMessage(audioMessage);
|
||||
} catch (error) {
|
||||
console.error('Error sending audio message:', error);
|
||||
}
|
||||
}
|
||||
|
||||
sendMessage(message) {
|
||||
if (!(message instanceof Message)) {
|
||||
console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message);
|
||||
|
@ -1,22 +1,26 @@
|
||||
import Message from "./Message.js";
|
||||
import b4a from "b4a";
|
||||
|
||||
class AudioMessage extends Message {
|
||||
constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType) {
|
||||
constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType, audioData) {
|
||||
super("audio", peerName, peerAvatar, topic, timestamp);
|
||||
this.audioUrl = audioUrl;
|
||||
this.audioType = audioType;
|
||||
this.audioData = audioData; // Add audio data property
|
||||
}
|
||||
|
||||
toJsonString() {
|
||||
return JSON.stringify({
|
||||
...this.toJson(),
|
||||
audioUrl: this.audioUrl,
|
||||
audioType: this.audioType
|
||||
audioType: this.audioType,
|
||||
audio: this.audioData // Include audio data in JSON
|
||||
});
|
||||
}
|
||||
|
||||
static new(bot, audioUrl, audioType) {
|
||||
return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType);
|
||||
static new(bot, audioUrl, audioType, audioBuffer) {
|
||||
const audioData = b4a.toString(audioBuffer, 'base64'); // Convert audio buffer to base64
|
||||
return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType, audioData);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user