stfca 1.0.2 → 1.0.3
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/CHANGELOG.md +32 -0
- package/README.md +45 -2
- package/checkUpdate.js +109 -0
- package/index.js +460 -1
- package/package.json +22 -46
- package/src/{api/action/addExternalModule.js → addExternalModule.js} +25 -25
- package/src/addUserToGroup.js +115 -0
- package/src/changeAdminStatus.js +103 -0
- package/src/changeArchivedStatus.js +55 -0
- package/src/{api/action/changeAvatar.js → changeAvatar.js} +136 -137
- package/src/changeAvatarV2.js +86 -0
- package/src/changeAvt.js +85 -0
- package/src/{api/action/changeBio.js → changeBio.js} +76 -75
- package/src/{api/messaging/changeBlockedStatus.js → changeBlockedStatus.js} +49 -48
- package/src/changeBlockedStatusMqtt.js +80 -0
- package/src/changeCover.js +72 -0
- package/src/changeGroupImage.js +135 -0
- package/src/changeName.js +79 -0
- package/src/changeNickname.js +59 -0
- package/src/changeThreadColor.js +65 -0
- package/src/changeThreadEmoji.js +55 -0
- package/src/changeUsername.js +59 -0
- package/src/createCommentPost.js +230 -0
- package/src/{api/messaging/createNewGroup.js → createNewGroup.js} +88 -88
- package/src/createPoll.js +71 -0
- package/src/createPost.js +276 -0
- package/src/{api/messaging/deleteMessage.js → deleteMessage.js} +56 -56
- package/src/{api/messaging/deleteThread.js → deleteThread.js} +56 -56
- package/src/editMessage.js +68 -0
- package/src/editMessageOld.js +67 -0
- package/src/follow.js +74 -0
- package/src/forwardAttachment.js +60 -0
- package/src/getAccess.js +112 -0
- package/src/getAvatarUser.js +78 -0
- package/src/{api/action/getCurrentUserID.js → getCurrentUserID.js} +7 -7
- package/src/{api/messaging/getEmojiUrl.js → getEmojiUrl.js} +2 -2
- package/src/{api/messaging/getFriendsList.js → getFriendsList.js} +83 -82
- package/src/{api/messaging/getMessage.js → getMessage.js} +847 -829
- package/src/getRegion.js +7 -0
- package/src/{api/threads/getThreadHistory.js → getThreadHistory.js} +680 -664
- package/src/getThreadHistoryDeprecated.js +71 -0
- package/src/getThreadInfo.js +232 -0
- package/src/getThreadInfoDeprecated.js +56 -0
- package/src/getThreadList.js +213 -0
- package/src/getThreadListDeprecated.js +46 -0
- package/src/getThreadPictures.js +59 -0
- package/src/getUID.js +119 -0
- package/src/{api/users/getUserID.js → getUserID.js} +61 -65
- package/src/getUserInfo.js +66 -0
- package/src/handleFriendRequest.js +46 -0
- package/src/handleMessageRequest.js +47 -0
- package/src/httpGet.js +49 -0
- package/src/httpPost.js +48 -0
- package/src/listenMqtt.js +833 -0
- package/src/logout.js +75 -0
- package/src/markAsDelivered.js +47 -0
- package/src/markAsRead.js +70 -0
- package/src/markAsReadAll.js +40 -0
- package/src/markAsSeen.js +48 -0
- package/src/muteThread.js +45 -0
- package/src/refreshFb_dtsg.js +89 -0
- package/src/removeUserFromGroup.js +79 -0
- package/src/{api/messaging/resolvePhotoUrl.js → resolvePhotoUrl.js} +45 -43
- package/src/{api/messaging/searchForThread.js → searchForThread.js} +53 -52
- package/src/searchStickers.js +53 -0
- package/src/sendMessage.js +379 -0
- package/src/{api/messaging/sendMessageMqtt.js → sendMessageMqtt.js} +322 -323
- package/src/sendTypingIndicator.js +101 -0
- package/src/sendTypingIndicatorV2.js +28 -0
- package/src/setMessageReaction.js +122 -0
- package/src/setMessageReactionMqtt.js +62 -0
- package/src/{api/action/setPostReaction.js → setPostReaction.js} +112 -106
- package/src/setStoryReaction.js +64 -0
- package/src/setTitle.js +90 -0
- package/src/shareContact.js +110 -0
- package/src/shareLink.js +59 -0
- package/src/stopListenMqtt.js +23 -0
- package/src/{api/messaging/threadColors.js → threadColors.js} +131 -128
- package/src/{api/action/unfriend.js → unfriend.js} +52 -54
- package/src/unsendMessage.js +45 -0
- package/src/{api/messaging/uploadAttachment.js → uploadAttachment.js} +93 -95
- package/utils.js +2876 -0
- package/LICENSE-MIT +0 -4
- package/index.d.ts +0 -615
- package/module/config.js +0 -33
- package/module/login.js +0 -48
- package/module/loginHelper.js +0 -722
- package/module/options.js +0 -44
- package/src/api/action/handleFriendRequest.js +0 -57
- package/src/api/action/logout.js +0 -76
- package/src/api/action/refreshFb_dtsg.js +0 -71
- package/src/api/http/httpGet.js +0 -46
- package/src/api/http/httpPost.js +0 -52
- package/src/api/http/postFormData.js +0 -47
- package/src/api/messaging/addUserToGroup.js +0 -68
- package/src/api/messaging/changeAdminStatus.js +0 -122
- package/src/api/messaging/changeArchivedStatus.js +0 -55
- package/src/api/messaging/changeGroupImage.js +0 -90
- package/src/api/messaging/changeNickname.js +0 -70
- package/src/api/messaging/changeThreadColor.js +0 -79
- package/src/api/messaging/changeThreadEmoji.js +0 -106
- package/src/api/messaging/createPoll.js +0 -43
- package/src/api/messaging/editMessage.js +0 -68
- package/src/api/messaging/forwardAttachment.js +0 -51
- package/src/api/messaging/handleMessageRequest.js +0 -65
- package/src/api/messaging/markAsDelivered.js +0 -57
- package/src/api/messaging/markAsRead.js +0 -88
- package/src/api/messaging/markAsReadAll.js +0 -49
- package/src/api/messaging/markAsSeen.js +0 -61
- package/src/api/messaging/muteThread.js +0 -50
- package/src/api/messaging/removeUserFromGroup.js +0 -105
- package/src/api/messaging/sendMessage.js +0 -379
- package/src/api/messaging/sendTypingIndicator.js +0 -67
- package/src/api/messaging/setMessageReaction.js +0 -75
- package/src/api/messaging/setTitle.js +0 -119
- package/src/api/messaging/shareContact.js +0 -49
- package/src/api/messaging/unsendMessage.js +0 -81
- package/src/api/socket/core/connectMqtt.js +0 -179
- package/src/api/socket/core/getSeqID.js +0 -25
- package/src/api/socket/core/getTaskResponseData.js +0 -22
- package/src/api/socket/core/markDelivery.js +0 -12
- package/src/api/socket/core/parseDelta.js +0 -351
- package/src/api/socket/detail/buildStream.js +0 -208
- package/src/api/socket/detail/constants.js +0 -24
- package/src/api/socket/listenMqtt.js +0 -133
- package/src/api/threads/getThreadInfo.js +0 -358
- package/src/api/threads/getThreadList.js +0 -248
- package/src/api/threads/getThreadPictures.js +0 -78
- package/src/api/users/getUserInfo.js +0 -319
- package/src/api/users/getUserInfoV2.js +0 -133
- package/src/core/sendReqMqtt.js +0 -63
- package/src/database/models/index.js +0 -49
- package/src/database/models/thread.js +0 -31
- package/src/database/models/user.js +0 -32
- package/src/database/threadData.js +0 -98
- package/src/database/userData.js +0 -89
- package/src/utils/client.js +0 -214
- package/src/utils/constants.js +0 -23
- package/src/utils/format.js +0 -1111
- package/src/utils/headers.js +0 -41
- package/src/utils/request.js +0 -215
|
@@ -1,52 +1,53 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
let
|
|
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
|
-
.then(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const utils = require("../utils");
|
|
4
|
+
|
|
5
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
6
|
+
return function searchForThread(name, callback) {
|
|
7
|
+
let resolveFunc = function () {};
|
|
8
|
+
let rejectFunc = function () {};
|
|
9
|
+
const returnPromise = new Promise(function (resolve, reject) {
|
|
10
|
+
resolveFunc = resolve;
|
|
11
|
+
rejectFunc = reject;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (!callback) {
|
|
15
|
+
callback = function (err, friendList) {
|
|
16
|
+
if (err) {
|
|
17
|
+
return rejectFunc(err);
|
|
18
|
+
}
|
|
19
|
+
resolveFunc(friendList);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const tmpForm = {
|
|
24
|
+
client: "web_messenger",
|
|
25
|
+
query: name,
|
|
26
|
+
offset: 0,
|
|
27
|
+
limit: 21,
|
|
28
|
+
index: "fbid",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
defaultFuncs
|
|
32
|
+
.post(
|
|
33
|
+
"https://www.facebook.com/ajax/mercury/search_threads.php",
|
|
34
|
+
ctx.jar,
|
|
35
|
+
tmpForm,
|
|
36
|
+
)
|
|
37
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
38
|
+
.then(function (resData) {
|
|
39
|
+
if (resData.error) {
|
|
40
|
+
throw resData;
|
|
41
|
+
}
|
|
42
|
+
if (!resData.payload.mercury_payload.threads) {
|
|
43
|
+
return callback({ error: "Could not find thread `" + name + "`." });
|
|
44
|
+
}
|
|
45
|
+
return callback(
|
|
46
|
+
null,
|
|
47
|
+
resData.payload.mercury_payload.threads.map(utils.formatThread),
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return returnPromise;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var utils = require('../utils.js');
|
|
5
|
+
var log = require('npmlog');
|
|
6
|
+
|
|
7
|
+
module.exports = function (http, api, ctx) {
|
|
8
|
+
function formatData(res) {
|
|
9
|
+
return {
|
|
10
|
+
id: res.node.id,
|
|
11
|
+
image: res.node.image,
|
|
12
|
+
package: res.node.pack != null ? {
|
|
13
|
+
name: res.node.pack.name,
|
|
14
|
+
id: res.node.pack.id
|
|
15
|
+
} : {},
|
|
16
|
+
label: res.node.label
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return function searchStickers(query = '', callback) {
|
|
21
|
+
var cb;
|
|
22
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
23
|
+
cb = function (error, data) {
|
|
24
|
+
data ? resolve(data) : reject(error);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (typeof callback == 'function') cb = callback;
|
|
29
|
+
|
|
30
|
+
var form = {
|
|
31
|
+
fb_api_req_friendly_name: 'StickersFlyoutTagSelectorQuery',
|
|
32
|
+
variables: JSON.stringify({
|
|
33
|
+
stickerWidth: 64,
|
|
34
|
+
stickerHeight: 64,
|
|
35
|
+
stickerInterface: 'messages',
|
|
36
|
+
query
|
|
37
|
+
}),
|
|
38
|
+
doc_id: '4642836929159953'
|
|
39
|
+
}
|
|
40
|
+
http
|
|
41
|
+
.post('https://www.facebook.com/api/graphql/', ctx.jar, form)
|
|
42
|
+
.then(utils.parseAndCheckLogin(ctx, http))
|
|
43
|
+
.then(function (res) {
|
|
44
|
+
return cb(null, res.data.sticker_search.sticker_results.edges.map(formatData));
|
|
45
|
+
})
|
|
46
|
+
.catch(function (err) {
|
|
47
|
+
console.error('searchStickers', err);
|
|
48
|
+
return cb(err);
|
|
49
|
+
});
|
|
50
|
+
return returnPromise;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { Readable } = require("stream");
|
|
4
|
+
const log = require("npmlog");
|
|
5
|
+
const allowedProperties = {
|
|
6
|
+
attachment: true,
|
|
7
|
+
url: true,
|
|
8
|
+
sticker: true,
|
|
9
|
+
emoji: true,
|
|
10
|
+
emojiSize: true,
|
|
11
|
+
body: true,
|
|
12
|
+
mentions: true,
|
|
13
|
+
location: true,
|
|
14
|
+
asPage: true
|
|
15
|
+
};
|
|
16
|
+
const { isReadableStream } = require("../utils");
|
|
17
|
+
const { parseAndCheckLogin } = require("../utils");
|
|
18
|
+
const { getType, generateThreadingID, generateTimestampRelative, generateOfflineThreadingID, getSignatureID } = require("../utils");
|
|
19
|
+
|
|
20
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
21
|
+
function toReadable(input) {
|
|
22
|
+
if (isReadableStream(input)) return input;
|
|
23
|
+
if (Buffer.isBuffer(input)) return Readable.from(input);
|
|
24
|
+
if (typeof input === "string" && fs.existsSync(input) && fs.statSync(input).isFile()) return fs.createReadStream(path.resolve(input));
|
|
25
|
+
throw { error: "Unsupported attachment input. Use stream/buffer/filepath." };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function uploadAttachment(attachments, callback) {
|
|
29
|
+
const uploads = [];
|
|
30
|
+
for (let i = 0; i < attachments.length; i++) {
|
|
31
|
+
if (!isReadableStream(attachments[i])) throw { error: "Attachment should be a readable stream and not " + getType(attachments[i]) + "." };
|
|
32
|
+
const form = { upload_1024: attachments[i], voice_clip: "true" };
|
|
33
|
+
uploads.push(
|
|
34
|
+
defaultFuncs
|
|
35
|
+
.postFormData("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, form, {}, {})
|
|
36
|
+
.then(parseAndCheckLogin(ctx, defaultFuncs))
|
|
37
|
+
.then(resData => {
|
|
38
|
+
if (resData.error) throw resData;
|
|
39
|
+
return resData.payload.metadata[0];
|
|
40
|
+
})
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
Promise.all(uploads)
|
|
44
|
+
.then(resData => callback(null, resData))
|
|
45
|
+
.catch(err => {
|
|
46
|
+
log.error("uploadAttachment", err);
|
|
47
|
+
callback(err);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getUrl(url, callback) {
|
|
52
|
+
const form = { image_height: 960, image_width: 960, uri: url };
|
|
53
|
+
defaultFuncs
|
|
54
|
+
.post("https://www.facebook.com/message_share_attachment/fromURI/", ctx.jar, form)
|
|
55
|
+
.then(parseAndCheckLogin(ctx, defaultFuncs))
|
|
56
|
+
.then(resData => {
|
|
57
|
+
if (resData.error) return callback(resData);
|
|
58
|
+
if (!resData.payload) return callback({ error: "Invalid url" });
|
|
59
|
+
callback(null, resData.payload.share_data.share_params);
|
|
60
|
+
})
|
|
61
|
+
.catch(err => {
|
|
62
|
+
log.error("getUrl", err);
|
|
63
|
+
callback(err);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function sleep(ms) {
|
|
68
|
+
return new Promise(r => setTimeout(r, ms));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function postWithRetry(url, jar, form, tries = 3) {
|
|
72
|
+
let lastErr;
|
|
73
|
+
for (let i = 0; i < tries; i++) {
|
|
74
|
+
try {
|
|
75
|
+
const res = await defaultFuncs.post(url, jar, form).then(parseAndCheckLogin(ctx, defaultFuncs));
|
|
76
|
+
if (res && !res.error) return res;
|
|
77
|
+
lastErr = res;
|
|
78
|
+
if (res && (res.error === 1545003 || res.error === 368)) await sleep(500 * (i + 1));
|
|
79
|
+
else break;
|
|
80
|
+
} catch (e) {
|
|
81
|
+
lastErr = e;
|
|
82
|
+
await sleep(500 * (i + 1));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
throw lastErr || { error: "Send failed" };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function applyPageAuthor(form, msg) {
|
|
89
|
+
const pageID = msg && msg.asPage ? msg.asPage : ctx.globalOptions.pageID;
|
|
90
|
+
if (!pageID) return;
|
|
91
|
+
form["author"] = "fbid:" + pageID;
|
|
92
|
+
form["specific_to_list[1]"] = "fbid:" + pageID;
|
|
93
|
+
form["creator_info[creatorID]"] = ctx.userID;
|
|
94
|
+
form["creator_info[creatorType]"] = "direct_admin";
|
|
95
|
+
form["creator_info[labelType]"] = "sent_message";
|
|
96
|
+
form["creator_info[pageID]"] = pageID;
|
|
97
|
+
form["request_user_id"] = pageID;
|
|
98
|
+
form["creator_info[profileURI]"] = "https://www.facebook.com/profile.php?id=" + ctx.userID;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function applyMentions(msg, form) {
|
|
102
|
+
if (!msg.mentions || !msg.mentions.length) return;
|
|
103
|
+
let body = typeof msg.body === "string" ? msg.body : "";
|
|
104
|
+
const need = [];
|
|
105
|
+
for (const m of msg.mentions) {
|
|
106
|
+
const tag = String(m.tag || "");
|
|
107
|
+
if (tag && !body.includes(tag)) need.push(tag);
|
|
108
|
+
}
|
|
109
|
+
if (need.length) body = (body ? body + " " : "") + need.join(" ");
|
|
110
|
+
const emptyChar = "\u200E";
|
|
111
|
+
form["body"] = emptyChar + body;
|
|
112
|
+
let searchFrom = 0;
|
|
113
|
+
msg.mentions.forEach((m, i) => {
|
|
114
|
+
const tag = String(m.tag || "");
|
|
115
|
+
const from = typeof m.fromIndex === "number" ? m.fromIndex : searchFrom;
|
|
116
|
+
const off = Math.max(0, body.indexOf(tag, from));
|
|
117
|
+
form[`profile_xmd[${i}][offset]`] = off + 1;
|
|
118
|
+
form[`profile_xmd[${i}][length]`] = tag.length;
|
|
119
|
+
form[`profile_xmd[${i}][id]`] = m.id || 0;
|
|
120
|
+
form[`profile_xmd[${i}][type]`] = "p";
|
|
121
|
+
searchFrom = off + tag.length;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function finalizeHasAttachment(form) {
|
|
126
|
+
const keys = ["image_ids", "gif_ids", "file_ids", "video_ids", "audio_ids", "sticker_id", "shareable_attachment[share_params]"];
|
|
127
|
+
form.has_attachment = keys.some(k => k in form && (Array.isArray(form[k]) ? form[k].length > 0 : !!form[k]));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function extractMessageInfo(resData, fallbackThreadID) {
|
|
131
|
+
let messageID = null;
|
|
132
|
+
let threadFBID = null;
|
|
133
|
+
let timestamp = null;
|
|
134
|
+
const actions = resData && resData.payload && Array.isArray(resData.payload.actions) ? resData.payload.actions : null;
|
|
135
|
+
if (actions && actions.length) {
|
|
136
|
+
const v = actions.find(x => x && x.message_id) || actions[0];
|
|
137
|
+
messageID = v && v.message_id ? v.message_id : null;
|
|
138
|
+
threadFBID = (v && (v.thread_fbid || v.thread_id)) || fallbackThreadID || null;
|
|
139
|
+
timestamp = v && v.timestamp ? v.timestamp : null;
|
|
140
|
+
}
|
|
141
|
+
if (!messageID) messageID = (resData && resData.payload && resData.payload.message_id) || resData.message_id || null;
|
|
142
|
+
if (!threadFBID) threadFBID = (resData && resData.payload && resData.payload.thread_id) || fallbackThreadID || null;
|
|
143
|
+
if (!timestamp) timestamp = (resData && resData.timestamp) || Date.now();
|
|
144
|
+
if (!messageID) return null;
|
|
145
|
+
return { threadID: threadFBID, messageID, timestamp };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function sendContent(form, threadID, isSingleUser, messageAndOTID, callback) {
|
|
149
|
+
if (getType(threadID) === "Array") {
|
|
150
|
+
for (let i = 0; i < threadID.length; i++) form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
|
|
151
|
+
form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
|
|
152
|
+
form["client_thread_id"] = "root:" + messageAndOTID;
|
|
153
|
+
} else {
|
|
154
|
+
if (isSingleUser) {
|
|
155
|
+
form["specific_to_list[0]"] = "fbid:" + threadID;
|
|
156
|
+
form["specific_to_list[1]"] = "fbid:" + ctx.userID;
|
|
157
|
+
form["other_user_fbid"] = threadID;
|
|
158
|
+
} else {
|
|
159
|
+
form["thread_fbid"] = threadID;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
postWithRetry("https://www.facebook.com/messaging/send/", ctx.jar, form)
|
|
163
|
+
.then(resData => {
|
|
164
|
+
if (!resData) return callback({ error: "Send message failed." });
|
|
165
|
+
if (resData.error) {
|
|
166
|
+
if (resData.error === 1545012) log.warn("sendMessage", "Got error 1545012. This might mean that you're not part of the conversation " + threadID);
|
|
167
|
+
else log.error("sendMessage", resData);
|
|
168
|
+
return callback(resData);
|
|
169
|
+
}
|
|
170
|
+
const info = extractMessageInfo(resData, getType(threadID) === "Array" ? null : String(threadID));
|
|
171
|
+
if (!info) return callback({ error: "Cannot parse message info." });
|
|
172
|
+
callback(null, info);
|
|
173
|
+
})
|
|
174
|
+
.catch(err => {
|
|
175
|
+
log.error("sendMessage", err);
|
|
176
|
+
if (getType(err) === "Object" && err.error === "Not logged in.") ctx.loggedIn = false;
|
|
177
|
+
callback(err);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function sendOnce(baseForm, threadID, isSingleUser) {
|
|
182
|
+
const otid = generateOfflineThreadingID();
|
|
183
|
+
const form = { ...baseForm, offline_threading_id: otid, message_id: otid };
|
|
184
|
+
return new Promise((resolve, reject) => {
|
|
185
|
+
sendContent(form, threadID, isSingleUser, otid, (err, info) => (err ? reject(err) : resolve(info)));
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function send(form, threadID, messageAndOTID, callback, isGroup) {
|
|
190
|
+
if (getType(threadID) === "Array") return sendContent(form, threadID, false, messageAndOTID, callback);
|
|
191
|
+
if (getType(isGroup) === "Boolean") return sendContent(form, threadID, !isGroup, messageAndOTID, callback);
|
|
192
|
+
sendOnce(form, threadID, false)
|
|
193
|
+
.then(info => callback(null, info))
|
|
194
|
+
.catch(() => {
|
|
195
|
+
sendOnce(form, threadID, true)
|
|
196
|
+
.then(info => callback(null, info))
|
|
197
|
+
.catch(err => callback(err));
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function handleUrl(msg, form, callback, cb) {
|
|
202
|
+
if (msg.url) {
|
|
203
|
+
form["shareable_attachment[share_type]"] = "100";
|
|
204
|
+
getUrl(msg.url, function (err, params) {
|
|
205
|
+
if (err) return callback(err);
|
|
206
|
+
form["shareable_attachment[share_params]"] = params;
|
|
207
|
+
cb();
|
|
208
|
+
});
|
|
209
|
+
} else cb();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function handleLocation(msg, form, callback, cb) {
|
|
213
|
+
if (msg.location) {
|
|
214
|
+
if (msg.location.latitude == null || msg.location.longitude == null) return callback({ error: "location property needs both latitude and longitude" });
|
|
215
|
+
form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
|
|
216
|
+
form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
|
|
217
|
+
form["location_attachment[is_current_location]"] = !!msg.location.current;
|
|
218
|
+
}
|
|
219
|
+
cb();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function handleSticker(msg, form, callback, cb) {
|
|
223
|
+
if (msg.sticker) form["sticker_id"] = msg.sticker;
|
|
224
|
+
cb();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function handleEmoji(msg, form, callback, cb) {
|
|
228
|
+
if (msg.emojiSize != null && msg.emoji == null) return callback({ error: "emoji property is empty" });
|
|
229
|
+
if (msg.emoji) {
|
|
230
|
+
if (msg.emojiSize == null) msg.emojiSize = "medium";
|
|
231
|
+
if (msg.emojiSize !== "small" && msg.emojiSize !== "medium" && msg.emojiSize !== "large") return callback({ error: "emojiSize property is invalid" });
|
|
232
|
+
if (form["body"] != null && form["body"] !== "") return callback({ error: "body is not empty" });
|
|
233
|
+
form["body"] = msg.emoji;
|
|
234
|
+
form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
|
|
235
|
+
}
|
|
236
|
+
cb();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function splitAttachments(list) {
|
|
240
|
+
if (!Array.isArray(list)) list = [list];
|
|
241
|
+
const ids = [];
|
|
242
|
+
const streams = [];
|
|
243
|
+
for (const a of list) {
|
|
244
|
+
if (Array.isArray(a) && /_id$/.test(a[0])) {
|
|
245
|
+
ids.push([a[0], String(a[1])]);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (a && typeof a === "object") {
|
|
249
|
+
if (a.id && a.type && /_id$/.test(a.type)) {
|
|
250
|
+
ids.push([a.type, String(a.id)]);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const k = Object.keys(a || {}).find(x => /_id$/.test(x));
|
|
254
|
+
if (k) {
|
|
255
|
+
ids.push([k, String(a[k])]);
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
streams.push(toReadable(a));
|
|
260
|
+
}
|
|
261
|
+
return { ids, streams };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function handleAttachment(msg, form, callback, cb) {
|
|
265
|
+
if (!msg.attachment) return cb();
|
|
266
|
+
form["image_ids"] = [];
|
|
267
|
+
form["gif_ids"] = [];
|
|
268
|
+
form["file_ids"] = [];
|
|
269
|
+
form["video_ids"] = [];
|
|
270
|
+
form["audio_ids"] = [];
|
|
271
|
+
const { ids, streams } = splitAttachments(msg.attachment);
|
|
272
|
+
for (const [type, id] of ids) form[`${type}s`].push(id);
|
|
273
|
+
if (!streams.length) return cb();
|
|
274
|
+
uploadAttachment(streams, function (err, files) {
|
|
275
|
+
if (err) return callback(err);
|
|
276
|
+
files.forEach(function (file) {
|
|
277
|
+
const type = Object.keys(file)[0];
|
|
278
|
+
form[type + "s"].push(file[type]);
|
|
279
|
+
});
|
|
280
|
+
cb();
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function handleMention(msg, form, callback, cb) {
|
|
285
|
+
try {
|
|
286
|
+
applyMentions(msg, form);
|
|
287
|
+
cb();
|
|
288
|
+
} catch (e) {
|
|
289
|
+
callback(e);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return function sendMessage(msg, threadID, callback, replyToMessage, isGroup) {
|
|
294
|
+
const isFn = v => typeof v === "function";
|
|
295
|
+
const isStr = v => typeof v === "string";
|
|
296
|
+
|
|
297
|
+
if (typeof isGroup === "undefined") isGroup = null;
|
|
298
|
+
if (!callback && (getType(threadID) === "Function" || getType(threadID) === "AsyncFunction")) return threadID({ error: "Pass a threadID as a second argument." });
|
|
299
|
+
|
|
300
|
+
if (isStr(callback) && isFn(replyToMessage)) {
|
|
301
|
+
const t = callback;
|
|
302
|
+
callback = replyToMessage;
|
|
303
|
+
replyToMessage = t;
|
|
304
|
+
} else if (!replyToMessage && isStr(callback)) {
|
|
305
|
+
replyToMessage = callback;
|
|
306
|
+
callback = null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let resolveFunc = function () { };
|
|
310
|
+
let rejectFunc = function () { };
|
|
311
|
+
const returnPromise = new Promise(function (resolve, reject) {
|
|
312
|
+
resolveFunc = resolve;
|
|
313
|
+
rejectFunc = reject;
|
|
314
|
+
});
|
|
315
|
+
if (!callback) {
|
|
316
|
+
callback = function (err, data) {
|
|
317
|
+
if (err) return rejectFunc(err);
|
|
318
|
+
resolveFunc(data);
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
const msgType = getType(msg);
|
|
322
|
+
const threadIDType = getType(threadID);
|
|
323
|
+
const messageIDType = getType(replyToMessage);
|
|
324
|
+
if (msgType !== "String" && msgType !== "Object") return callback({ error: "Message should be of type string or object and not " + msgType + "." });
|
|
325
|
+
if (threadIDType !== "Array" && threadIDType !== "Number" && threadIDType !== "String") return callback({ error: "ThreadID should be of type number, string, or array and not " + threadIDType + "." });
|
|
326
|
+
if (replyToMessage && messageIDType !== "String") return callback({ error: "MessageID should be of type string and not " + threadIDType + "." });
|
|
327
|
+
if (msgType === "String") msg = { body: msg };
|
|
328
|
+
const disallowedProperties = Object.keys(msg).filter(prop => !allowedProperties[prop]);
|
|
329
|
+
if (disallowedProperties.length > 0) return callback({ error: "Disallowed props: `" + disallowedProperties.join(", ") + "`" });
|
|
330
|
+
const messageAndOTID = generateOfflineThreadingID();
|
|
331
|
+
const form = {
|
|
332
|
+
client: "mercury",
|
|
333
|
+
action_type: "ma-type:user-generated-message",
|
|
334
|
+
author: "fbid:" + ctx.userID,
|
|
335
|
+
timestamp: Date.now(),
|
|
336
|
+
timestamp_absolute: "Today",
|
|
337
|
+
timestamp_relative: generateTimestampRelative(),
|
|
338
|
+
timestamp_time_passed: "0",
|
|
339
|
+
is_unread: false,
|
|
340
|
+
is_cleared: false,
|
|
341
|
+
is_forward: false,
|
|
342
|
+
is_filtered_content: false,
|
|
343
|
+
is_filtered_content_bh: false,
|
|
344
|
+
is_filtered_content_account: false,
|
|
345
|
+
is_filtered_content_quasar: false,
|
|
346
|
+
is_filtered_content_invalid_app: false,
|
|
347
|
+
is_spoof_warning: false,
|
|
348
|
+
source: "source:chat:web",
|
|
349
|
+
"source_tags[0]": "source:chat",
|
|
350
|
+
body: msg.body ? msg.body.toString() : "",
|
|
351
|
+
html_body: false,
|
|
352
|
+
ui_push_phase: "V3",
|
|
353
|
+
status: "0",
|
|
354
|
+
offline_threading_id: messageAndOTID,
|
|
355
|
+
message_id: messageAndOTID,
|
|
356
|
+
threading_id: generateThreadingID(ctx.clientID),
|
|
357
|
+
ephemeral_ttl_mode: "0",
|
|
358
|
+
manual_retry_cnt: "0",
|
|
359
|
+
signatureID: getSignatureID(),
|
|
360
|
+
replied_to_message_id: replyToMessage ? replyToMessage.toString() : ""
|
|
361
|
+
};
|
|
362
|
+
applyPageAuthor(form, msg);
|
|
363
|
+
handleLocation(msg, form, callback, () =>
|
|
364
|
+
handleSticker(msg, form, callback, () =>
|
|
365
|
+
handleAttachment(msg, form, callback, () =>
|
|
366
|
+
handleUrl(msg, form, callback, () =>
|
|
367
|
+
handleEmoji(msg, form, callback, () =>
|
|
368
|
+
handleMention(msg, form, callback, () => {
|
|
369
|
+
finalizeHasAttachment(form);
|
|
370
|
+
send(form, threadID, messageAndOTID, callback, isGroup);
|
|
371
|
+
})
|
|
372
|
+
)
|
|
373
|
+
)
|
|
374
|
+
)
|
|
375
|
+
)
|
|
376
|
+
);
|
|
377
|
+
return returnPromise;
|
|
378
|
+
};
|
|
379
|
+
};
|