@andrey4emk/npm-app-back-b24 0.5.18 → 0.5.20
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/smsgold.js +65 -2
package/package.json
CHANGED
package/sendMessage/smsgold.js
CHANGED
|
@@ -3,9 +3,10 @@ import dotEnv from "dotenv";
|
|
|
3
3
|
dotEnv.config();
|
|
4
4
|
|
|
5
5
|
export class Smsgold {
|
|
6
|
-
constructor(authParam) {
|
|
6
|
+
constructor(authParam, b24 = null) {
|
|
7
7
|
this.user = authParam.user;
|
|
8
8
|
this.pass = authParam.pass;
|
|
9
|
+
this.b24 = b24;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
async sendSms(messageData) {
|
|
@@ -17,12 +18,39 @@ export class Smsgold {
|
|
|
17
18
|
if (!phone || !message) {
|
|
18
19
|
return { error: true, message: "Отсутствуют номер телефона или текст сообщения", result: null };
|
|
19
20
|
}
|
|
21
|
+
// Проверяем нужно ли отправить файл
|
|
22
|
+
let publicLink = "";
|
|
23
|
+
if (messageData.fileUrl && messageData.fileName) {
|
|
24
|
+
let fileUrl = messageData.fileUrl;
|
|
25
|
+
let fileName = messageData.fileName;
|
|
26
|
+
// Скачиваем файл по ссылке
|
|
27
|
+
let resDownloadFile = await this.downloadFileToUrl(fileUrl);
|
|
28
|
+
if (resDownloadFile.error) {
|
|
29
|
+
return { error: true, message: `Не получилось скачать файл: ${resDownloadFile.message}` };
|
|
30
|
+
}
|
|
31
|
+
// Копируем файл на диск проекта битрикс24. Где автоматически запускается БП на автоматическое удаление файла через 14 дней
|
|
32
|
+
let resUploadFileInFolderB24 = await this.uploadFileInFolderB24(resDownloadFile.buffer, fileName);
|
|
33
|
+
if (resUploadFileInFolderB24.error) {
|
|
34
|
+
return { error: true, message: resUploadFileInFolderB24.message };
|
|
35
|
+
}
|
|
36
|
+
let idFileContract = resUploadFileInFolderB24.data.ID;
|
|
37
|
+
// Получаем публичную ссылку на файл для вставки в сообщение
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
publicLink = await this.b24.callMethod("disk.file.getExternalLink", {
|
|
41
|
+
id: idFileContract,
|
|
42
|
+
});
|
|
43
|
+
publicLink = publicLink.getData().result;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return { error: true, message: `Ошибка при получении публичной ссылки на файл: ${error.message}` };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
20
48
|
try {
|
|
21
49
|
let body = new URLSearchParams({
|
|
22
50
|
user: this.user,
|
|
23
51
|
pass: this.pass,
|
|
24
52
|
action: "send",
|
|
25
|
-
text: message,
|
|
53
|
+
text: message + (publicLink ? `\n${publicLink}` : ""),
|
|
26
54
|
number: phone,
|
|
27
55
|
sender: "potolkiRepa",
|
|
28
56
|
}).toString();
|
|
@@ -59,4 +87,39 @@ export class Smsgold {
|
|
|
59
87
|
return { error: true, message: error.message, result: null };
|
|
60
88
|
}
|
|
61
89
|
}
|
|
90
|
+
|
|
91
|
+
// Функция для закачивания файла в папку битрикс24. Используем метод disk.folder.uploadfile
|
|
92
|
+
async uploadFileInFolderB24(fileBuffer, fileName) {
|
|
93
|
+
try {
|
|
94
|
+
let base64File = await fileBuffer.toString("base64");
|
|
95
|
+
// Используем метод disk.folder.uploadfile
|
|
96
|
+
let res = await this.b24.callMethod("disk.folder.uploadfile", {
|
|
97
|
+
id: 2792881, // Id папки "contract_company" из проекта "Документы и договора" https://repacheb.bitrix24.ru/workgroups/group/193/disk/path/
|
|
98
|
+
data: {
|
|
99
|
+
NAME: fileName,
|
|
100
|
+
},
|
|
101
|
+
fileContent: [fileName, base64File],
|
|
102
|
+
});
|
|
103
|
+
const data = await res.getData().result;
|
|
104
|
+
return { error: false, data: data };
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return { error: true, message: `Ошибка при загрузке файла в папку битрикс24: ${error.message}` };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async downloadFileToUrl(fileUrl) {
|
|
111
|
+
try {
|
|
112
|
+
const response = await fetch(fileUrl);
|
|
113
|
+
if (!response.ok) {
|
|
114
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
115
|
+
}
|
|
116
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
117
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
118
|
+
|
|
119
|
+
// Возвращаем буфер
|
|
120
|
+
return { error: false, buffer: buffer };
|
|
121
|
+
} catch (error) {
|
|
122
|
+
return { error: true, message: `Ошибка при скачивании файла по fileUrl: ${error.message}` };
|
|
123
|
+
}
|
|
124
|
+
}
|
|
62
125
|
}
|