I'm trying to make a Facebook chat bot that can send cat pictures. I use a RESTful API to get the cat pictures. They are returned as raw png. The next and final step is to convert that image into a Readable Stream so the Facebook Chat API can send it as an attachment.
I use request.js
to grab the image. Request's documentation only mentions saving images as a file and reading files into stream.Readable
. I wonder if there's a way to bypass that temporary file, and pipe the image directly into Facebook Chat API.
Here's my code so far:
var request = require("request");
var stream = require("stream");
module.exports = function getCatPicture(api, threadID, body) {
var options = {
url: 'http://thecatapi.com/api/images/get?type=png',
encoding: 'base64'
}
var picStream = new stream.Readable;
request.get(options, function (error, response, body) {
picStream.push(body, 'base64');
var catPic = {
attachment: picStream
};
api.sendMessage(catPic, threadID);
return;
});
}
I'm getting an error:
Error in uploadAttachment Error: form-data: not implemented
Error in uploadAttachment at Readable._read (_stream_readable.js:457:22)
Error in uploadAttachment at Readable.read (_stream_readable.js:336:10)
Error in uploadAttachment at flow (_stream_readable.js:751:26)
Error in uploadAttachment at resume_ (_stream_readable.js:731:3)
Error in uploadAttachment at nextTickCallbackWith2Args (node.js:442:9)
Error in uploadAttachment at process._tickCallback (node.js:356:17)
Error in uploadAttachment { [Error: form-data: not implemented]
Error in uploadAttachment cause: [Error: form-data: not implemented],
Error in uploadAttachment isOperational: true }