Giter Club home page Giter Club logo

Comments (4)

Eatric avatar Eatric commented on June 20, 2024

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}");

from telegram.bots.

AmanAgnihotri avatar AmanAgnihotri commented on June 20, 2024

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.

from telegram.bots.

Eatric avatar Eatric commented on June 20, 2024

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?

from telegram.bots.

AmanAgnihotri avatar AmanAgnihotri commented on June 20, 2024

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.

from telegram.bots.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.