我正在尝试制作一个可以发送猫图片的 Facebook 聊天机器人。我使用RESTful API来获取猫的图片。它们以原始 png 的形式返回。下一步也是最后一步是将该图像转换为可读流,以便Facebook 聊天 API可以将其作为附件发送。
我request.js
用来抓取图像。Request 的文档仅提到将图像保存为文件并将文件读入stream.Readable
. 我想知道是否有办法绕过该临时文件,并将图像直接传送到 Facebook Chat API。
到目前为止,这是我的代码:
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;
});
}
我收到一个错误:
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 }