Send a photo using telegram.bot

I want to create a robot in Telegram. After searching, I found telegram.bot in the Nuget package.

But I have problems sending photos. Function definition is similar to

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

But I do not know what is expected in the parameter string photo. Should I convert my image to base64 string or pass image path or ...?

Currently my code is as follows

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 throws an exception

Telegram.Bot.Types.ApiRequestException: [Error]: Invalid request: send file must be non-empty

+4
source share
3 answers

"file_id String , Telegram, multipart/form-data". , file_id .

/// <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)

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

FileToSend, . .

: API, .

+5

: = >

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

api api .

+1

, .

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

on the bot telegram you send the image, video as the 2nd way. first you can send how to convert your image to a stream, and then send it further, and the second is to give me just a photo with a photo: "here give url as string", then the bot’s telegram will automatically download this image from the URL.

0
source

Source: https://habr.com/ru/post/1616223/


All Articles