私はネコ画像を送信できるFacebookチャットボットを作ろうとしています。私はRESTful APIを使って猫の写真を撮る。それらは生のpngとして返されます。次の最後のステップは、その画像を読み取り可能なストリームに変換して、Facebook Chat APIがそれを添付ファイルとして送信できるようにすることです。PNGをGETリクエストからNode.jsの読み込み可能なストリームに変換する
私は画像をつかむためにrequest.js
を使用します。リクエストのドキュメントには、イメージをファイルとして保存し、ファイルをstream.Readable
に保存することだけが記載されています。その一時ファイルをバイパスし、画像をFacebookチャット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 }
https://github.com/maxogden/mississippi#fromとhttps://github.com/yoshuawuyts/from2-stringを参照してください。 –
あなたが読んでいるストリームにプッシュするのは、あなたがコールバックを使って作業しています。あなたは 'request( 'http://google.com/doodle.png').pipe(fs.createWriteStream( 'doodle.png'))のようにパイプする必要があります。 – yeya