node.js - How to use facebook messenger send image API in Node js? -
i'm building facebook bot in nodejs facebook messenger api. i'm trying send image bot directly uploading image file heroku server (not through url) , not work.
here error log console.
failed calling send api 400 bad request { message: '(#100) incorrect number of files uploaded. must upload 1 file.',type: 'oauthexception', code: 100,error_subcode: 2018005,fbtrace_id: 'e32ogm/ofxd' }
the official facebook document contains example in curl format , i'dont know how replicate curl node format.
i've tested curl , worked charm.
curl \ -f 'recipient={"id":"recipientid"}' \ -f 'message={"attachment":{"type":"image", "payload":{}}}' \ -f 'filedata=@resource/pdf_img/sample.jpg;type=image/jpeg' \ "https://graph.facebook.com/v2.6/me/messages?access_token=page_access_token"
this node implementation seems problematic,
//file_loc = __dirname+"/resource/pdf_img/sample.jpg" function sendimagemessage(recipientid, file_loc){ let fs = require('fs'); var readstream = fs.createreadstream(file_loc); var messagedata = { recipient : { id : recipientid }, message : { attachment : { type : "image", payload :{} } }, filedata:readstream } callsendapi(messagedata); } function callsendapi(messagedata) { request({ uri: "https://graph.facebook.com/v2.6/me/messages", qs: {access_token: process.env.page_access_token}, method: "post", json: messagedata }, function(error, response, body) { if (!error && response.statuscode == 200) { var recipientid = body.recipient_id; var messageid = body.message_id; if (messageid) { console.log("successfully sent message id %s recipient %s", messageid, recipientid); } else { console.log("successfully called send api recipient %s", recipientid); } } else { console.error("failed calling send api", response.statuscode, response.statusmessage, body.error); } }); }
please fixing node implementation or translating curl in node appreciated.
Comments
Post a Comment