savage-buttons 2.0.0 → 2.0.1

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/index.cjs +114 -0
  2. package/index.mjs +116 -0
  3. package/package.json +7 -4
package/index.cjs ADDED
@@ -0,0 +1,114 @@
1
+ const { generateWAMessageFromContent, prepareWAMessageMedia } = require('savage-baileys');
2
+
3
+ const btn = {
4
+ url(display_text, url, merchant_url) {
5
+ return {
6
+ name: 'cta_url',
7
+ buttonParamsJson: JSON.stringify({ display_text, url, merchant_url: merchant_url || url })
8
+ };
9
+ },
10
+ copy(display_text, copy_code) {
11
+ return {
12
+ name: 'cta_copy',
13
+ buttonParamsJson: JSON.stringify({ display_text, copy_code })
14
+ };
15
+ },
16
+ call(display_text, phone_number) {
17
+ return {
18
+ name: 'cta_call',
19
+ buttonParamsJson: JSON.stringify({ display_text, phone_number })
20
+ };
21
+ },
22
+ reply(display_text, id) {
23
+ return {
24
+ name: 'quick_reply',
25
+ buttonParamsJson: JSON.stringify({ display_text, id })
26
+ };
27
+ },
28
+ list(list_button_text, sections) {
29
+ return {
30
+ name: 'single_select',
31
+ buttonParamsJson: JSON.stringify({ title: list_button_text, sections })
32
+ };
33
+ }
34
+ };
35
+
36
+ async function sendButtons(sock, jid, options = {}) {
37
+ const { text = '', footer = '', title, subtitle, buttons, interactiveButtons, contextInfo, quoted, image } = options;
38
+ const resolvedButtons = buttons || interactiveButtons || [];
39
+
40
+ if (!Array.isArray(resolvedButtons) || resolvedButtons.length === 0) {
41
+ throw new Error('savage-buttons: sendButtons requires at least one button');
42
+ }
43
+
44
+ for (const b of resolvedButtons) {
45
+ if (!b || typeof b.name !== 'string' || typeof b.buttonParamsJson !== 'string') {
46
+ throw new Error('savage-buttons: each button needs { name, buttonParamsJson }');
47
+ }
48
+ }
49
+
50
+ let header = { title: title || '', subtitle: subtitle || '', hasMediaAttachment: false };
51
+
52
+ if (image) {
53
+ const media = await prepareWAMessageMedia(
54
+ { image: typeof image === 'string' ? { url: image } : image },
55
+ { upload: sock.waUploadToServer }
56
+ );
57
+ header = {
58
+ title: title || '',
59
+ subtitle: subtitle || '',
60
+ hasMediaAttachment: true,
61
+ imageMessage: media.imageMessage
62
+ };
63
+ }
64
+
65
+ const bodyAndHeader = {
66
+ header,
67
+ ...(text ? { body: { text } } : {}),
68
+ ...(footer ? { footer: { text: footer } } : {})
69
+ };
70
+
71
+ if (contextInfo && typeof contextInfo === 'object') {
72
+ bodyAndHeader.contextInfo = contextInfo;
73
+ }
74
+
75
+ const interactiveMessage = {
76
+ ...bodyAndHeader,
77
+ nativeFlowMessage: {
78
+ buttons: resolvedButtons,
79
+ messageParamsJson: ''
80
+ }
81
+ };
82
+
83
+ const messageContent = { interactiveMessage };
84
+
85
+ const interactiveStanzaNodes = [
86
+ {
87
+ tag: 'biz',
88
+ attrs: {},
89
+ content: [
90
+ {
91
+ tag: 'interactive',
92
+ attrs: { type: 'native_flow', v: '1' },
93
+ content: [
94
+ { tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }
95
+ ]
96
+ }
97
+ ]
98
+ }
99
+ ];
100
+
101
+ const fullMessage = generateWAMessageFromContent(jid, messageContent, {
102
+ quoted,
103
+ userJid: sock.user?.id
104
+ });
105
+
106
+ await sock.relayMessage(jid, fullMessage.message, {
107
+ messageId: fullMessage.key.id,
108
+ additionalNodes: interactiveStanzaNodes
109
+ });
110
+
111
+ return fullMessage;
112
+ }
113
+
114
+ module.exports = { sendButtons, sendInteractiveMessage: sendButtons, btn };
package/index.mjs ADDED
@@ -0,0 +1,116 @@
1
+ import baileys from 'savage-baileys';
2
+ const { generateWAMessageFromContent, prepareWAMessageMedia } = baileys;
3
+
4
+ export const btn = {
5
+ url(display_text, url, merchant_url) {
6
+ return {
7
+ name: 'cta_url',
8
+ buttonParamsJson: JSON.stringify({ display_text, url, merchant_url: merchant_url || url })
9
+ };
10
+ },
11
+ copy(display_text, copy_code) {
12
+ return {
13
+ name: 'cta_copy',
14
+ buttonParamsJson: JSON.stringify({ display_text, copy_code })
15
+ };
16
+ },
17
+ call(display_text, phone_number) {
18
+ return {
19
+ name: 'cta_call',
20
+ buttonParamsJson: JSON.stringify({ display_text, phone_number })
21
+ };
22
+ },
23
+ reply(display_text, id) {
24
+ return {
25
+ name: 'quick_reply',
26
+ buttonParamsJson: JSON.stringify({ display_text, id })
27
+ };
28
+ },
29
+ list(list_button_text, sections) {
30
+ return {
31
+ name: 'single_select',
32
+ buttonParamsJson: JSON.stringify({ title: list_button_text, sections })
33
+ };
34
+ }
35
+ };
36
+
37
+ export async function sendButtons(sock, jid, options = {}) {
38
+ const { text = '', footer = '', title, subtitle, buttons, interactiveButtons, contextInfo, quoted, image } = options;
39
+ const resolvedButtons = buttons || interactiveButtons || [];
40
+
41
+ if (!Array.isArray(resolvedButtons) || resolvedButtons.length === 0) {
42
+ throw new Error('savage-buttons: sendButtons requires at least one button');
43
+ }
44
+
45
+ for (const b of resolvedButtons) {
46
+ if (!b || typeof b.name !== 'string' || typeof b.buttonParamsJson !== 'string') {
47
+ throw new Error('savage-buttons: each button needs { name, buttonParamsJson }');
48
+ }
49
+ }
50
+
51
+ let header = { title: title || '', subtitle: subtitle || '', hasMediaAttachment: false };
52
+
53
+ if (image) {
54
+ const media = await prepareWAMessageMedia(
55
+ { image: typeof image === 'string' ? { url: image } : image },
56
+ { upload: sock.waUploadToServer }
57
+ );
58
+ header = {
59
+ title: title || '',
60
+ subtitle: subtitle || '',
61
+ hasMediaAttachment: true,
62
+ imageMessage: media.imageMessage
63
+ };
64
+ }
65
+
66
+ const bodyAndHeader = {
67
+ header,
68
+ ...(text ? { body: { text } } : {}),
69
+ ...(footer ? { footer: { text: footer } } : {})
70
+ };
71
+
72
+ if (contextInfo && typeof contextInfo === 'object') {
73
+ bodyAndHeader.contextInfo = contextInfo;
74
+ }
75
+
76
+ const interactiveMessage = {
77
+ ...bodyAndHeader,
78
+ nativeFlowMessage: {
79
+ buttons: resolvedButtons,
80
+ messageParamsJson: ''
81
+ }
82
+ };
83
+
84
+ const messageContent = { interactiveMessage };
85
+
86
+ const interactiveStanzaNodes = [
87
+ {
88
+ tag: 'biz',
89
+ attrs: {},
90
+ content: [
91
+ {
92
+ tag: 'interactive',
93
+ attrs: { type: 'native_flow', v: '1' },
94
+ content: [
95
+ { tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }
96
+ ]
97
+ }
98
+ ]
99
+ }
100
+ ];
101
+
102
+ const fullMessage = generateWAMessageFromContent(jid, messageContent, {
103
+ quoted,
104
+ userJid: sock.user?.id
105
+ });
106
+
107
+ await sock.relayMessage(jid, fullMessage.message, {
108
+ messageId: fullMessage.key.id,
109
+ additionalNodes: interactiveStanzaNodes
110
+ });
111
+
112
+ return fullMessage;
113
+ }
114
+
115
+ export const sendInteractiveMessage = sendButtons;
116
+ export default { sendButtons, sendInteractiveMessage, btn };
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "savage-buttons",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Pure, transparent interactive-buttons helper for Savage Baileys",
5
5
  "icon": "https://files.catbox.moe/si5ztc.jpg",
6
- "type": "module",
7
- "main": "index.js",
6
+ "type": "commonjs",
7
+ "main": "./index.cjs",
8
8
  "exports": {
9
- ".": "./index.js"
9
+ ".": {
10
+ "import": "./index.mjs",
11
+ "require": "./index.cjs"
12
+ }
10
13
  },
11
14
  "keywords": [
12
15
  "whatsapp",