Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Messenger SDK to send file data

I have a webview that opens from a messenger bot.

From the webview I want to send image data to the conversation (no URL - data coming from the canvas).

I tried to use Messenger SDK beginShareFlow with file data attachment:

function uploadImage(data) {
        let message = {
            "attachment": {
                "type": "image",
                "payload": {
                    "is_reusable": true
                },
                "filedata": data
            }
        };

        MessengerExtensions.beginShareFlow(function (share_response) {
                // User dismissed without error
                if (share_response.is_sent) {
                    // The user actually did share.
                    //close the webview
                    MessengerExtensions.requestCloseBrowser(function success() {
                        // webview closed
                    }, function error(err) {
                        console.log(err);
                    });
                }
            },
            function (errorCode, errorMessage) {
                // An error occurred in the process
                console.log(errorMessage);
            },
            message, "current_thread");
    }

But I get an error:

Messenger Extensions unexpected error.

Would appreciate help =]

EDIT:

I found out that filedata is used to transfer a file location (which I do not have).

So I tried other solutions:

  • I created from my cavas blob, and tried to pass it in filedata - did not work
  • I created a blob file (by adding name and date) and tried to move the location - did not work
  • I created a url from blob and tried to move it as a url (not as filedata) - and got an error:

Invalid image URL provided in message content

When I go to the blob url from the browser I see the image =[

like image 377
Hodaya Shalom Avatar asked May 16 '19 08:05

Hodaya Shalom


People also ask

Can I send a file through Messenger?

You probably already know that you can share photos, videos, and GIFs in Facebook Messenger, but you can even send files, too, including PDFs. This method of sharing is handy if you don't have a recipient's email address readily available or if you're only connected with them through Facebook.

How can I send HTML file in Messenger?

To Insert text or HTML files into a message:Select the file and expand the Insert button. Select Insert as text. If the file contains HTML tags, it will render in the message as HTML.

Is there a Facebook Messenger API?

The Messenger Profile API allows you to set, update, retrieve, and delete properties from the Page Messenger Profile.


1 Answers

Per the SDK's section on sending attachments:

There are three ways to attach an asset to a message:

  • URL
  • File
  • attachment_id

The attachment_id refers to previously uploaded URL/File attachments. Sending raw file data is not an option. You must either upload the image to a URL or save it to a file. Blob URLs do not work because they refer only to data stored in the local system's memory. You need to move that data to an image or file on a server.

Upload the image to a URL

Your first option is to upload the image to a URL. Depending on how private the contents of the image are, you could use a public image hosting service like imgur, or you could upload the image to a public location on your server. If you want to keep the image hidden, you could save the image at a URL containing a randomly generated hash and delete that file as soon as the attachment has been uploaded to Messenger. However, you could keep the image totally private with the second option:

Upload the image from a (temp) file

Your second option is to upload the image according to a file location. By uploading the image to a file on your server, you could avoid the image ever becoming visible to the public. To avoid filling up server space, you could manually delete the file once the attachment has uploaded, or you could use a temp file. In fact, the SDK's example for sending a file demonstrates sending a temporary file saved in the /tmp folder.

like image 130
FThompson Avatar answered Oct 03 '22 06:10

FThompson