-
Notifications
You must be signed in to change notification settings - Fork 10
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
Comments
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}"); |
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 // 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 I hope this helps! Feel free to ask any more questions that you may have while using this library. |
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? |
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 The code I showed as an example works. You are not supposed to call the 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 Also, when I said "the problem lies with choice of naming", I was referring to my choice of calling it |
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?
The text was updated successfully, but these errors were encountered: