@andrey4emk/npm-app-back-b24 0.6.4 → 0.6.5
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/package.json +1 -1
- package/sendMessage/wappi.js +46 -6
package/package.json
CHANGED
package/sendMessage/wappi.js
CHANGED
|
@@ -37,14 +37,16 @@ export class Wappi {
|
|
|
37
37
|
url = ``;
|
|
38
38
|
}
|
|
39
39
|
if (messangerType === "telegram") {
|
|
40
|
-
|
|
40
|
+
token = this.telegaAuth.token;
|
|
41
|
+
profile_id = this.telegaAuth.profile_id;
|
|
42
|
+
url = `https://wappi.pro/tapi/sync/message/send?profile_id=${profile_id}`;
|
|
41
43
|
}
|
|
42
44
|
if (messangerType === "max") {
|
|
43
|
-
url = `https://wappi.pro/maxapi/sync/message/send`;
|
|
44
45
|
token = this.maxAuth.token;
|
|
45
46
|
profile_id = this.maxAuth.profile_id;
|
|
47
|
+
url = `https://wappi.pro/maxapi/sync/message/send?profile_id=${profile_id}`;
|
|
46
48
|
}
|
|
47
|
-
let res = await fetch(url
|
|
49
|
+
let res = await fetch(url, {
|
|
48
50
|
method: "POST",
|
|
49
51
|
headers: {
|
|
50
52
|
"Content-Type": "application/json",
|
|
@@ -71,6 +73,9 @@ export class Wappi {
|
|
|
71
73
|
if (!messageData.phone || !messageData.message) {
|
|
72
74
|
return { error: true, message: "Отсутствует phone или message", data: null };
|
|
73
75
|
}
|
|
76
|
+
if (!messageData.fileUrl || !messageData.fileName) {
|
|
77
|
+
return { error: true, message: "Отсутствует fileUrl или fileName", data: null };
|
|
78
|
+
}
|
|
74
79
|
let phone = messageData.phone;
|
|
75
80
|
let message = messageData.message;
|
|
76
81
|
let fileUrl = messageData.fileUrl;
|
|
@@ -78,23 +83,40 @@ export class Wappi {
|
|
|
78
83
|
let url;
|
|
79
84
|
let token;
|
|
80
85
|
let profile_id;
|
|
86
|
+
let base64;
|
|
81
87
|
if (messangerType === "whatsApp") {
|
|
82
88
|
url = ``;
|
|
83
89
|
}
|
|
84
90
|
if (messangerType === "telegram") {
|
|
85
|
-
|
|
91
|
+
token = this.telegaAuth.token;
|
|
92
|
+
profile_id = this.telegaAuth.profile_id;
|
|
93
|
+
// скачивам файл и конвертируем в base64
|
|
94
|
+
let resConvert = await this.convertToBase64(fileUrl);
|
|
95
|
+
if (resConvert.error) {
|
|
96
|
+
return { error: true, message: `Ошибка при конвертации файла в base64 для Telegram: ${resConvert.message}`, data: null };
|
|
97
|
+
} else {
|
|
98
|
+
base64 = resConvert.data;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (fileName.includes(".pdf")) {
|
|
102
|
+
url = `https://wappi.pro/tapi/sync/message/document/send?profile_id=${profile_id}`;
|
|
103
|
+
}
|
|
104
|
+
// Если отправили картинку .png, .jpg и т.д.
|
|
105
|
+
if (fileName.includes(".png" || ".jpg" || ".jpeg" || ".gif")) {
|
|
106
|
+
url = `https://wappi.pro/tapi/sync/message/img/send?profile_id=${profile_id}`;
|
|
107
|
+
}
|
|
86
108
|
}
|
|
87
109
|
if (messangerType === "max") {
|
|
88
|
-
url = `https://wappi.pro/maxapi/async/message/file/url/send`;
|
|
89
110
|
token = this.maxAuth.token;
|
|
90
111
|
profile_id = this.maxAuth.profile_id;
|
|
112
|
+
url = `https://wappi.pro/maxapi/async/message/file/url/send?profile_id=${profile_id}`;
|
|
91
113
|
}
|
|
92
114
|
|
|
93
115
|
// Max при отправке pdf не прикрепляет сообщение caption: message, поэтому отправляем его отдельно
|
|
94
116
|
if (fileName.includes(".pdf")) {
|
|
95
117
|
await this.sendMessageWappi(messangerType, { phone: phone, message: message });
|
|
96
118
|
}
|
|
97
|
-
let res = await fetch(url
|
|
119
|
+
let res = await fetch(url, {
|
|
98
120
|
method: "POST",
|
|
99
121
|
headers: {
|
|
100
122
|
"Content-Type": "application/json",
|
|
@@ -106,6 +128,7 @@ export class Wappi {
|
|
|
106
128
|
url: fileUrl,
|
|
107
129
|
file_name: fileName,
|
|
108
130
|
caption: message,
|
|
131
|
+
base64: base64,
|
|
109
132
|
}),
|
|
110
133
|
});
|
|
111
134
|
let data = await res.json();
|
|
@@ -246,4 +269,21 @@ export class Wappi {
|
|
|
246
269
|
return { error: true, message: `Ошибка при создании контакта: ${error.message}`, data: null };
|
|
247
270
|
}
|
|
248
271
|
}
|
|
272
|
+
|
|
273
|
+
// Скачиваем файл по ссылке и конвертируем в base64
|
|
274
|
+
async convertToBase64(fileUrl) {
|
|
275
|
+
try {
|
|
276
|
+
const response = await fetch(fileUrl);
|
|
277
|
+
if (!response.ok) {
|
|
278
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
279
|
+
}
|
|
280
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
281
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
282
|
+
|
|
283
|
+
const base64String = buffer.toString("base64");
|
|
284
|
+
return { error: false, base64: base64String };
|
|
285
|
+
} catch (error) {
|
|
286
|
+
return { error: true, message: `Ошибка при скачивании и конвертировании в base64 файла по fileUrl: ${error.message}` };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
249
289
|
}
|