@kropaku/mbuilder 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +32 -2
  2. package/index.js +158 -0
  3. package/package.json +33 -6
package/README.md CHANGED
@@ -1,3 +1,33 @@
1
- # Temporary Holding Version
1
+ # @kropaku/mbuilder
2
+
3
+ > WhatsApp Interactive Message & Button Builder by **Kropaku**
4
+
5
+ A lightweight and reliable interactive message builder for Baileys WhatsApp Bots (Button, ButtonV2, AIRich, and Toolkit).
6
+
7
+ ## ✨ Features
8
+ - **Button / ButtonV2** — Send Interactive Native Flow Buttons (quick reply, url, copy, single select)
9
+ - **AIRich** — AI Rich format builder (tips, code blocks, tables)
10
+ - **Toolkit** — Media buffer fetcher and image resizer
11
+
12
+ ## 📦 Installation
13
+ ```bash
14
+ npm install @kropaku/mbuilder
15
+ ```
16
+
17
+ ## 🚀 Usage Example
18
+ ```javascript
19
+ const MB = require('@kropaku/mbuilder');
20
+
21
+ const btn = new MB.ButtonV2(sock)
22
+ .setTitle("Bot Menu")
23
+ .setBody("Choose an option:")
24
+ .addButton("Main Menu", "!menu")
25
+ .addButton("Owner", "!owner")
26
+ .setFooter("© Kropa");
27
+
28
+ await btn.send(m.chat);
29
+ ```
30
+
31
+ ## 📄 License
32
+ MIT © Kris
2
33
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
package/index.js ADDED
@@ -0,0 +1,158 @@
1
+ const axios = require('axios');
2
+ const Jimp = require('jimp');
3
+
4
+ let baileys;
5
+ try {
6
+ baileys = require('@kropaku/baileys');
7
+ } catch (e1) {
8
+ try {
9
+ baileys = require('@whiskeysockets/baileys');
10
+ } catch (e2) {
11
+ baileys = require('baileys');
12
+ }
13
+ }
14
+
15
+ const { generateWAMessageFromContent } = baileys || {};
16
+
17
+ const Toolkit = {
18
+ async fetchBuffer(url, options = {}, extra = {}) {
19
+ if (!url) return Buffer.alloc(0);
20
+ if (Buffer.isBuffer(url)) return url;
21
+ try {
22
+ const res = await axios.get(url, { responseType: 'arraybuffer', timeout: 15000, ...options });
23
+ return Buffer.from(res.data);
24
+ } catch (e) {
25
+ return Buffer.alloc(0);
26
+ }
27
+ },
28
+ async resize(buffer, width = 300, height = 300) {
29
+ if (!buffer || buffer.length === 0) return Buffer.alloc(0);
30
+ try {
31
+ const image = await Jimp.read(buffer);
32
+ return await image.resize(width, height).getBufferAsync(Jimp.MIME_JPEG);
33
+ } catch (e) {
34
+ return buffer;
35
+ }
36
+ }
37
+ };
38
+
39
+ class ButtonV2 {
40
+ constructor(sock) {
41
+ this.sock = sock;
42
+ this.thumbnail = null;
43
+ this.title = '';
44
+ this.subtitle = '';
45
+ this.body = '';
46
+ this.footer = '';
47
+ this.buttons = [];
48
+ }
49
+
50
+ setThumbnail(thumb) { this.thumbnail = thumb; return this; }
51
+ setTitle(title) { this.title = title; return this; }
52
+ setSubtitle(sub) { this.subtitle = sub; return this; }
53
+ setBody(body) { this.body = body; return this; }
54
+ setFooter(footer) { this.footer = footer; return this; }
55
+
56
+ addButton(displayText, commandOrId) {
57
+ this.buttons.push({
58
+ buttonId: commandOrId,
59
+ buttonText: { displayText },
60
+ type: 1
61
+ });
62
+ return this;
63
+ }
64
+
65
+ addRawButton(btn) {
66
+ this.buttons.push(btn);
67
+ return this;
68
+ }
69
+
70
+ async send(chat, options = {}) {
71
+ try {
72
+ let thumbBuffer = Buffer.alloc(0);
73
+ if (this.thumbnail) {
74
+ thumbBuffer = await Toolkit.fetchBuffer(this.thumbnail);
75
+ if (thumbBuffer.length > 0) {
76
+ thumbBuffer = await Toolkit.resize(thumbBuffer, 300, 300);
77
+ }
78
+ }
79
+
80
+ const header = this.thumbnail ? {
81
+ hasMediaAttachment: true,
82
+ locationMessage: {
83
+ degreesLatitude: 0,
84
+ degreesLongitude: 0,
85
+ name: this.title || '',
86
+ address: this.subtitle || '',
87
+ jpegThumbnail: thumbBuffer
88
+ }
89
+ } : { hasMediaAttachment: false };
90
+
91
+ const nativeButtons = this.buttons.map(b => {
92
+ if (b.nativeFlowInfo) {
93
+ return b;
94
+ }
95
+ return {
96
+ name: 'quick_reply',
97
+ buttonParamsJson: JSON.stringify({
98
+ display_text: b.buttonText?.displayText || 'Select',
99
+ id: b.buttonId || ''
100
+ })
101
+ };
102
+ });
103
+
104
+ const interactiveMessage = {
105
+ header,
106
+ body: { text: this.body || '' },
107
+ footer: { text: this.footer || '' },
108
+ nativeFlowMessage: {
109
+ buttons: nativeButtons
110
+ }
111
+ };
112
+
113
+ const msg = generateWAMessageFromContent(chat, {
114
+ viewOnceMessage: {
115
+ message: {
116
+ interactiveMessage
117
+ }
118
+ }
119
+ }, options);
120
+
121
+ return await this.sock.relayMessage(chat, msg.message, { messageId: msg.key.id });
122
+ } catch (err) {
123
+ const fallbackText = `${this.title ? `*${this.title}*\n` : ''}${this.body || ''}${this.footer ? `\n\n_${this.footer}_` : ''}`;
124
+ return await this.sock.sendMessage(chat, { text: fallbackText }, options);
125
+ }
126
+ }
127
+ }
128
+
129
+ class AIRich {
130
+ constructor(sock) {
131
+ this.sock = sock;
132
+ this.parts = [];
133
+ }
134
+
135
+ addTip(tip) { this.parts.push(`💡 _${tip}_`); return this; }
136
+ addText(text) { this.parts.push(text); return this; }
137
+ addCode(lang, code) { this.parts.push(`\`\`\`${lang || ''}\n${code}\n\`\`\``); return this; }
138
+ addTable(rows) {
139
+ if (Array.isArray(rows) && rows.length > 0) {
140
+ const tableStr = rows.map(r => Array.isArray(r) ? r.join(' : ') : String(r)).join('\n');
141
+ this.parts.push(tableStr);
142
+ }
143
+ return this;
144
+ }
145
+
146
+ async send(chat, options = {}) {
147
+ const fullText = this.parts.join('\n\n');
148
+ return await this.sock.sendMessage(chat, { text: fullText }, options);
149
+ }
150
+ }
151
+
152
+ module.exports = {
153
+ Toolkit,
154
+ Button: ButtonV2,
155
+ ButtonV2,
156
+ AIRich
157
+ };
158
+
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
- {
2
- "name": "@kropaku/mbuilder",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@kropaku/mbuilder",
3
+ "version": "1.0.0",
4
+ "description": "WhatsApp Interactive Message & Button Builder by Kropaku",
5
+ "main": "index.js",
6
+ "keywords": [
7
+ "baileys",
8
+ "whatsapp",
9
+ "message-builder",
10
+ "button",
11
+ "interactive-message",
12
+ "native-flow",
13
+ "kropa",
14
+ "kropaku"
15
+ ],
16
+ "author": "Kris",
17
+ "license": "MIT",
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "dependencies": {
22
+ "axios": "^1.6.0",
23
+ "jimp": "^0.22.12"
24
+ },
25
+ "peerDependencies": {
26
+ "@whiskeysockets/baileys": "*"
27
+ },
28
+ "peerDependenciesMeta": {
29
+ "@whiskeysockets/baileys": {
30
+ "optional": true
31
+ }
32
+ }
33
+ }