ckptw
An easy way to make a WhatsApp Bot.
- ✨ Effortless
- 🧱 Builder
- 🛒 Built in Collector
- ⏰ Built in Cooldown
- 🔑 Built in Command handler
- 🎉 And more!
Table Of Contents
Installation
npm install @mengkodingan/ckptw
yarn add @mengkodingan/ckptw
pnpm add @mengkodingan/ckptw
Example
import { Client } from "@mengkodingan/ckptw";
import { Events, MessageType } from "@mengkodingan/ckptw/lib/Constant";
const bot = new Client({
name: "something",
prefix: "!",
readIncommingMsg: true
});
bot.ev.once(Events.ClientReady, (m) => {
console.log(`ready at ${m.user.id}`);
});
bot.command('ping', async(ctx) => ctx.reply({ text: 'pong!' }));
bot.command('hi', async(ctx) => ctx.reply('hello! you can use string as a first parameter in reply function too!'));
bot.hears('test', async(ctx) => ctx.reply('test 1 2 3 beep boop...'));
bot.hears(MessageType.stickerMessage, async(ctx) => ctx.reply('wow, cool sticker'));
bot.hears(['help', 'menu'], async(ctx) => ctx.reply('hears can be use with array too!'));
bot.hears(/(using\s?)?regex/, async(ctx) => ctx.reply('or using regex!'));
bot.launch();
Or using the Events
import { Client } from "@mengkodingan/ckptw";
import { Events } from "@mengkodingan/ckptw/lib/Constant";
const bot = new Client({
name: "something",
prefix: "!",
readIncommingMsg: true
});
bot.ev.once(Events.ClientReady, (m) => {
console.log(`ready at ${m.user.id}`);
});
bot.ev.on(Events.MessagesUpsert, (m, ctx) => {
if(m.key.fromMe) return;
if(m.content === "hello") {
ctx.reply("hi 👋");
}
})
bot.launch();
Client Configuration
export interface ClientOptions {
name: string;
prefix: Array<string>|string;
readIncommingMsg?: boolean;
authDir?: string;
printQRInTerminal?: boolean;
qrTimeout?: number;
markOnlineOnConnect?: boolean;
}
Command Options
bot.command(opts: CommandOptions | string, code?: (ctx: Ctx) => Promise<any>)
bot.command('ping', async(ctx) => ctx.reply('pong!'))
export interface CommandOptions {
name: string;
aliases?: Array<string>;
code: (ctx: Ctx) => Promise<any>;
}
bot.command({
name: 'ping',
code: async(ctx) => ctx.reply('pong!');
})
Command Handler
With command handler you dont need all your command is located in one file.
-
in your main file
import { CommandHandler } from "@mengkodingan/ckptw";
import path from "path";
const cmd = new CommandHandler(bot, path.resolve() + '/CommandsPath');
cmd.load();
-
in your command file
module.exports = {
name: "ping",
code: async (ctx) => {
ctx.reply("pong!");
},
};
Command Cooldown
Cooldown can give a delay on the command. This can be done to prevent users from spamming your bot commands.
+ import { Cooldown } from "@mengkodingan/ckptw";
bot.command('ping', async(ctx) => {
+ const cd = new Cooldown(ctx, 8000);
+ if(cd.onCooldown) return ctx.reply(`slow down... wait ${cd.timeleft}ms`);
ctx.reply('pong!')
})
if you want to trigger some function when the cooldown end, you can use the "end" events in the cooldown:
⚠
Will always be triggered when the cooldown is over (even though he only runs the command once)
cd.on("end", () => {
ctx.reply({ text: "cd timeout" });
})
Cooldown getter:
cd.onCooldown;
cd.timeleft;
Builder
-
Button
make a button message with Button Builder.
import { ButtonBuilder } from "@mengkodingan/ckptw";
const btn = new ButtonBuilder()
.setId("id1")
.setDisplayText("button 1")
.setType(1);
ctx.sendMessage(ctx.id, { text: "buttons", buttons: [btn] });
-
Sections
Sections message is like a list.
import { SectionBuilder } from "@mengkodingan/ckptw";
const a = new SectionBuilder()
.setTitle("title")
.setRows(
{ title: "abc", rowId: 1 },
{ title: "b", rowId: 2, description: "a" }
);
ctx.sendMessage(ctx.id, {
text: "sections",
buttonText: "button text",
sections: [a],
});
-
Contact
send a contact.
import { VCardBuilder } from "@mengkodingan/ckptw";
const vcard = new VCardBuilder()
.setFullName("John Doe")
.setOrg("PT Mencari Cinta Sejati")
.setNumber("621234567890")
.build();
ctx.reply({ contacts: { displayName: "John D", contacts: [{ vcard }] }});
-
Template Buttons
send a button with "attachment".
import { TemplateButtonsBuilder } from "@mengkodingan/ckptw";
const templateButtons = new TemplateButtonsBuilder()
.addURL({ displayText: 'ckptw at Github', url: 'https://github.com/mengkodingan/ckptw' })
.addCall({ displayText: 'call me', phoneNumber: '+1234567890' })
.addQuickReply({ displayText: 'just a normal button', id: 'btn1' })
.build();
ctx.sendMessage(ctx.id, { text: "template buttons", templateButtons });
Collector
There are several options that can be used in the collector:
export interface CollectorArgs {
time?: number;
max?: number;
endReason?: string[];
maxProcessed?: number;
filter?: () => boolean;
}
-
Message Collector
let col = ctx.MessageCollector({ time: 10000 });
ctx.reply({ text: "say something... Timeout: 10s" });
col.on("collect", (m) => {
console.log("COLLECTED", m);
ctx.sendMessage(ctx.id, {
text: `Collected: ${m.content}\nFrom: ${m.sender}`,
});
});
col.on("end", (collector, r) => {
console.log("ended", r);
ctx.sendMessage(ctx.id, { text: `Collector ended` });
});
-
Awaited Messages
ctx.awaitMessages({ time: 10000 }).then((m) => ctx.reply(`got ${m.length} array length`)).catch(() => ctx.reply('end'))
Downloading Media
the code below will save the received image to ./saved.jpeg
import { MessageType } from "@mengkodingan/ckptw/lib/Constant";
import fs from "node:fs";
bot.ev.on(Events.MessagesUpsert, async(m, ctx) => {
if(ctx.getMessageType() === MessageType.imageMessage) {
const buffer = await ctx.getMediaMessage(ctx.msg, 'buffer')
fs.writeFileSync('./saved.jpeg', buffer);
}
})
Events
Firstly you must import the Events Constant like this:
import { Events } from "@mengkodingan/ckptw/lib/Constant";
-
Available Events
- ClientReady - Emitted when the bot client is ready.
- MessagesUpsert - Received an messages.
- QR - The bot QR is ready to scan. Return the QR Codes.
- GroupsJoin - Emitted when bot joining groups.
- UserJoin - Emitted when someone joins a group where bots are also in that group.
- UserLeave - Same with UserJoin but this is when the user leaves the group.
Sending Message
ctx.sendMessage(ctx.id, { text: "hello" });
ctx.reply("hello");
ctx.reply({ text: "hello" });
ctx.sendMessage(ctx.id, { image: { url: 'https://example.com/image.jpeg' }, caption: "image caption" });
ctx.reply({ image: { url: 'https://example.com/image.jpeg' }, caption: "image caption" });
ctx.reply({ audio: { url: './audio.mp3' }, mimetype: 'audio/mp4', ptt: false });
ctx.reply({ sticker: { url: './tmp/generatedsticker.webp' }});
import fs from "node:fs";
ctx.reply({ video: fs.readFileSync("./video.mp4"), caption: "video caption", gifPlayback: false });
Misc
ctx.reply({ text: "test" });
ctx.reply("you can use string as a first parameter too!");
bot.hears('test', async(ctx) => ctx.reply('test 1 2 3 beep boop...'));
import { MessageType } from "@mengkodingan/ckptw/lib/Constant";
bot.hears(MessageType.stickerMessage, async(ctx) => ctx.reply('wow, cool sticker'));
ctx.react(jid: string, emoji: string, key?: object);
ctx.react(ctx.id, "👀");
bot.readyAt;
ctx.id
ctx.args
ctx.sender
ctx.getMessageType()
ctx.read()
ctx.simulateTyping()
ctx._client