diff --git a/whatsapp_bridge/server.js b/whatsapp_bridge/server.js index 22d9697..b6e50c4 100644 --- a/whatsapp_bridge/server.js +++ b/whatsapp_bridge/server.js @@ -458,6 +458,46 @@ async function handleMessage(ws, raw) { return respond({ type: 'messages', chatId, data: messages.map(formatMessage) }); } + case 'send_message': { + if (!clientReady) return respond({ type: 'error', message: 'WhatsApp is not ready' }); + const { chatId, text } = payload; + if (!chatId || !text) return respond({ type: 'error', message: 'chatId and text are required' }); + try { + const sentMsg = await waClient.sendMessage(chatId, text); + return respond({ + type: 'message_sent', + chatId: chatId, + data: formatMessage(sentMsg) + }); + } catch (err) { + return respond({ type: 'error', message: err.message }); + } + } + + case 'send_media': { + if (!clientReady) return respond({ type: 'error', message: 'WhatsApp is not ready' }); + const { chatId, base64, mimetype, filename, caption } = payload; + if (!chatId || !base64 || !mimetype) { + return respond({ type: 'error', message: 'chatId, base64, and mimetype are required' }); + } + try { + let cleanBase64 = base64.trim(); + if (cleanBase64.includes(';base64,')) { + cleanBase64 = cleanBase64.split(';base64,')[1]; + } + const { MessageMedia } = require('whatsapp-web.js'); + const media = new MessageMedia(mimetype, cleanBase64, filename || 'file'); + const sentMsg = await waClient.sendMessage(chatId, media, { caption: caption || '' }); + return respond({ + type: 'message_sent', + chatId: chatId, + data: formatMessage(sentMsg) + }); + } catch (err) { + return respond({ type: 'error', message: err.message }); + } + } + case 'get_media': { if (!clientReady) return respond({ type: 'error', message: 'WhatsApp is not ready' }); const { messageId } = payload;