put notes/eebb1191-a95f-4bd1-9b09-526738008543.json
Browse files
notes/eebb1191-a95f-4bd1-9b09-526738008543.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"id": "eebb1191-a95f-4bd1-9b09-526738008543",
|
3 |
+
"title": "sendMessageMqtt.js",
|
4 |
+
"content": "var utils = require(\"../utils\");\n\nmodule.exports = function (defaultFuncs, api, ctx) {\n function uploadAttachment(attachments, callback) {\n callback = callback || function () {};\n var uploads = [];\n\n // create an array of promises\n for (var i = 0; i < attachments.length; i++) {\n if (!utils.isReadableStream(attachments[i])) {\n throw {\n error:\n \"Attachment should be a readable stream and not \" +\n utils.getType(attachments[i]) +\n \".\",\n };\n }\n\n var form = {\n upload_1024: attachments[i],\n voice_clip: \"true\",\n };\n\n uploads.push(\n defaultFuncs\n .postFormData(\n \"https://upload.facebook.com/ajax/mercury/upload.php\",\n ctx.jar,\n form,\n {},\n )\n .then(utils.parseAndCheckLogin(ctx, defaultFuncs))\n .then(function (resData) {\n if (resData.error) {\n throw resData;\n }\n\n // We have to return the data unformatted unless we want to change it\n // back in sendMessage.\n return resData.payload.metadata[0];\n }),\n );\n }\n\n // resolve all promises\n Promise.all(uploads)\n .then(function (resData) {\n callback(null, resData);\n })\n .catch(function (err) {\n console.error(\"uploadAttachment\", err);\n return callback(err);\n });\n }\n\n let variance = 0;\n const epoch_id = () =>\n Math.floor(Date.now() * (4194304 + (variance = (variance + 0.1) % 5)));\n const emojiSizes = {\n small: 1,\n medium: 2,\n large: 3,\n };\n\n function handleEmoji(msg, form, callback, cb) {\n if (msg.emojiSize != null && msg.emoji == null) {\n return callback({ error: \"emoji property is empty\" });\n }\n if (msg.emoji) {\n if (!msg.emojiSize) {\n msg.emojiSize = \"small\";\n }\n if (\n msg.emojiSize !== \"small\" &&\n msg.emojiSize !== \"medium\" &&\n msg.emojiSize !== \"large\" &&\n (isNaN(msg.emojiSize) || msg.emojiSize < 1 || msg.emojiSize > 3)\n ) {\n return callback({ error: \"emojiSize property is invalid\" });\n }\n\n form.payload.tasks[0].payload.send_type = 1;\n form.payload.tasks[0].payload.text = msg.emoji;\n form.payload.tasks[0].payload.hot_emoji_size = !isNaN(msg.emojiSize)\n ? msg.emojiSize\n : emojiSizes[msg.emojiSize];\n }\n cb();\n }\n\n function handleSticker(msg, form, callback, cb) {\n if (msg.sticker) {\n form.payload.tasks[0].payload.send_type = 2;\n form.payload.tasks[0].payload.sticker_id = msg.sticker;\n }\n cb();\n }\n\n function handleAttachment(msg, form, callback, cb) {\n if (msg.attachment) {\n form.payload.tasks[0].payload.send_type = 3;\n form.payload.tasks[0].payload.attachment_fbids = [];\n if (form.payload.tasks[0].payload.text == \"\")\n form.payload.tasks[0].payload.text = null;\n if (utils.getType(msg.attachment) !== \"Array\") {\n msg.attachment = [msg.attachment];\n }\n\n uploadAttachment(msg.attachment, function (err, files) {\n if (err) {\n return callback(err);\n }\n\n files.forEach(function (file) {\n var key = Object.keys(file);\n var type = key[0]; // image_id, file_id, etc\n form.payload.tasks[0].payload.attachment_fbids.push(file[type]); // push the id\n });\n cb();\n });\n } else {\n cb();\n }\n }\n\n function handleMention(msg, form, callback, cb) {\n if (msg.mentions) {\n form.payload.tasks[0].payload.send_type = 1;\n\n const arrayIds = [];\n const arrayOffsets = [];\n const arrayLengths = [];\n const mention_types = [];\n\n for (let i = 0; i < msg.mentions.length; i++) {\n const mention = msg.mentions[i];\n\n const tag = mention.tag;\n if (typeof tag !== \"string\") {\n return callback({ error: \"Mention tags must be strings.\" });\n }\n\n const offset = msg.body.indexOf(tag, mention.fromIndex || 0);\n\n if (offset < 0) {\n console.warn(\n \"handleMention\",\n 'Mention for \"' + tag + '\" not found in message string.',\n );\n }\n\n if (mention.id == null) {\n console.warn(\"handleMention\", \"Mention id should be non-null.\");\n }\n\n const id = mention.id || 0;\n arrayIds.push(id);\n arrayOffsets.push(offset);\n arrayLengths.push(tag.length);\n mention_types.push(\"p\");\n }\n\n form.payload.tasks[0].payload.mention_data = {\n mention_ids: arrayIds.join(\",\"),\n mention_offsets: arrayOffsets.join(\",\"),\n mention_lengths: arrayLengths.join(\",\"),\n mention_types: mention_types.join(\",\"),\n };\n }\n cb();\n }\n\n function handleLocation(msg, form, callback, cb) {\n // this is not working yet\n if (msg.location) {\n if (msg.location.latitude == null || msg.location.longitude == null) {\n return callback({\n error: \"location property needs both latitude and longitude\",\n });\n }\n\n form.payload.tasks[0].payload.send_type = 1;\n form.payload.tasks[0].payload.location_data = {\n coordinates: {\n latitude: msg.location.latitude,\n longitude: msg.location.longitude,\n },\n is_current_location: !!msg.location.current,\n is_live_location: !!msg.location.live,\n };\n }\n\n cb();\n }\n\n function send(form, threadID, callback, replyToMessage) {\n if (replyToMessage) {\n form.payload.tasks[0].payload.reply_metadata = {\n reply_source_id: replyToMessage,\n reply_source_type: 1,\n reply_type: 0,\n };\n }\n const mqttClient = ctx.mqttClient;\n form.payload.tasks.forEach((task) => {\n task.payload = JSON.stringify(task.payload);\n });\n form.payload = JSON.stringify(form.payload);\n return mqttClient.publish(\n \"/ls_req\",\n JSON.stringify(form),\n function (err, data) {\n if (err) {\n console.error(\"Error publishing message: \", err);\n callback(err);\n } else {\n callback(null, data);\n }\n },\n );\n }\n\n return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {\n if (\n !callback &&\n (utils.getType(threadID) === \"Function\" ||\n utils.getType(threadID) === \"AsyncFunction\")\n ) {\n return threadID({ error: \"Pass a threadID as a second argument.\" });\n }\n if (!replyToMessage && utils.getType(callback) === \"String\") {\n replyToMessage = callback;\n callback = function () {};\n }\n\n if (!callback) {\n callback = function (err, friendList) {};\n }\n\n var msgType = utils.getType(msg);\n var threadIDType = utils.getType(threadID);\n var messageIDType = utils.getType(replyToMessage);\n\n if (msgType !== \"String\" && msgType !== \"Object\") {\n return callback({\n error:\n \"Message should be of type string or object and not \" + msgType + \".\",\n });\n }\n\n if (msgType === \"String\") {\n msg = { body: msg };\n }\n\n const timestamp = Date.now();\n // get full date time\n const epoch = timestamp << 22;\n //const otid = epoch + 0; // TODO replace with randomInt(0, 2**22)\n const otid = epoch + Math.floor(Math.random() * 4194304);\n\n const form = {\n app_id: \"2220391788200892\",\n payload: {\n tasks: [\n {\n label: \"46\",\n payload: {\n thread_id: threadID.toString(),\n otid: otid.toString(),\n source: 0,\n send_type: 1,\n sync_group: 1,\n text:\n msg.body != null && msg.body != undefined\n ? msg.body.toString()\n : \"\",\n initiating_source: 1,\n skip_url_preview_gen: 0,\n },\n queue_name: threadID.toString(),\n task_id: 0,\n failure_count: null,\n },\n {\n label: \"21\",\n payload: {\n thread_id: threadID.toString(),\n last_read_watermark_ts: Date.now(),\n sync_group: 1,\n },\n queue_name: threadID.toString(),\n task_id: 1,\n failure_count: null,\n },\n ],\n epoch_id: epoch_id(),\n version_id: \"6120284488008082\",\n data_trace_id: null,\n },\n request_id: 1,\n type: 3,\n };\n\n handleEmoji(msg, form, callback, function () {\n handleLocation(msg, form, callback, function () {\n handleMention(msg, form, callback, function () {\n handleSticker(msg, form, callback, function () {\n handleAttachment(msg, form, callback, function () {\n send(form, threadID, callback, replyToMessage);\n });\n });\n });\n });\n });\n };\n};\n",
|
5 |
+
"language": "javascript",
|
6 |
+
"createdAt": 1756718467716,
|
7 |
+
"updatedAt": 1756718467716
|
8 |
+
}
|