Skip to content

Instantly share code, notes, and snippets.

@p1ho
Last active May 28, 2019 02:17
Show Gist options
  • Save p1ho/d1acd2b941b00949bf7bc04518be0b58 to your computer and use it in GitHub Desktop.
Save p1ho/d1acd2b941b00949bf7bc04518be0b58 to your computer and use it in GitHub Desktop.
Sending Attachment through fb-messenger-cli
// Send an attachment in a thread
// recipient: Url name of recipient, also called vanity (eg: alexandre.rose)
// recipientId : Facebook numeric id of recipient. Can be a person or a thread
// path : path of the file
// callback(err) does not get any data
sendAttachment(recipient, recipientId, path, callback) {
const recipientUrl = `${this.baseUrl }/t/${ recipient}`; // Your recipient;
const utcTimestamp = new Date().getTime();
const localTime = new Date().toLocaleTimeString().replace(/\s+/g, '').toLowerCase();
const messageId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
request.post("https://upload.messenger.com/ajax/mercury/upload.php", {
headers: {
'origin': 'https://www.messenger.com',
'accept-encoding': 'gzip, deflate',
'x-msgr-region': 'ATN',
'accept-language': 'en-US,en;q=0.8',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary',
'accept': '*/*',
'referer': recipientUrl,
'cookie': this.cookie,
'authority': 'www.messenger.com',
},
formData: {
'__a': '1',
'__be': '1',
'__dyn': '7AzkXh8Z398jgDxKy1l0BwRyaF3oyfJLFwgoqwWhEoyUnwgU9GGEcVovkwy3eE99XDG4UiwExW14DwPxSFEW2O9xicG4EnwkUC9z8Kew',
'__pc': 'EXP1:messengerdotcom_pkg',
'__req': '2q',
'__rev': '2335431',
'__user': this.userId,
'dpr': '1.5',
'fb_dtsg': this.fbdtsg,
'jazoest': '21948',
'upload': fs.createReadStream(path),
},
gzip: true,
}, (err, res, body) => {
if (err) {
callback(err);
return;
}
let bodyJson = JSON.parse(body.replace('for (;;);', ''));
let imageId = bodyJson.payload.metadata[0].image_id;
request.post("https://www.messenger.com/messaging/send/?dpr=1", {
headers: {
'origin': 'https://www.messenger.com',
'accept-encoding': 'gzip, deflate',
'x-msgr-region': 'ATN',
'accept-language': 'en-US,en;q=0.8',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
'accept': '*/*',
'referer': recipientUrl,
'cookie': this.cookie,
'authority': 'www.messenger.com'
},
formData: {
'action_type': 'ma-type:user-generated-message',
'author': `fbid:${this.userId}`,
'timestamp': utcTimestamp,
'timestamp_absolute': 'Today',
'timestamp_relative': localTime,
'timestamp_time_passed': '0',
'is_unread': '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:messenger:web',
'has_attachment': 'true',
'image_ids[0]': imageId,
'specific_to_list][0]': `fbid:${recipientId}`,
'specific_to_list[1]': `fbid:${this.userId}`,
'status': '0',
'offline_threading_id': messageId,
'message_id': messageId,
'ephemeral_ttl_mode': '0',
'manual_retry_cnt': '0',
'other_user_fbid': recipientId,
'client': 'mercury',
'__user': this.userId,
'__a': '1',
'__req': '2q',
'__be': '0',
'__pc': 'EXP1:messengerdotcom_pkg',
'fb_dtsg': this.fbdtsg,
'ttstamp': '265817073691196867855211811758658172458277511215256110114',
'__rev': '2335431'
}
}, (err) => {
if (err) {
callback(err);
}
callback();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment