@andrey4emk/npm-app-back-b24 0.5.17 → 0.5.18
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/chatApp.js +54 -0
package/package.json
CHANGED
package/sendMessage/chatApp.js
CHANGED
|
@@ -79,6 +79,60 @@ export class ChatApp {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
async sendFileChatApp(messangerType, messageData) {
|
|
83
|
+
// Проверяем что верно указан messangerType и в messageData есть phone и message
|
|
84
|
+
if (!["whatsApp"].includes(messangerType) && !["telegram"].includes(messangerType)) {
|
|
85
|
+
return { error: true, message: "Неверный тип мессенджера", data: null };
|
|
86
|
+
}
|
|
87
|
+
if (!messageData.phone || !messageData.message) {
|
|
88
|
+
return { error: true, message: "Отсутствует phone или message", data: null };
|
|
89
|
+
}
|
|
90
|
+
let check = await this.checkTokenChatApp();
|
|
91
|
+
if (check.error) {
|
|
92
|
+
return { error: true, message: `Ошибка с токеном ChatApp в функции sendMessageChatApp класса ChatApp\n${check.message}`, data: null };
|
|
93
|
+
}
|
|
94
|
+
let phone = messageData.phone;
|
|
95
|
+
let caption = messageData.message;
|
|
96
|
+
let file = messageData.fileUrl;
|
|
97
|
+
let fileName = messageData.fileName;
|
|
98
|
+
|
|
99
|
+
let url;
|
|
100
|
+
if (messangerType === "whatsApp") {
|
|
101
|
+
url = `https://api.chatapp.online/v1/licenses/${this.type.whatsApp.licenseId}/messengers/${this.type.whatsApp.messenger[0].type}/chats/${phone}/messages/file`;
|
|
102
|
+
}
|
|
103
|
+
if (messangerType === "telegram") {
|
|
104
|
+
url = `https://api.chatapp.online/v1/licenses/${this.type.telegram.licenseId}/messengers/${this.type.telegram.messenger[0].type}/chats/${phone}/messages/file`;
|
|
105
|
+
}
|
|
106
|
+
let res = await fetch(url, {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers: {
|
|
109
|
+
Lang: "en",
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
Accept: "application/json",
|
|
112
|
+
Authorization: this.auth.accessToken,
|
|
113
|
+
},
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
file: file,
|
|
116
|
+
fileName: fileName,
|
|
117
|
+
caption: caption,
|
|
118
|
+
}),
|
|
119
|
+
});
|
|
120
|
+
let data = await res.json();
|
|
121
|
+
|
|
122
|
+
if (!data.success) {
|
|
123
|
+
return { error: true, message: `Ошибка при отправке сообщения в ChatApp через ${messangerType}`, data: data };
|
|
124
|
+
} else {
|
|
125
|
+
// Если в процессе запроса обновили токен, то возвращаем информацию об этом.
|
|
126
|
+
let newAuth = null;
|
|
127
|
+
if (this.updateToken) {
|
|
128
|
+
newAuth = this.auth;
|
|
129
|
+
// Сбрасываем флаг обновления токена
|
|
130
|
+
this.updateToken = false;
|
|
131
|
+
}
|
|
132
|
+
return { error: false, message: `Сообщение успешно отправлено в ChatApp через ${messangerType}`, data: data, auth: newAuth };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
82
136
|
async phoneCheckChatApp(messangerType, phone) {
|
|
83
137
|
if (!["whatsApp"].includes(messangerType) && !["telegram"].includes(messangerType)) {
|
|
84
138
|
return { error: true, message: "Неверный тип мессенджера", data: null };
|