Shadow1349
Shadow1349

Reputation: 771

Google Cloud Storage upload file to Facebook Attachment API

I have an app that I am trying to create a Facebook bot for and it needs to send photo cards to users. However, to do so I need to get a Facebook attachment_id because I can't upload my files directly from the web. (https://developers.facebook.com/docs/messenger-platform/reference/attachment-upload-api/)

My app is on Firebase, for some reason when I try the URL it gives me this error:

{ "error": { "message": "(#546) The type of file you're trying to attach isn't allowed. Please try again with a different format.", "type": "OAuthException", "code": 546, "error_subcode": 1545026, "fbtrace_id": "H8qfi28cNvp" } }

That happens with any URL I try but here is a test https://memes-dev.mymemestore.com/mms-tp1pESD7hgWWLiOUziHz.jpg

So I try to use the Google Cloud Storage nodejs api to get a readstream and send that like so

const stream = gcs().bucket('my bucket').file('mms-tp1pESD7hgWWLiOUziHz.jpg').createReadStream();

const message = new formdata();
message.append('message', '{"attachment":{"type":"image", "payload":{"is_reusable":true}}}');
message.append('filedata', stream);

fetch('https://graph.facebook.com/v2.6/me/message_attachments?access_token=<PAGE ACCESS TOKEN>',
{
    method: 'POST',
    headers: message.getHeaders(),
    body: message
})
.then(res => {
    console.log(res);
    return res.json();
})
.then(json => {
    console.log(json);
});

but that gives me the following error

(#100) Incorrect number of files uploaded. Must upload exactly one file.

Does anyone know what is going on?

Thanks

Upvotes: 0

Views: 308

Answers (1)

Shadow1349
Shadow1349

Reputation: 771

One thing I forgot to mention was that this all had to be done serverless. But I got around it by downloading the image to a tmpdir using tmpdir = path.join(os.tmpdir(), filename) and then fs.createReadStream(tmpdir) and that is working.

Upvotes: 1

Related Questions