-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
send-message-scope.js
161 lines (147 loc) · 4.02 KB
/
send-message-scope.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* send message in options mode
* @param {string} to contact number
* @param {string} body message body
* @param {object} options shipping options
*/
export async function sendMessage(to, body, options = {}) {
const types = [
'sendText',
'sendAudioBase64',
'sendImageFromBase64',
'sendAudio',
'sendFile',
'sendImage'
];
let typesObj;
types.reduce(
(a, v) =>
(typesObj = {
...a,
[v]: v
}),
{}
);
if (!body) {
return WAPI.scope(undefined, true, null, `parameters are missing`);
}
if (!options.type || (options.type && !types.includes(options.type))) {
return WAPI.scope(
undefined,
true,
null,
`pass the message type, examples: ${types.join(', ')}`
);
}
const chat = await WAPI.sendExist(to);
const merge = {};
if (chat && chat.status != 404 && chat.id) {
const newMsgId = await WAPI.getNewMessageId(chat.id._serialized);
const fromwWid = await Store.MaybeMeUser.getMaybeMeUser();
if (options.type === typesObj.sendText) {
merge.type = 'chat';
}
if (
options.type === typesObj.sendAudioBase64 ||
options.type === typesObj.sendAudio ||
options.type === typesObj.sendFile ||
options.type === typesObj.sendImage ||
options.type === typesObj.sendImageFromBase64
) {
const chatWid = new Store.WidFactory.createWid(to);
await Store.Chat.add(
{
createdLocally: true,
id: chatWid
},
{
merge: true
}
);
let result = await Store.Chat.find(chat.id);
const mediaBlob = WAPI.base64ToFile(body);
const mc = await WAPI.processFiles(result, mediaBlob);
if (typeof mc === 'object' && mc._models && mc._models[0]) {
const media = mc._models[0];
let enc, type;
if (
options.type === typesObj.sendFile ||
options.type === typesObj.sendImage ||
options.type === typesObj.sendImageFromBase64
) {
type = media.type;
merge.caption = options?.caption;
merge.filename = options?.filename;
enc = await WAPI.encryptAndUploadFile(type, mediaBlob);
} else {
type = 'ptt';
enc = await WAPI.encryptAndUploadFile(type, mediaBlob);
}
if (enc === false) {
return WAPI.scope(
chat.id,
true,
404,
'Error to encryptAndUploadFile'
);
}
merge.type = type;
merge.duration = media?.__x_mediaPrep?._mediaData?.duration;
merge.mimetype = media.mimetype;
merge.size = media.filesize;
merge.deprecatedMms3Url = enc.url;
merge.directPath = enc.directPath;
merge.encFilehash = enc.encFilehash;
merge.filehash = enc.filehash;
merge.mediaKeyTimestamp = enc.mediaKeyTimestamp;
merge.ephemeralStartTimestamp = enc.mediaKeyTimestamp;
merge.mediaKey = enc.mediaKey;
body = undefined;
}
}
if (!Object.keys(merge).length) {
return WAPI.scope(undefined, true, null, 'Error sending message');
}
const message = WAPI.baseSendMessage(
{
to,
body,
newMsgId,
fromwWid,
chat
},
merge
);
try {
const result = (
await Promise.all(Store.addAndSendMsgToChat(chat, message))
)[1];
if (result === 'OK' || result.messageSendResult === 'OK') {
return WAPI.scope(newMsgId, false, result, null, options.type, body);
}
throw result;
} catch (result) {
return WAPI.scope(newMsgId, true, result, null, options.type, body);
}
} else {
if (!chat.erro) {
chat.erro = true;
}
return chat;
}
}
export function baseSendMessage(param, merge) {
const message = {
id: param.newMsgId,
ack: 0,
body: param?.body,
from: param.fromwWid,
to: param.chat.id,
local: !0,
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
...merge
};
return message;
}