From 2cf819553a1694b0f1f089f98513eb0b0632a195 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sat, 15 Jun 2024 02:52:20 -0400 Subject: [PATCH] correct support for sending files via the bot --- chatBot/commands/ping.js | 13 ++++++---- chatBot/includes/Client.js | 31 +++++++++++++++++++----- chatBot/includes/message/AudioMessage.js | 10 ++++---- chatBot/includes/message/FileMessage.js | 12 ++++++--- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/chatBot/commands/ping.js b/chatBot/commands/ping.js index d30a588..f9e0044 100644 --- a/chatBot/commands/ping.js +++ b/chatBot/commands/ping.js @@ -1,7 +1,10 @@ -// ping.js - export default { - handler: function(bot, args, message) { - bot.sendTextMessage('Pong!'); - } + handler: function(bot, args, message) { + // Specify the path to the file you want to send + const filePath = '/Users/raven/chat/chatBot/commands/ping.js'; // Replace with the actual file path + const fileType = 'text/html'; // Specify the correct file type + + // Send the file message using the bot instance + bot.sendFileMessage(filePath, fileType); + } }; \ No newline at end of file diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 2391f2d..011d17e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -1,3 +1,4 @@ +import path from 'path'; import Hyperswarm from 'hyperswarm'; import EventEmitter from 'node:events'; import b4a from "b4a"; @@ -90,27 +91,31 @@ class Client extends EventEmitter { peer.write(this.iconMessage.toJsonString()); } - peer.on('data', message => { + peer.on('data', async message => { const messageObj = JSON.parse(message.toString()); if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room this.currentTopic = messageObj.topic; // Set the current topic from the incoming message const msgType = messageObj.type; - const peerName = messageObj.name; + const peerName = messageObj.name; // Changed from name to userName const peerAvatar = messageObj.avatar; const timestamp = messageObj.timestamp; if (msgType === "message") this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message)); - if (msgType === "file") - this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); + if (msgType === "file") { + const fileBuffer = await this.drive.get(`/files/${messageObj.fileName}`); + this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, `http://localhost:${this.servePort}/files/${messageObj.fileName}`, messageObj.fileType, messageObj.fileData)); + } if (msgType === "icon") this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp)); - if (msgType === "audio") - this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.audio, messageObj.audioType)); + 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)); + } } }); @@ -144,6 +149,20 @@ class Client extends EventEmitter { this.sendMessage(TextMessage.new(this, message)); } + async sendFileMessage(filePath, fileType) { + try { + await this.drive.ready(); + const fileBuffer = fs.readFileSync(filePath); + const fileName = path.basename(filePath); + await this.drive.put(`/files/${fileName}`, fileBuffer); + const fileUrl = `http://localhost:${this.servePort}/files/${fileName}`; + const fileMessage = FileMessage.new(this, fileName, fileUrl, fileType, fileBuffer); // Pass fileBuffer to the new method + this.sendMessage(fileMessage); + } catch (error) { + console.error('Error sending file message:', error); + } + } + sendMessage(message) { if (!(message instanceof Message)) { console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message); diff --git a/chatBot/includes/message/AudioMessage.js b/chatBot/includes/message/AudioMessage.js index 2fcb8a0..d1e2108 100644 --- a/chatBot/includes/message/AudioMessage.js +++ b/chatBot/includes/message/AudioMessage.js @@ -1,22 +1,22 @@ import Message from "./Message.js"; class AudioMessage extends Message { - constructor(peerName, peerAvatar, topic, timestamp, audio, audioType) { + constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType) { super("audio", peerName, peerAvatar, topic, timestamp); - this.audio = audio; + this.audioUrl = audioUrl; this.audioType = audioType; } toJsonString() { return JSON.stringify({ ...this.toJson(), - audio: this.audio, + audioUrl: this.audioUrl, audioType: this.audioType }); } - static new(bot, audio, audioType) { - return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audio, audioType); + static new(bot, audioUrl, audioType) { + return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType); } } diff --git a/chatBot/includes/message/FileMessage.js b/chatBot/includes/message/FileMessage.js index d62ff7b..a9be736 100644 --- a/chatBot/includes/message/FileMessage.js +++ b/chatBot/includes/message/FileMessage.js @@ -1,11 +1,13 @@ import Message from "./Message.js"; +import b4a from "b4a"; class FileMessage extends Message { - constructor(peerName, peerAvatar, topic, timestamp, fileName, fileUrl, fileType) { + constructor(peerName, peerAvatar, topic, timestamp, fileName, fileUrl, fileType, fileData) { super("file", peerName, peerAvatar, topic, timestamp); this.fileName = fileName; this.fileUrl = fileUrl; this.fileType = fileType; + this.fileData = fileData; // Add file data property } toJsonString() { @@ -13,12 +15,14 @@ class FileMessage extends Message { ...this.toJson(), fileName: this.fileName, fileUrl: this.fileUrl, - fileType: this.fileType + fileType: this.fileType, + file: this.fileData // Include file data in JSON }); } - static new(bot, fileName, fileUrl, fileType) { - return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType); + static new(bot, fileName, fileUrl, fileType, fileBuffer) { + const fileData = b4a.toString(fileBuffer, 'base64'); // Convert file buffer to base64 + return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType, fileData); } }