WhatsApp bot integration
The bot stays a separate CommonJS project. It never loads a scraper file; it calls this API and reads JSON.
One client module
Put every call behind one small module so the handlers never build URLs by hand. CommonJS works as is, with axios or with the global fetch.
bot/scraper-api.cjs
// bot/scraper-api.cjs
const axios = require("axios");
const api = axios.create({
baseURL: process.env.SCRAPER_API_URL || "https://scraper.aznime.com",
headers: { "x-api-key": process.env.SCRAPER_API_KEY },
timeout: 12000,
validateStatus: () => true,
});
async function call(path, params) {
const { status, data } = await api.get(path, { params });
if (status === 200 && data && data.success) {
return data.data;
}
if (status === 429) {
throw new Error("Scraper API is rate limited, try again in a minute");
}
const code = data && data.error ? data.error.code : "HTTP_" + status;
const message = data && data.error ? data.error.message : "Unknown failure";
const error = new Error(code + ": " + message);
error.code = code;
throw error;
}
module.exports = {
home: () => call("/anilist/v1/home"),
search: (q) => call("/anilist/v1/search", { q }),
anime: (id) => call("/anilist/v1/anime/" + id),
schedule: () => call("/anilist/v1/schedule", { limit: 10 }),
};Using it in a handler
bot/handlers/anime.cjs
const scraperApi = require("./scraper-api.cjs");
async function animeHandler(message) {
const keyword = message.body.trim();
if (!keyword) return message.reply("Give me a title to look for.");
try {
const result = await scraperApi.search(keyword);
if (!result.results.length) {
return message.reply("Nothing found for " + keyword);
}
const top = result.results.slice(0, 5);
const lines = top.map((anime, index) => {
const title = anime.title.english || anime.title.romaji;
return index + 1 + ". " + title + " (" + (anime.seasonYear || "-") + ")";
});
return message.reply(lines.join("\n"));
} catch (error) {
console.error("[anime]", error.code, error.message);
return message.reply("The catalogue is not answering right now.");
}
}
module.exports = { animeHandler };Environment
SCRAPER_API_URL=https://scraper.aznime.com SCRAPER_API_KEY=sk_scr_xxxxxxxxxxxxxxxxxxxxxxxx
Keep the key in the bot environment, not in the repository. The API never asks for a WhatsApp credential, and the bot never needs to know which site a scraper reads.
Rules that keep the bot healthy
- Set a timeout on the client. The API gives up at 9 seconds; a bot that waits longer holds the chat hostage.
- Branch on
error.code, not on the message text.VALIDATION_ERRORis a user mistake,SOURCE_TIMEOUTdeserves one retry,RATE_LIMITEDdeserves a wait. - Cache the boring calls in the bot for a minute; the API already caches, this saves a round trip.
- Do not send raw API JSON to a chat. Map it to a few lines.
- Keep the scraper runtime out of the bot process. One broken selector must not restart the bot.
CommonJS with fetch instead of axios
const response = await fetch(
"https://scraper.aznime.com/anilist/v1/home",
{ headers: { "x-api-key": process.env.SCRAPER_API_KEY } }
);
const payload = await response.json();
if (!payload.success) throw new Error(payload.error.code);
console.log(payload.data.trending.length);