使用telegram.bot发送照片

Mr.*_*cal 4 c# telegram-bot

我想在电报中创建一个机器人.搜索之后,我在Nuget包中找到了telegram.bot.

但我发送照片时遇到了麻烦.功能定义就像

Bot.SendPhoto(int channelId, string photo, string caption)
Run Code Online (Sandbox Code Playgroud)

但我不知道string photo参数中的预期结果.我应该将图像转换为base64字符串,还是传递图像路径,还是......?

我的代码目前看起来像这样

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);
Run Code Online (Sandbox Code Playgroud)

但这导致了一个例外

Telegram.Bot.Types.ApiRequestException:[错误]:错误请求:要发送的文件必须为非空

PHe*_*erg 5

根据源代码方法文档,您应该传递"一个file_id as 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)
Run Code Online (Sandbox Code Playgroud)

超载

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

接受FileToSend包含文件名和流的a.使用第二次重载来上传新照片.

免责声明:我没有使用过API,所以这些只是检查源代码的简单推论.