Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SendPhoto use MemoryStream is it possible? #9

Closed
Eatric opened this issue Oct 26, 2021 · 4 comments
Closed

SendPhoto use MemoryStream is it possible? #9

Eatric opened this issue Oct 26, 2021 · 4 comments

Comments

@Eatric
Copy link

Eatric commented Oct 26, 2021

When i trying to send photo with MemoryStream I get this:

Send Telegram photo {"ChatId":304667397, "Photo":{"Id":"79d1d33d-f54b-48a1-b337-b557622baad5", "Data":"System.IO.MemoryStream"}, "Method":"sendPhoto"} and get response {"Failure":{"Description":"Bad Request: file must be non-empty", "ErrorCode":400}, "Ok":false}.

Data is just use MemoryStream.ToString()?

Can you help me?

@Eatric
Copy link
Author

Eatric commented Oct 26, 2021

Working that, but looks so bad

var fileName = $"{Guid.NewGuid()}.png";
Logger.Info($"Starting create new photo file with name: {fileName}");

var fileStream = new FileStream(fileName, FileMode.CreateNew);
photo.WriteTo(fileStream);
fileStream.Close();
Logger.Info($"File with name {fileName} successfully created at {fileStream.Name}");

var fileNewStream = File.OpenRead(fileName);
var request = new SendPhotoFile(to, fileNewStream);

var response = BotClient.HandleAsync(request);
response.Wait();

Logger.Info("Send Telegram photo {@value1} and get response {@value2}.", request, response.Result);
File.Delete(fileName);
Logger.Info($"Deleted file with name: {fileName}");

@AmanAgnihotri
Copy link
Member

AmanAgnihotri commented Oct 27, 2021

Hello! Thank you for choosing this library for solving your use-cases. The issue you have is fairly straightforward to resolve.

Here's how you can use MemoryStream to send photos using the bot:

// Choosing to load some bytes from the file system but you can source this from elsewhere
byte[] data = await System.IO.File.ReadAllBytesAsync("/path/to/some/photo.jpg");

// Here we have the memory stream itself
MemoryStream stream = new(data);

// Sending a photo using a memory stream is as simple as
// passing the stream to the SendPhotoFile's constructor
SendPhotoFile request = new(123456789L, stream);

// Finally we await the response
Response<PhotoMessage> response = await bot.HandleAsync(request);

// Handle the response accordingly 

The problem sort of lies in the choice of naming, I guess, as it can be confusing to think of InputFile as anything other than an actual file. But if you look at the code, InputFile has a constructor which takes in a generic Stream value. It also has an implicit operator to convert any Stream to InputFile so you do not have to. This means that the file is just a logical concept and not an actual file in the file system. So, passing MemoryStream also works as shown in the above example.

I hope this helps! Feel free to ask any more questions that you may have while using this library.

@Eatric
Copy link
Author

Eatric commented Nov 2, 2021

I get the bytes of image from Selenium WebDriver so, there is no file name in data. Maybe that call the exception in code example?

@AmanAgnihotri
Copy link
Member

AmanAgnihotri commented Nov 3, 2021

As long as the bytes you get represent a proper image format (like JPEG or PNG), the lack of name is not an issue because InputFile class automatically generates a GUID as the identifier/name for the stream representing the image.

The code I showed as an example works. You are not supposed to call the ToString function on the stream as that will be problematic. Just pass the stream to the SendPhotoFile function along with the chat id and it should be good.

In the example you showed, you were successful in writing the stream you got to a file with png extension and then read the file and sent it to Telegram using the SendPhotoFile method. So, you should be able to pass the stream directly to the method and have it working without any issue.

Also, when I said "the problem lies with choice of naming", I was referring to my choice of calling it InputFile class and not anything to do with the name of some file itself. In case that was the thing that got misinterpreted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants