gpt4 book ai didi

c# - 使用 telegram.bot 发送照片

转载 作者:太空狗 更新时间:2023-10-30 00:50:06 64 4
gpt4 key购买 nike

我想在 Telegram 中创建一个机器人。经过搜索,我在一个Nuget包中找到了telegram.bot。

但是我无法发送照片。函数定义就像

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

但我不知道 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);

但这会导致异常

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

最佳答案

根据source code method documentation你应该传递“一个 file_id 作为字符串来重新发送 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,所以这些纯粹是检查源代码的推论。

关于c# - 使用 telegram.bot 发送照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33757887/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com