Alex-Rose / fb-messenger-cli

fb-messenger-cli, console Facebook messenger
759 stars 99 forks source link

Add the ability to send attachments both from filename and from the clipboard #90

Open justanironic opened 7 years ago

justanironic commented 7 years ago

I often screenshot things to send to my friends, and I usually just use lightshot to copy a portion of my screen into a picture. Adding a /send clipboard command would be very useful, as well as /send attachment {filepath} command would be very useful.

sarangjo commented 7 years ago

Is there a Messenger API endpoint that exists for something like this? @SamBergeron @Alex-Rose this would be pretty fantastic.

jackpetraitis commented 6 years ago

+1 for this. Not sure how we'd do it.

p1ho commented 5 years ago

There is this Attachment Upload API, not sure if this will work.

I can play around with it, and add my updates here.

UPDATE: I made this comment before having taken a deeper look at the source code. I see that the messenger essentially tries to emulate the actions that would have been done in the browser, instead of using the Messenger Platform API.

In this case, I looked at the requests that are sent when uploading an attachment.

  1. bringing up uplpoad box: POST to https://www.messenger.com/ajax/bz
  2. after selecting file:
    • OPTIONS to https://upload.messenger.com/ajax/mercury/upload.php
    • POST to https://upload.messenger.com/ajax/mercury/upload.php
  3. after sending:
    • OPTIONS to https://upload.messenger.com/ajax/mercury/upload.php
    • POST to https://upload.messenger.com/ajax/mercury/upload.php which will have a response with the fbid of the uploaded file.
    • POST to https://www.messenger.com/messaging/send/ with has_attachment set to true and parameter pointing to the id of the uploaded file
p1ho commented 5 years ago

@justanironic @sarangjo @jackpetraitis I got it working!! Here's the gist of the function.

Essentially what happens is you must first upload (POST) the resource to the endpoint https://upload.messenger.com/ajax/mercury/upload.php and create a binary stream of your file using fs.createReadStream(path). Inside the response body from the endpoint exists the imageId that points to the uploaded resource.

You then send a second POST to the normal endpoint https://www.messenger.com/messaging/send/ but include "has_attachment": "true" and "image_ids[0]": imageId in the form data. (I'm still testing different file types)

However, there existed a bug where if someone uploads a resource, you have to do /r to get that information, it doesn't automatically get updated, but if you have your messenger opened in the web browser, you should see this file uploaded correctly. (Someone should be on it though)

UPDATE: I've made a PR using my fork You're able to now go to a thread and type /a path-to-file and it will upload the file. The bug mentioned above will prevent you from seeing it working in the commandline, so I'd have the messenger webpage pulled up as well.