correct support for sending files via the bot

This commit is contained in:
Raven Scott 2024-06-15 02:52:20 -04:00
parent 49f4cc88ed
commit 2cf819553a
4 changed files with 46 additions and 20 deletions

View File

@ -1,7 +1,10 @@
// ping.js
export default { export default {
handler: function(bot, args, message) { handler: function(bot, args, message) {
bot.sendTextMessage('Pong!'); // 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);
}
}; };

View File

@ -1,3 +1,4 @@
import path from 'path';
import Hyperswarm from 'hyperswarm'; import Hyperswarm from 'hyperswarm';
import EventEmitter from 'node:events'; import EventEmitter from 'node:events';
import b4a from "b4a"; import b4a from "b4a";
@ -90,27 +91,31 @@ class Client extends EventEmitter {
peer.write(this.iconMessage.toJsonString()); peer.write(this.iconMessage.toJsonString());
} }
peer.on('data', message => { peer.on('data', async message => {
const messageObj = JSON.parse(message.toString()); const messageObj = JSON.parse(message.toString());
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room 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 this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
const msgType = messageObj.type; const msgType = messageObj.type;
const peerName = messageObj.name; const peerName = messageObj.name; // Changed from name to userName
const peerAvatar = messageObj.avatar; const peerAvatar = messageObj.avatar;
const timestamp = messageObj.timestamp; const timestamp = messageObj.timestamp;
if (msgType === "message") if (msgType === "message")
this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message)); this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message));
if (msgType === "file") if (msgType === "file") {
this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); 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") if (msgType === "icon")
this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp)); this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp));
if (msgType === "audio") if (msgType === "audio") {
this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.audio, messageObj.audioType)); 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)); 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) { sendMessage(message) {
if (!(message instanceof Message)) { if (!(message instanceof Message)) {
console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message); console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message);

View File

@ -1,22 +1,22 @@
import Message from "./Message.js"; import Message from "./Message.js";
class AudioMessage extends Message { class AudioMessage extends Message {
constructor(peerName, peerAvatar, topic, timestamp, audio, audioType) { constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType) {
super("audio", peerName, peerAvatar, topic, timestamp); super("audio", peerName, peerAvatar, topic, timestamp);
this.audio = audio; this.audioUrl = audioUrl;
this.audioType = audioType; this.audioType = audioType;
} }
toJsonString() { toJsonString() {
return JSON.stringify({ return JSON.stringify({
...this.toJson(), ...this.toJson(),
audio: this.audio, audioUrl: this.audioUrl,
audioType: this.audioType audioType: this.audioType
}); });
} }
static new(bot, audio, audioType) { static new(bot, audioUrl, audioType) {
return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audio, audioType); return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType);
} }
} }

View File

@ -1,11 +1,13 @@
import Message from "./Message.js"; import Message from "./Message.js";
import b4a from "b4a";
class FileMessage extends Message { 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); super("file", peerName, peerAvatar, topic, timestamp);
this.fileName = fileName; this.fileName = fileName;
this.fileUrl = fileUrl; this.fileUrl = fileUrl;
this.fileType = fileType; this.fileType = fileType;
this.fileData = fileData; // Add file data property
} }
toJsonString() { toJsonString() {
@ -13,12 +15,14 @@ class FileMessage extends Message {
...this.toJson(), ...this.toJson(),
fileName: this.fileName, fileName: this.fileName,
fileUrl: this.fileUrl, fileUrl: this.fileUrl,
fileType: this.fileType fileType: this.fileType,
file: this.fileData // Include file data in JSON
}); });
} }
static new(bot, fileName, fileUrl, fileType) { static new(bot, fileName, fileUrl, fileType, fileBuffer) {
return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType); 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);
} }
} }