@andrey4emk/npm-app-back-b24 0.5.25 → 0.5.26

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.
package/index.js CHANGED
@@ -4,3 +4,4 @@ export * from "./bitrix24/eventGet.js"; // и из нового файла event
4
4
  export * from "./sendMessage/chatApp.js"; // и из chatApp.js
5
5
  export * from "./sendMessage/smsgold.js"; // и из smsGold.js
6
6
  export * from "./sendMessage/email.js"; // и из email.js
7
+ export * from "./sendMessage/wappi.js"; // и из wappi.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrey4emk/npm-app-back-b24",
3
- "version": "0.5.25",
3
+ "version": "0.5.26",
4
4
  "description": "Bitrix24 OAuth helpers for Node.js projects",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -25,7 +25,8 @@
25
25
  "bitrix24/eventGet.js",
26
26
  "sendMessage/chatApp.js",
27
27
  "sendMessage/smsgold.js",
28
- "sendMessage/email.js"
28
+ "sendMessage/email.js",
29
+ "sendMessage/wappi.js"
29
30
  ],
30
31
  "dependencies": {
31
32
  "@bitrix24/b24jssdk": "^0.5.1",
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Wappi API integration class
3
+ * инициализация класса Wappi initWappi
4
+ * @param {string} token - Wappi API token
5
+ * @param {string} profile_id - Wappi profile ID
6
+ * методы класса:
7
+ * sendMessageWappi(messangerType, messageData) - отправка сообщения
8
+ * sendFileWappi(messangerType, messageData) - отправка файла
9
+ * phoneCheckWappi(messangerType, phone) - проверка номера телефона
10
+ * @param {string} messangerType - тип мессенджера (whatsApp, telegram, max)
11
+ * @param {object} messageData - данные сообщения {phone: string, message: string, fileUrl?: string, fileName?: string}
12
+ * @return {object} - результат выполнения метода {error: boolean, message: string, data: object}
13
+ * @return {error: boolean, message: string, data: object} - ошибка выполнения метода
14
+ * @return {data.detail: boolean} - результат выполнения метода phoneCheckWappi(messangerType, phone) (существует ли номер в мессенджере true/false)
15
+ */
16
+
17
+ export class Wappi {
18
+ constructor() {
19
+ this.token = token;
20
+ this.profile_id = profile_id;
21
+ }
22
+
23
+ async initWappi(profile_id, token) {
24
+ this.profile_id = profile_id;
25
+ this.token = token;
26
+ }
27
+
28
+ async sendMessageWappi(messangerType, messageData) {
29
+ if (!["whatsApp"].includes(messangerType) && !["telegram"].includes(messangerType) && !["max"].includes(messangerType)) {
30
+ return { error: true, message: "Неверный тип мессенджера", data: null };
31
+ }
32
+ if (!messageData.phone || !messageData.message) {
33
+ return { error: true, message: "Отсутствует phone или message", data: null };
34
+ }
35
+ let phone = messageData.phone;
36
+ let message = messageData.message;
37
+ let url;
38
+ if (messangerType === "whatsApp") {
39
+ url = ``;
40
+ }
41
+ if (messangerType === "telegram") {
42
+ url = ``;
43
+ }
44
+ if (messangerType === "max") {
45
+ url = `https://wappi.pro/maxapi/sync/message/send`;
46
+ }
47
+ let res = await fetch(url, {
48
+ method: "POST",
49
+ headers: {
50
+ "Content-Type": "application/json",
51
+ Authorization: this.token,
52
+ },
53
+ params: {
54
+ profile_id: this.profile_id,
55
+ },
56
+ body: JSON.stringify({
57
+ recipient: phone,
58
+ body: message,
59
+ }),
60
+ });
61
+ let data = await res.json();
62
+ if (data.status != "done") {
63
+ return { error: true, message: `Ошибка при отправке сообщения в ChatApp через ${messangerType}`, data: data };
64
+ } else {
65
+ return { error: false, message: `Сообщение успешно отправлено в ChatApp через ${messangerType}`, data: data };
66
+ }
67
+ }
68
+
69
+ async sendFileWappi(messangerType, messageData) {
70
+ if (!["whatsApp"].includes(messangerType) && !["telegram"].includes(messangerType) && !["max"].includes(messangerType)) {
71
+ return { error: true, message: "Неверный тип мессенджера", data: null };
72
+ }
73
+ if (!messageData.phone || !messageData.message) {
74
+ return { error: true, message: "Отсутствует phone или message", data: null };
75
+ }
76
+ let phone = messageData.phone;
77
+ let message = messageData.message;
78
+ let fileUrl = messageData.fileUrl;
79
+ let fileName = messageData.fileName;
80
+ let url;
81
+ if (messangerType === "whatsApp") {
82
+ url = ``;
83
+ }
84
+ if (messangerType === "telegram") {
85
+ url = ``;
86
+ }
87
+ if (messangerType === "max") {
88
+ url = `/maxapi/async/message/file/url/send`;
89
+ }
90
+ let res = await fetch(url, {
91
+ method: "POST",
92
+ headers: {
93
+ "Content-Type": "application/json",
94
+ Authorization: this.token,
95
+ },
96
+ params: {
97
+ profile_id: this.profile_id,
98
+ },
99
+ body: JSON.stringify({
100
+ recipient: phone,
101
+ url: fileUrl,
102
+ file_name: fileName,
103
+ caption: message,
104
+ }),
105
+ });
106
+ let data = await res.json();
107
+ if (data.status != "done") {
108
+ return { error: true, message: `Ошибка при отправке файла в ChatApp через ${messangerType}`, data: data };
109
+ } else {
110
+ return { error: false, message: `Файл успешно отправлен в ChatApp через ${messangerType}`, data: data };
111
+ }
112
+ }
113
+
114
+ async phoneCheckWappi(messangerType, phone) {
115
+ if (!["whatsApp"].includes(messangerType) && !["telegram"].includes(messangerType) && !["max"].includes(messangerType)) {
116
+ return { error: true, message: "Неверный тип мессенджера", data: null };
117
+ }
118
+ if (!phone) {
119
+ return { error: true, message: "Отсутствует phone ", data: null };
120
+ }
121
+ let url;
122
+ if (messangerType === "whatsApp") {
123
+ url = ``;
124
+ }
125
+ if (messangerType === "telegram") {
126
+ url = ``;
127
+ }
128
+ if (messangerType === "max") {
129
+ url = `https://wappi.pro/maxapi/sync/contact/check`;
130
+ }
131
+ let res = await fetch(url, {
132
+ method: "get",
133
+ headers: {
134
+ "Content-Type": "application/json",
135
+ Authorization: this.token,
136
+ },
137
+ params: {
138
+ profile_id: this.profile_id,
139
+ phone: phone,
140
+ },
141
+ });
142
+ let data = await res.json();
143
+ if (data.status != "done") {
144
+ return { error: true, message: `Ошибка при отправке сообщения в ChatApp через ${messangerType}`, data: data };
145
+ } else {
146
+ return { error: false, message: `Сообщение успешно отправлено в ChatApp через ${messangerType}`, data: data };
147
+ }
148
+ }
149
+ }