skip to Main Content

I’m implementing a bot that uses Facebook’s Send API. According to the documentation it is possible to send files using a request. The documentation offers two methods, one sending a URL to the file and the other uploading the file. I don’t want to upload the file and give it a URL as this is an open source library that doesn’t want to assume anything about the implementation.

I do want to upload the file directly. The documentation for uploading the file uses cURL for the example and looks as follows:

curl  
  -F recipient='{"id":"USER_ID"}' 
  -F message='{"attachment":{"type":"file", "payload":{}}}' 
  -F filedata=@/tmp/receipt.pdf 
  "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"    

My current take is that it should look something like this:

facebook_message.access_token = configuration.access_token;
var fileReaderStream = fs.createReadStream('./sampleData.json')
var formData = {
                "recipient": JSON.stringify({
                  "id":message.channel
               }),
               "attachment": JSON.stringify({
                  "type":"file", 
                  "payload":{}
               }),
               "filedata": fileReaderStream
               }

request({
         "method": 'POST',
         "json": true,
         "formData": formData,
         "uri": 'https://graph.facebook.com/v2.6/me/messages?access_token=' + configuration.access_token
        },
        function(err, res, body) {
               //***
        });

When I run this I receive the following response:

{ 
  message: '(#100) Must send either message or state',
  type: 'OAuthException',
  code: 100,
  error_subcode: 2018015,
  fbtrace_id: '***' 
 }

2

Answers


  1. The official example app includes a function for file uploads via URL: https://github.com/fbsamples/messenger-platform-samples/blob/master/node/app.js

    This is the relevant function:

    function sendFileMessage(recipientId) {
      var messageData = {
        recipient: {
          id: recipientId
        },
        message: {
          attachment: {
            type: "file",
            payload: {
              url: SERVER_URL + "/assets/test.txt"
            }
          }
        }
      };
    
      callSendAPI(messageData);
    }
    

    If you really want to do it the hard way, take a look at the request module docs: https://www.npmjs.com/package/request#forms
    Check out the example code with “formData” in the section. You are using “form”, which seems to be for regular data.

    Login or Signup to reply.
  2. The error you are receiving is because "attachment":{} needs to be inside an object called message. You must send either a message or sender_action object with facebook send API.

    var formData = {
                    "recipient": JSON.stringify({
                      "id":message.channel
                   }),
                   "message": JSON.stringify({
                   "attachment": {
                      "type":"file",
                      "payload":{}
                  }
              }),
                   "filedata": fileReaderStream
    
                   }
    

    Facebook should accept your api call after this, however I was unable to display a jpg file sent using your code. Perhaps it will work with your JSON file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search