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 {
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);
}
};

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);
}
}