@skyzopedia/baileys-mod 3.0.8 → 4.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.
@@ -1,113 +1,191 @@
1
- //=======================================================//
2
- import { proto } from "../../WAProto/index.js";
3
- import { Boom } from "@hapi/boom";
4
- import {} from "./types.js";
5
- //=======================================================//
1
+ import { Boom } from '@hapi/boom';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import {} from './types.js';
4
+ import { unixTimestampSeconds } from '../Utils/index.js';
5
+
6
+ // some extra useful utilities
6
7
  export const getBinaryNodeChildren = (node, childTag) => {
7
- if (Array.isArray(node?.content)) {
8
- return node.content.filter(item => item.tag === childTag);
9
- }
10
- return [];
8
+ if (Array.isArray(node?.content)) {
9
+ return node.content.filter(item => item.tag === childTag);
10
+ }
11
+ return [];
11
12
  };
12
- //=======================================================//
13
13
  export const getAllBinaryNodeChildren = ({ content }) => {
14
- if (Array.isArray(content)) {
15
- return content;
16
- }
17
- return [];
14
+ if (Array.isArray(content)) {
15
+ return content;
16
+ }
17
+ return [];
18
18
  };
19
- //=======================================================//
20
19
  export const getBinaryNodeChild = (node, childTag) => {
21
- if (Array.isArray(node?.content)) {
22
- return node?.content.find(item => item.tag === childTag);
23
- }
20
+ if (Array.isArray(node?.content)) {
21
+ return node?.content.find(item => item.tag === childTag);
22
+ }
24
23
  };
25
- //=======================================================//
26
24
  export const getBinaryNodeChildBuffer = (node, childTag) => {
27
- const child = getBinaryNodeChild(node, childTag)?.content;
28
- if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
29
- return child;
30
- }
25
+ const child = getBinaryNodeChild(node, childTag)?.content;
26
+ if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
27
+ return child;
28
+ }
31
29
  };
32
- //=======================================================//
33
30
  export const getBinaryNodeChildString = (node, childTag) => {
34
- const child = getBinaryNodeChild(node, childTag)?.content;
35
- if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
36
- return Buffer.from(child).toString("utf-8");
37
- }
38
- else if (typeof child === "string") {
39
- return child;
40
- }
31
+ const child = getBinaryNodeChild(node, childTag)?.content;
32
+ if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
33
+ return Buffer.from(child).toString('utf-8');
34
+ }
35
+ else if (typeof child === 'string') {
36
+ return child;
37
+ }
41
38
  };
42
- //=======================================================//
43
39
  export const getBinaryNodeChildUInt = (node, childTag, length) => {
44
- const buff = getBinaryNodeChildBuffer(node, childTag);
45
- if (buff) {
46
- return bufferToUInt(buff, length);
47
- }
40
+ const buff = getBinaryNodeChildBuffer(node, childTag);
41
+ if (buff) {
42
+ return bufferToUInt(buff, length);
43
+ }
48
44
  };
49
- //=======================================================//
50
45
  export const assertNodeErrorFree = (node) => {
51
- const errNode = getBinaryNodeChild(node, "error");
52
- if (errNode) {
53
- throw new Boom(errNode.attrs.text || "Unknown error", { data: +errNode.attrs.code });
54
- }
46
+ const errNode = getBinaryNodeChild(node, 'error');
47
+ if (errNode) {
48
+ throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
49
+ }
55
50
  };
56
- //=======================================================//
57
51
  export const reduceBinaryNodeToDictionary = (node, tag) => {
58
- const nodes = getBinaryNodeChildren(node, tag);
59
- const dict = nodes.reduce((dict, { attrs }) => {
60
- if (typeof attrs.name === "string") {
61
- dict[attrs.name] = attrs.value || attrs.config_value;
62
- }
63
- else {
64
- dict[attrs.config_code] = attrs.value || attrs.config_value;
65
- }
52
+ const nodes = getBinaryNodeChildren(node, tag);
53
+ const dict = nodes.reduce((dict, { attrs }) => {
54
+ if (typeof attrs.name === 'string') {
55
+ dict[attrs.name] = attrs.value || attrs.config_value;
56
+ }
57
+ else {
58
+ dict[attrs.config_code] = attrs.value || attrs.config_value;
59
+ }
60
+ return dict;
61
+ }, {});
66
62
  return dict;
67
- }, {});
68
- return dict;
69
63
  };
70
- //=======================================================//
71
64
  export const getBinaryNodeMessages = ({ content }) => {
72
- const msgs = [];
73
- if (Array.isArray(content)) {
74
- for (const item of content) {
75
- if (item.tag === "message") {
76
- msgs.push(proto.WebMessageInfo.decode(item.content).toJSON());
77
- }
65
+ const msgs = [];
66
+ if (Array.isArray(content)) {
67
+ for (const item of content) {
68
+ if (item.tag === 'message') {
69
+ msgs.push(proto.WebMessageInfo.decode(item.content));
70
+ }
71
+ }
78
72
  }
79
- }
80
- return msgs;
73
+ return msgs;
81
74
  };
82
- //=======================================================//
83
75
  function bufferToUInt(e, t) {
84
- let a = 0;
85
- for (let i = 0; i < t; i++) {
86
- a = 256 * a + e[i];
87
- }
88
- return a;
76
+ let a = 0;
77
+ for (let i = 0; i < t; i++) {
78
+ a = 256 * a + e[i];
79
+ }
80
+ return a;
89
81
  }
90
- //=======================================================//
91
- const tabs = (n) => "\t".repeat(n);
82
+ const tabs = (n) => '\t'.repeat(n);
92
83
  export function binaryNodeToString(node, i = 0) {
93
- if (!node) {
94
- return node;
95
- }
96
- if (typeof node === "string") {
97
- return tabs(i) + node;
98
- }
99
- if (node instanceof Uint8Array) {
100
- return tabs(i) + Buffer.from(node).toString("hex");
101
- }
102
- if (Array.isArray(node)) {
103
- return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join("\n");
104
- }
105
- const children = binaryNodeToString(node.content, i + 1);
106
- const tag = `<${node.tag} ${Object.entries(node.attrs || {})
107
- .filter(([, v]) => v !== undefined)
108
- .map(([k, v]) => `${k}="${v}"`)
109
- .join(" ")}`;
110
- const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : "/>";
111
- return tag + content;
84
+ if (!node) {
85
+ return node;
86
+ }
87
+ if (typeof node === 'string') {
88
+ return tabs(i) + node;
89
+ }
90
+ if (node instanceof Uint8Array) {
91
+ return tabs(i) + Buffer.from(node).toString('hex');
92
+ }
93
+ if (Array.isArray(node)) {
94
+ return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n');
95
+ }
96
+ const children = binaryNodeToString(node.content, i + 1);
97
+ const tag = `<${node.tag} ${Object.entries(node.attrs || {})
98
+ .filter(([, v]) => v !== undefined)
99
+ .map(([k, v]) => `${k}='${v}'`)
100
+ .join(' ')}`;
101
+ const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
102
+ return tag + content;
103
+ }
104
+ export const getBinaryNodeFilter = (node) => {
105
+ if (!Array.isArray(node)) return false
106
+
107
+ return node.some(item =>
108
+ ['native_flow'].includes(item?.content?.[0]?.content?.[0]?.tag) ||
109
+ ['interactive', 'buttons', 'list'].includes(item?.content?.[0]?.tag) ||
110
+ ['hsm', 'biz'].includes(item?.tag) ||
111
+ ['bot'].includes(item?.tag) && item?.attrs?.biz_bot === '1'
112
+ )
113
+ }
114
+ export const getAdditionalNode = (name) => {
115
+ if (name) name = name.toLowerCase()
116
+ const ts = unixTimestampSeconds(new Date()) - 77980457
117
+
118
+ const order_response_name = {
119
+ review_and_pay: 'order_details',
120
+ review_order: 'order_status',
121
+ payment_info: 'payment_info',
122
+ payment_status: 'payment_status',
123
+ payment_method: 'payment_method'
124
+ }
125
+
126
+ const flow_name = {
127
+ cta_catalog: 'cta_catalog',
128
+ mpm: 'mpm',
129
+ call_request: 'call_permission_request',
130
+ view_catalog: 'automated_greeting_message_view_catalog',
131
+ wa_pay_detail: 'wa_payment_transaction_details',
132
+ send_location: 'send_location',
133
+ }
134
+
135
+ if(order_response_name[name]) {
136
+ return [{
137
+ tag: 'biz',
138
+ attrs: {
139
+ native_flow_name: order_response_name[name]
140
+ },
141
+ content: []
142
+ }]
143
+ } else if (flow_name[name] || name === 'interactive' || name === 'buttons') {
144
+ return [{
145
+ tag: 'biz',
146
+ attrs: {
147
+ actual_actors: '2',
148
+ host_storage: '2',
149
+ privacy_mode_ts: `${ts}`
150
+ },
151
+ content: [{
152
+ tag: 'engagement',
153
+ attrs: {
154
+ customer_service_state: 'open',
155
+ conversation_state: 'open'
156
+ }
157
+ }, {
158
+ tag: 'interactive',
159
+ attrs: {
160
+ type: 'native_flow',
161
+ v: '1'
162
+ },
163
+ content: [{
164
+ tag: 'native_flow',
165
+ attrs: {
166
+ v: '9',
167
+ name: flow_name[name] ?? 'mixed',
168
+ },
169
+ content: []
170
+ }]
171
+ }]
172
+ }]
173
+ } else {
174
+ return [{
175
+ tag: 'biz',
176
+ attrs: {
177
+ actual_actors: '2',
178
+ host_storage: '2',
179
+ privacy_mode_ts: `${ts}`
180
+ },
181
+ content: [{
182
+ tag: 'engagement',
183
+ attrs: {
184
+ customer_service_state: 'open',
185
+ conversation_state: 'open'
186
+ }
187
+ }]
188
+ }]
189
+ }
112
190
  }
113
- //=======================================================//
191
+ //# sourceMappingURL=generic-utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyzopedia/baileys-mod",
3
- "version": "3.0.8",
3
+ "version": "4.0.0",
4
4
  "description": "Websocket Whatsapp API for Node.js",
5
5
  "keywords": [
6
6
  "whatsapp",
@@ -1,247 +0,0 @@
1
- //=======================================================//
2
- import { executeWMexQuery as genericExecuteWMexQuery } from "./mex.js";
3
- import { generateProfilePicture } from "../Utils/messages-media.js";
4
- import { getBinaryNodeChild } from "../WABinary/index.js";
5
- import { QueryIds, XWAPaths } from "../Types/index.js";
6
- import { makeGroupsSocket } from "./groups.js";
7
- //=======================================================//
8
-
9
- const extractNewsletterMetadata = (node, isCreate) => {
10
- const result = getBinaryNodeChild(node, 'result')?.content?.toString()
11
- const metadataPath = JSON.parse(result).data[isCreate ? XWAPaths.xwa2_newsletter_create : "xwa2_newsletter"]
12
-
13
- const metadata = {
14
- id: metadataPath?.id,
15
- state: metadataPath?.state?.type,
16
- creation_time: +metadataPath?.thread_metadata?.creation_time,
17
- name: metadataPath?.thread_metadata?.name?.text,
18
- nameTime: +metadataPath?.thread_metadata?.name?.update_time,
19
- description: metadataPath?.thread_metadata?.description?.text,
20
- descriptionTime: +metadataPath?.thread_metadata?.description?.update_time,
21
- invite: metadataPath?.thread_metadata?.invite,
22
- handle: metadataPath?.thread_metadata?.handle,
23
- reaction_codes: metadataPath?.thread_metadata?.settings?.reaction_codes?.value,
24
- subscribers: +metadataPath?.thread_metadata?.subscribers_count,
25
- verification: metadataPath?.thread_metadata?.verification,
26
- viewer_metadata: metadataPath?.viewer_metadata
27
- }
28
- return metadata
29
- }
30
-
31
- const parseNewsletterCreateResponse = (response) => {
32
- const { id, thread_metadata: thread, viewer_metadata: viewer } = response;
33
- return {
34
- id: id,
35
- owner: undefined,
36
- name: thread.name.text,
37
- creation_time: parseInt(thread.creation_time, 10),
38
- description: thread.description.text,
39
- invite: thread.invite,
40
- subscribers: parseInt(thread.subscribers_count, 10),
41
- verification: thread.verification,
42
- picture: {
43
- id: thread.picture.id,
44
- directPath: thread.picture.direct_path
45
- },
46
- mute_state: viewer.mute
47
- };
48
- };
49
- const parseNewsletterMetadata = (result) => {
50
- if (typeof result !== "object" || result === null) {
51
- return null;
52
- }
53
- if ("id" in result && typeof result.id === "string") {
54
- return result;
55
- }
56
- if ("result" in result && typeof result.result === "object" && result.result !== null && "id" in result.result) {
57
- return result.result;
58
- }
59
- return null;
60
- };
61
-
62
- export const makeNewsletterSocket = (config) => {
63
- const sock = makeGroupsSocket(config);
64
- const { query, generateMessageTag } = sock;
65
- const encoder = new TextEncoder()
66
- const newsletterWMexQuery = async (jid, queryId, content) => (query({
67
- tag: 'iq',
68
- attrs: {
69
- id: generateMessageTag(),
70
- type: 'get',
71
- xmlns: 'w:mex',
72
- to: "@s.whatsapp.net",
73
- },
74
- content: [
75
- {
76
- tag: 'query',
77
- attrs: { 'query_id': queryId },
78
- content: encoder.encode(JSON.stringify({
79
- variables: {
80
- 'newsletter_id': jid,
81
- ...content
82
- }
83
- }))
84
- }
85
- ]
86
- }))
87
- const executeWMexQuery = (variables, queryId, dataPath) => {
88
- return genericExecuteWMexQuery(variables, queryId, dataPath, query, generateMessageTag);
89
- };
90
- const newsletterMetadata = async (type, key, role) => {
91
- const result = await newsletterWMexQuery(undefined, QueryIds.METADATA, {
92
- input: {
93
- key,
94
- type: type.toUpperCase(),
95
- view_role: role || 'GUEST'
96
- },
97
- fetch_viewer_metadata: true,
98
- fetch_full_image: true,
99
- fetch_creation_time: true
100
- })
101
-
102
- return extractNewsletterMetadata(result)
103
- }
104
- const newsletterUpdate = async (jid, updates) => {
105
- const variables = {
106
- newsletter_id: jid,
107
- updates: {
108
- ...updates,
109
- settings: null
110
- }
111
- };
112
- return executeWMexQuery(variables, QueryIds.UPDATE_METADATA, "xwa2_newsletter_update");
113
- };
114
- return {
115
- ...sock,
116
- newsletterCreate: async (name, description) => {
117
- const variables = {
118
- input: {
119
- name,
120
- description: description ?? null
121
- }
122
- };
123
- const rawResponse = await executeWMexQuery(variables, QueryIds.CREATE, XWAPaths.xwa2_newsletter_create);
124
- return parseNewsletterCreateResponse(rawResponse);
125
- },
126
- newsletterUpdate,
127
- newsletterSubscribers: async (jid) => {
128
- return executeWMexQuery({ newsletter_id: jid }, QueryIds.SUBSCRIBERS, XWAPaths.xwa2_newsletter_subscribers);
129
- },
130
- newsletterMetadata,
131
- newsletterFetchAllParticipating: async () => {
132
- const data = {}
133
-
134
- const result = await newsletterWMexQuery(undefined, QueryIds.SUBSCRIBERS)
135
- const child = JSON.parse(getBinaryNodeChild(result, 'result')?.content?.toString())
136
- const newsletters = child.data["xwa2_newsletter_subscribed"]
137
-
138
- for (const i of newsletters) {
139
- if (i.id == null) continue
140
-
141
- const metadata = await newsletterMetadata('JID', i.id)
142
- if (metadata.id !== null) data[metadata.id] = metadata
143
- }
144
-
145
- return data
146
- },
147
- newsletterUnfollow: async (jid) => {
148
- await newsletterWMexQuery(jid, QueryIds.UNFOLLOW)
149
- },
150
- newsletterFollow: async (jid) => {
151
- await newsletterWMexQuery(jid, QueryIds.FOLLOW)
152
- },
153
- newsletterMute: (jid) => {
154
- return executeWMexQuery({ newsletter_id: jid }, QueryIds.MUTE, XWAPaths.xwa2_newsletter_mute_v2);
155
- },
156
- newsletterUnmute: (jid) => {
157
- return executeWMexQuery({ newsletter_id: jid }, QueryIds.UNMUTE, XWAPaths.xwa2_newsletter_unmute_v2);
158
- },
159
- newsletterUpdateName: async (jid, name) => {
160
- return await newsletterUpdate(jid, { name });
161
- },
162
- newsletterUpdateDescription: async (jid, description) => {
163
- return await newsletterUpdate(jid, { description });
164
- },
165
- newsletterUpdatePicture: async (jid, content) => {
166
- const { img } = await generateProfilePicture(content);
167
- return await newsletterUpdate(jid, { picture: img.toString("base64") });
168
- },
169
- newsletterRemovePicture: async (jid) => {
170
- return await newsletterUpdate(jid, { picture: "" });
171
- },
172
- newsletterReactMessage: async (jid, serverId, reaction) => {
173
- await query({
174
- tag: "message",
175
- attrs: {
176
- to: jid,
177
- ...(reaction ? {} : { edit: "7" }),
178
- type: "reaction",
179
- server_id: serverId,
180
- id: generateMessageTag()
181
- },
182
- content: [
183
- {
184
- tag: "reaction",
185
- attrs: reaction ? { code: reaction } : {}
186
- }
187
- ]
188
- });
189
- },
190
- newsletterFetchMessages: async (jid, count, since, after) => {
191
- const messageUpdateAttrs = {
192
- count: count.toString()
193
- };
194
- if (typeof since === "number") {
195
- messageUpdateAttrs.since = since.toString();
196
- }
197
- if (after) {
198
- messageUpdateAttrs.after = after.toString();
199
- }
200
- const result = await query({
201
- tag: "iq",
202
- attrs: {
203
- id: generateMessageTag(),
204
- type: "get",
205
- xmlns: "newsletter",
206
- to: jid
207
- },
208
- content: [
209
- {
210
- tag: "message_updates",
211
- attrs: messageUpdateAttrs
212
- }
213
- ]
214
- });
215
- return result;
216
- },
217
- subscribeNewsletterUpdates: async (jid) => {
218
- const result = await query({
219
- tag: "iq",
220
- attrs: {
221
- id: generateMessageTag(),
222
- type: "set",
223
- xmlns: "newsletter",
224
- to: jid
225
- },
226
- content: [{ tag: "live_updates", attrs: {}, content: [] }]
227
- });
228
- const liveUpdatesNode = getBinaryNodeChild(result, "live_updates");
229
- const duration = liveUpdatesNode?.attrs?.duration;
230
- return duration ? { duration: duration } : null;
231
- },
232
- newsletterAdminCount: async (jid) => {
233
- const response = await executeWMexQuery({ newsletter_id: jid }, QueryIds.ADMIN_COUNT, XWAPaths.xwa2_newsletter_admin_count);
234
- return response.admin_count;
235
- },
236
- newsletterChangeOwner: async (jid, newOwnerJid) => {
237
- await executeWMexQuery({ newsletter_id: jid, user_id: newOwnerJid }, QueryIds.CHANGE_OWNER, XWAPaths.xwa2_newsletter_change_owner);
238
- },
239
- newsletterDemote: async (jid, userJid) => {
240
- await executeWMexQuery({ newsletter_id: jid, user_id: userJid }, QueryIds.DEMOTE, XWAPaths.xwa2_newsletter_demote);
241
- },
242
- newsletterDelete: async (jid) => {
243
- await executeWMexQuery({ newsletter_id: jid }, QueryIds.DELETE, XWAPaths.xwa2_newsletter_delete_v2);
244
- }
245
- };
246
- };
247
- //=======================================================//