File size: 11,783 Bytes
e192d16 |
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
"use strict";
var utils = require("../utils");
var log = require("npmlog");
var bluebird = require("bluebird");
var allowedProperties = {
attachment: true,
url: true,
sticker: true,
emoji: true,
emojiSize: true,
body: true,
mentions: true,
location: true,
};
module.exports = function (defaultFuncs, api, ctx) {
function uploadAttachment(attachments, callback) {
var uploads = [];
// create an array of promises
for (var i = 0; i < attachments.length; i++) {
if (!utils.isReadableStream(attachments[i])) {
throw {
error:
"Attachment should be a readable stream and not " +
utils.getType(attachments[i]) +
"."
};
}
var form = {
upload_1024: attachments[i],
voice_clip: "true"
};
uploads.push(
defaultFuncs
.postFormData(
"https://upload.facebook.com/ajax/mercury/upload.php",
ctx.jar,
form,
{}
)
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
.then(function (resData) {
if (resData.error) {
throw resData;
}
// We have to return the data unformatted unless we want to change it
// back in sendMessage.
return resData.payload.metadata[0];
})
);
}
// resolve all promises
bluebird
.all(uploads)
.then(function (resData) {
callback(null, resData);
})
.catch(function (err) {
log.error("uploadAttachment", err);
return callback(err);
});
}
function getUrl(url, callback) {
var form = {
image_height: 960,
image_width: 960,
uri: url
};
defaultFuncs
.post(
"https://www.facebook.com/message_share_attachment/fromURI/",
ctx.jar,
form
)
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
.then(function (resData) {
if (resData.error) {
return callback(resData);
}
if (!resData.payload) {
return callback({ error: "Invalid url" });
}
callback(null, resData.payload.share_data.share_params);
})
.catch(function (err) {
log.error("getUrl", err);
return callback(err);
});
}
function sendContent(form, threadID, isSingleUser, messageAndOTID, callback) {
// There are three cases here:
// 1. threadID is of type array, where we're starting a new group chat with users
// specified in the array.
// 2. User is sending a message to a specific user.
// 3. No additional form params and the message goes to an existing group chat.
if (utils.getType(threadID) === "Array") {
for (var i = 0; i < threadID.length; i++) {
form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
}
form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
form["client_thread_id"] = "root:" + messageAndOTID;
log.info("sendMessage", "Sending message to multiple users: " + threadID);
} else {
// This means that threadID is the id of a user, and the chat
// is a single person chat
if (isSingleUser) {
form["specific_to_list[0]"] = "fbid:" + threadID;
form["specific_to_list[1]"] = "fbid:" + ctx.userID;
form["other_user_fbid"] = threadID;
} else {
form["thread_fbid"] = threadID;
}
}
if (ctx.globalOptions.pageID) {
form["author"] = "fbid:" + ctx.globalOptions.pageID;
form["specific_to_list[1]"] = "fbid:" + ctx.globalOptions.pageID;
form["creator_info[creatorID]"] = ctx.userID;
form["creator_info[creatorType]"] = "direct_admin";
form["creator_info[labelType]"] = "sent_message";
form["creator_info[pageID]"] = ctx.globalOptions.pageID;
form["request_user_id"] = ctx.globalOptions.pageID;
form["creator_info[profileURI]"] =
"https://www.facebook.com/profile.php?id=" + ctx.userID;
}
defaultFuncs
.post("https://www.facebook.com/messaging/send/", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
.then(function (resData) {
if (!resData) {
return callback({ error: "Send message failed." });
}
if (resData.error) {
if (resData.error === 1545012) {
log.warn(
"sendMessage",
"Got error 1545012. This might mean that you're not part of the conversation " +
threadID
);
}
return callback(resData);
}
var messageInfo = resData.payload.actions.reduce(function (p, v) {
return (
{
threadID: v.thread_fbid,
messageID: v.message_id,
timestamp: v.timestamp
} || p
);
}, null);
return callback(null, messageInfo);
})
.catch(function (err) {
//log.error("sendMessage", err);
if (utils.getType(err) == "Object" && err.error === "Not logged in.") {
ctx.loggedIn = false;
}
return callback(err);
});
}
function send(form, threadID, messageAndOTID, callback, isGroup) {
// We're doing a query to this to check if the given id is the id of
// a user or of a group chat. The form will be different depending
// on that.
if (utils.getType(threadID) === "Array") {
sendContent(form, threadID, false, messageAndOTID, callback);
} else {
if (utils.getType(isGroup) != "Boolean")
sendContent(form, threadID, threadID.length <= 15, messageAndOTID, callback);
else
sendContent(form, threadID, !isGroup, messageAndOTID, callback);
}
}
function handleUrl(msg, form, callback, cb) {
if (msg.url) {
form["shareable_attachment[share_type]"] = "100";
getUrl(msg.url, function (err, params) {
if (err) {
return callback(err);
}
form["shareable_attachment[share_params]"] = params;
cb();
});
} else {
cb();
}
}
function handleLocation(msg, form, callback, cb) {
if (msg.location) {
if (msg.location.latitude == null || msg.location.longitude == null) {
return callback({ error: "location property needs both latitude and longitude" });
}
form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
form["location_attachment[is_current_location]"] = !!msg.location.current;
}
cb();
}
function handleSticker(msg, form, callback, cb) {
if (msg.sticker) {
form["sticker_id"] = msg.sticker;
}
cb();
}
function handleEmoji(msg, form, callback, cb) {
if (msg.emojiSize != null && msg.emoji == null) {
return callback({ error: "emoji property is empty" });
}
if (msg.emoji) {
if (msg.emojiSize == null) {
msg.emojiSize = "medium";
}
if (
msg.emojiSize != "small" &&
msg.emojiSize != "medium" &&
msg.emojiSize != "large"
) {
return callback({ error: "emojiSize property is invalid" });
}
if (form["body"] != null && form["body"] != "") {
return callback({ error: "body is not empty" });
}
form["body"] = msg.emoji;
form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
}
cb();
}
function handleAttachment(msg, form, callback, cb) {
if (msg.attachment) {
form["image_ids"] = [];
form["gif_ids"] = [];
form["file_ids"] = [];
form["video_ids"] = [];
form["audio_ids"] = [];
if (utils.getType(msg.attachment) !== "Array") {
msg.attachment = [msg.attachment];
}
uploadAttachment(msg.attachment, function (err, files) {
if (err) {
return callback(err);
}
files.forEach(function (file) {
var key = Object.keys(file);
var type = key[0]; // image_id, file_id, etc
form["" + type + "s"].push(file[type]); // push the id
});
cb();
});
} else {
cb();
}
}
function handleMention(msg, form, callback, cb) {
if (msg.mentions) {
for (let i = 0; i < msg.mentions.length; i++) {
const mention = msg.mentions[i];
const tag = mention.tag;
if (typeof tag !== "string") {
return callback({ error: "Mention tags must be strings." });
}
const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
if (offset < 0) {
log.warn(
"handleMention",
'Mention for "' + tag + '" not found in message string.'
);
}
if (mention.id == null) {
log.warn("handleMention", "Mention id should be non-null.");
}
const id = mention.id || 0;
const emptyChar = '\u200E';
form["body"] = emptyChar + msg.body;
form["profile_xmd[" + i + "][offset]"] = offset + 1;
form["profile_xmd[" + i + "][length]"] = tag.length;
form["profile_xmd[" + i + "][id]"] = id;
form["profile_xmd[" + i + "][type]"] = "p";
}
}
cb();
}
return function sendMessage(msg, threadID, callback, replyToMessage, isGroup) {
typeof isGroup == "undefined" ? isGroup = null : "";
if (
!callback &&
(utils.getType(threadID) === "Function" ||
utils.getType(threadID) === "AsyncFunction")
) {
return threadID({ error: "Pass a threadID as a second argument." });
}
if (
!replyToMessage &&
utils.getType(callback) === "String"
) {
replyToMessage = callback;
callback = undefined;
}
var resolveFunc = function () { };
var rejectFunc = function () { };
var returnPromise = new Promise(function (resolve, reject) {
resolveFunc = resolve;
rejectFunc = reject;
});
if (!callback) {
callback = function (err, data) {
if (err) return rejectFunc(err);
resolveFunc(data);
};
}
var msgType = utils.getType(msg);
var threadIDType = utils.getType(threadID);
var messageIDType = utils.getType(replyToMessage);
if (msgType !== "String" && msgType !== "Object") {
return callback({
error:
"Message should be of type string or object and not " + msgType + "."
});
}
// Changing this to accomodate an array of users
if (
threadIDType !== "Array" &&
threadIDType !== "Number" &&
threadIDType !== "String"
) {
return callback({
error:
"ThreadID should be of type number, string, or array and not " +
threadIDType +
"."
});
}
if (replyToMessage && messageIDType !== 'String') {
return callback({
error:
"MessageID should be of type string and not " +
threadIDType +
"."
});
}
if (msgType === "String") {
msg = { body: msg };
}
var disallowedProperties = Object.keys(msg).filter(
prop => !allowedProperties[prop]
);
if (disallowedProperties.length > 0) {
return callback({
error: "Dissallowed props: `" + disallowedProperties.join(", ") + "`"
});
}
var messageAndOTID = utils.generateOfflineThreadingID();
var form = {
client: "mercury",
action_type: "ma-type:user-generated-message",
author: "fbid:" + ctx.userID,
timestamp: Date.now(),
timestamp_absolute: "Today",
timestamp_relative: utils.generateTimestampRelative(),
timestamp_time_passed: "0",
is_unread: false,
is_cleared: false,
is_forward: false,
is_filtered_content: false,
is_filtered_content_bh: false,
is_filtered_content_account: false,
is_filtered_content_quasar: false,
is_filtered_content_invalid_app: false,
is_spoof_warning: false,
source: "source:chat:web",
"source_tags[0]": "source:chat",
body: msg.body ? msg.body.toString() : "",
html_body: false,
ui_push_phase: "V3",
status: "0",
offline_threading_id: messageAndOTID,
message_id: messageAndOTID,
threading_id: utils.generateThreadingID(ctx.clientID),
"ephemeral_ttl_mode:": "0",
manual_retry_cnt: "0",
has_attachment: !!(msg.attachment || msg.url || msg.sticker),
signatureID: utils.getSignatureID(),
replied_to_message_id: replyToMessage
};
handleLocation(msg, form, callback, () =>
handleSticker(msg, form, callback, () =>
handleAttachment(msg, form, callback, () =>
handleUrl(msg, form, callback, () =>
handleEmoji(msg, form, callback, () =>
handleMention(msg, form, callback, () =>
send(form, threadID, messageAndOTID, callback, isGroup)
)
)
)
)
)
);
return returnPromise;
};
}; |