Lodaa / bot /autoreply.js
understanding's picture
Create bot/autoreply.js
6cbc6c3 verified
// bot/autoreply.js
export async function handleMessage(event, sock) {
try {
const messages = event.messages;
if (!messages || messages.length === 0) return;
for (const message of messages) {
if (message.key.fromMe) continue; // Skip self-sent
// Extract message content
let content;
if (message.message?.conversation) {
content = message.message.conversation;
} else if (message.message?.extendedTextMessage?.text) {
content = message.message.extendedTextMessage.text;
} else {
console.log('Unknown message type:', message.type);
continue;
}
// Auto-reply logic
if (content.toLowerCase() === 'hi') {
await sock.sendMessage(
message.key.remoteJid,
{ text: 'Hello! 🌟' },
{ quoted: message }
);
}
}
} catch (error) {
console.error('Error handling message:', error);
}
}