Mr.Logical
Mr.Logical

Reputation: 51

Send photo using telegram.bot

I want to create a Robot in Telegram. After searching, I found telegram.bot in a Nuget Package.

But I'm having trouble sending a photo. The function definition is like

Bot.SendPhoto(int channelId, string photo, string caption)

But I don't know what is expected in the string photo parameter. Should I convert my image to a base64 string, or pass an image path, or ...?

My code currently looks like this

var Bot = new Telegram.Bot.Api("API KEY");
var b = new System.Net.WebClient().DownloadData(a.DefaultImage());
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(new System.IO.MemoryStream(b));
var z = bmp.GetThumbnailImage(200, (200 * bmp.Height) / bmp.Width, 
   new System.Drawing.Image.GetThumbnailImageAbort(
      delegate { return true; }), IntPtr.Zero);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
z.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
var x = new Telegram.Bot.Types.FileToSend() 
{ 
    Filename = a.DefaultImage().Split('/').LastOrDefault(), Content = ms 
};

var t = Bot.SendPhoto("@Chanel", x, a.Title);

But this is causing an exception

Telegram.Bot.Types.ApiRequestException: [Error]: Bad Request: File to send must be non-empty

Upvotes: 5

Views: 15634

Answers (4)

Viacheslav Brovko
Viacheslav Brovko

Reputation: 33

Had the same exception when I tried to send a photo from a stream. And yes, the problem was that Position was not on 0. So the next line has solved my problem.

stream.Position = 0;

Upvotes: 1

Edit
Edit

Reputation: 393

you can also try like this if you need then.

Bot.SendPhoto(int channelId,photo : "http://abc.jpeg",caption:"hii");

on telegram bot you are send image, videos like 2 way. first is you can send like convert your image in to stream and then send on and 2nd is i am give like just pass photo:"here give url as string" then telegram bot server automatically download this image from url.

Upvotes: 0

Edit
Edit

Reputation: 393

You can also try like this =>

Bot.SendPhotoAsync(Chatid ,  new FileToSend(FileName,Streaminput),Caption);

This api is default api for the send photo with caption on telegram bot.

Upvotes: 1

PHeiberg
PHeiberg

Reputation: 29811

According to the source code method documentation you should pass "a file_id as String to resend a photo that is already on the Telegram servers, or upload a new photo using multipart/form-data". My guess is that the parameter comment is generic and that this overload only accepts the file_id of an existing file on the server.

/// <summary>
/// Use this method to send photos. On success, the sent Message is returned.
/// </summary>
/// <param name="chatId">Unique identifier for the target chat</param>
/// <param name="photo">Photo to send. You can either pass a file_id as String to resend a photo that is already on the Telegram servers, or upload a new photo using multipart/form-data.</param>
/// <param name="caption">Optional. Photo caption (may also be used when resending photos by file_id).</param>
/// <param name="replyToMessageId">Optional. If the message is a reply, ID of the original message</param>
/// <param name="replyMarkup">Optional. Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</param>
/// <returns>On success, the sent Message is returned.</returns>
public async Task<Message> SendPhoto(int chatId, string photo, string caption = "", int replyToMessageId = 0, ReplyMarkup replyMarkup = null)

The overload

public async Task<Message> SendPhoto(int chatId, FileToSend photo, 
    string caption = "", int replyToMessageId = 0,
    ReplyMarkup replyMarkup = null)

accepts a FileToSend that holds a file name and stream. Use that second overload for uploading new photos.

Disclaimer: I haven't used the API, so these are purely deductions from checking the source code.

Upvotes: 5

Related Questions