Hi. I have a doubt on sendPhoto method.
I have to send an image got by plotly's getImage() method and its callback has as parameter a "Stream of base64 encoded imageData". If I don't use fs.createWriteStream() like in
const filestream = fs.createWriteStream(path);
imageStream.pipe(fileStream);
console.log(fileStream); // returns a object with path property
bot.sendPhoto(toUser, fileStream);
I'm not able to get an object with a valid path property, so the wrapper gives me error (Cannot read property 'toString' of undefined on telegram.js:198:51 - _formatSendData). If I do that and save it in an image file, I get a delayed_stream.js error (source.pause is not a function). Can somebody help me to understand how can I send the image, also (if possible) without saving it? Thank you.
The current implementation depends on the property path on a stream to determine the name of the file being sent. It assumes the property is always available, which now seems like an inconsiderate assumption.
A possible fix for this would be to fall back to a generic name, i.e. filename. This can be done quite easily.
However, this raises the question of whether we should allow the user to specify details of the file, in place of fall-backs or errors. For example, in cases where we can not detect the file-type of the contents of a buffer, we throw an error! :astonished: A better solution would be to allow the user to explicitly provide such details, in addition to fall-backs and errors.
The error source.pause is not a function seems to related to request module not handling streams properly. See an almost-related issue: https://github.com/request/request/issues/1402. We will have to spend some time looking through request's code and figure out the issue!
To reproduce:
const stream = require('http').get('http://example.com/photo.jpg'); // use a proper URL here!
bot.sendPhoto(userId, stream);
Well... good to know that I found a bug... or two. 馃槀
Coming back to the problem, I was asking for a solution and someone who would have tried to explain me the streams... I still don't know how to send a Stream via sendPhoto 馃槄. Anyway, thank you for the replies.
any update on this?
I've also encountered the same problem when trying to pass a stream from ytdl-core to sendAudio method. since it doesn't have the path property it throws an exception. both falling back to filename or asking for an explicit filename are acceptable solutions.
code:
bot.sendAudio(msg.chat.id, ytdl.downloadFromInfo(info, {quality: 140}))
stack:
Unhandled rejection TypeError: Cannot read property 'toString' of undefined
at TelegramBot._formatSendData (...\node_modules\node-telegram-bot-api\src\telegram.js:271:51)
at TelegramBot.sendAudio (...\node_modules\node-telegram-bot-api\src\telegram.js:685:29)
...
This has been fixed in v0.30.0.
Using the code below, I still getting the error. Wasn't that fixed?
const stream = require('http').get('http://example.com/photo.jpg'); // use a proper URL here!
bot.sendPhoto(userId, stream);
source.pause is not a function
Most helpful comment
The current implementation depends on the property
pathon a stream to determine the name of the file being sent. It assumes the property is always available, which now seems like an inconsiderate assumption.A possible fix for this would be to fall back to a generic name, i.e. filename. This can be done quite easily.
However, this raises the question of whether we should allow the user to specify details of the file, in place of fall-backs or errors. For example, in cases where we can not detect the file-type of the contents of a buffer, we throw an error! :astonished: A better solution would be to allow the user to explicitly provide such details, in addition to fall-backs and errors.