Every incoming message in WAPI is represented by the Message class, which provides a structured way to access message content, sender information, and chat details.
The Message class is automatically instantiated when your bot receives a message. In middleware functions, you’ll work with the Context class, which extends Message with additional functionality.
WAPI automatically parses mentions from message text using the parseMentions method. Parsed mentions are available in the mentions array:
bot.use(async (ctx, next) => { if (ctx.mentions.length > 0) { console.log("Message mentions:", ctx.mentions); // Mentions are in JID format: "[email protected]" or "1234567890@lid" ctx.mentions.forEach(jid => { console.log("Mentioned user:", jid); }); // Reply mentioning the same users await ctx.reply("Thanks for the mention!", { mentions: ctx.mentions }); } await next();});
The parseMentions method (from bot.ts:261-275) uses a regex pattern to extract mentions in the format @1234567890 and converts them to proper WhatsApp JID format based on the addressing mode.
WAPI automatically extracts links from messages using the parseLinks method:
bot.use(async (ctx, next) => { if (ctx.links.length > 0) { console.log("Message contains links:", ctx.links); ctx.links.forEach(link => { console.log("Found link:", link); // Check if it's a specific domain if (link.includes("github.com")) { console.log("GitHub link detected!"); } }); } await next();});
The parseLinks method (from bot.ts:277-306) uses the Autolinker library to detect URLs, email addresses, and phone numbers in text. It supports both explicit URLs with schemes and TLD-based matching.
When a user replies to a message, you can access the quoted message:
bot.use(async (ctx, next) => { if (ctx.quoted) { console.log("This message quotes another message"); console.log("Quoted text:", ctx.quoted.text); console.log("Quoted type:", ctx.quoted.type); console.log("Quoted sender:", ctx.quoted.from.name); // Reply to the quoted message await ctx.reply(`You quoted: "${ctx.quoted.text}"`); } await next();});
The quoted property is automatically parsed from the message’s context info (see message.ts:142-154). Nested quotes are removed to prevent infinite recursion.
bot.command("echo", async (ctx) => { const text = ctx.args.join(" "); if (!text) { await ctx.reply("Please provide text to echo."); return; } await ctx.reply(text);});
2
Message Info Command
Display detailed information about the current message:
bot.use(async (ctx, next) => { if (ctx.links.length > 0) { console.log(`[${ctx.chat.name}] ${ctx.from.name} shared ${ctx.links.length} link(s)`); // Filter for specific domains const githubLinks = ctx.links.filter(link => link.includes("github.com") ); if (githubLinks.length > 0) { await ctx.reply(`Thanks for sharing GitHub links!`); } } await next();});
4
Mention Notifier
Notify when someone mentions specific users:
const ADMIN_JID = "[email protected]";bot.use(async (ctx, next) => { if (ctx.mentions.includes(ADMIN_JID)) { console.log(`Admin was mentioned in ${ctx.chat.name}`); // Could send notification or log to database } await next();});