-
-
Save ilhamsabir/cae67d9f870a69bf8db40cdee387118a to your computer and use it in GitHub Desktop.
Facebook Messenger APIs: upload and send image to messenger with Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Would be updated | |
| */ | |
| "use strict" | |
| const request = require("request"); | |
| const fs = require("fs"); | |
| const image = fs.createReadStream("path/to/image.jpg ") | |
| //run from terminal /node app.js/ and get variables from command line | |
| const args = process.argv.slice(2); | |
| const token = args[0]; | |
| const fbEnd = "https://graph.facebook.com"; | |
| const endpoint = fbEnd + "/v2.6/me/messages?access_token=" + token; | |
| //make 'POST' request and create form (multipart/form-data) | |
| const r = request.post(endpoint, (err, httpResponse, body) => { | |
| if (err) {return console.error("upload failed: ", err)}; | |
| console.log("upload successfull. server respond: ", body) //FB always sends 'OK' status, | |
| //so to check errors, look at body.error (if any) | |
| }); | |
| const form = r.form(); | |
| form.append('recipient', JSON.stringify({id: "1351158114933843"})); | |
| form.append('text', 'look at image'); //sadly, but fb never shows text while uploading/sending image; bug?! | |
| form.append('message', JSON.stringify({attachment:{type:"image", payload:{is_reusable:true}}})); | |
| form.append('filedata', image); | |
| /* | |
| how to send multiple messages | |
| with node.js to fb-messenger | |
| (batch send) | |
| https://gist.github.com/yura321y/4e0d0bac2134f2b252538a39622f4124 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment