Skip to content

Instantly share code, notes, and snippets.

@jt0in3e
Last active June 13, 2021 00:32
Show Gist options
  • Save jt0in3e/9af845522b4fea75c092f89c113f207d to your computer and use it in GitHub Desktop.
Save jt0in3e/9af845522b4fea75c092f89c113f207d to your computer and use it in GitHub Desktop.
Facebook Messenger APIs: upload and send image to messenger with Node.js
/*
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
*/
@ayoubomari
Copy link

I have this error:
server respond: {"error":{"message":"(#100) No matching user found","type":"OAuthException","code":100,"error_subcode":2018001,"fbtrace_id":"AokDv5j1pfa0tk1dxiA7Z92"}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment