我想在电报里创建一个机器人。在搜索之后,我在一个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:错误:错误的请求:要发送的文件必须是非空的
发布于 2015-11-17 14:21:06
根据源代码方法文档,您应该传递“一个file_id作为字符串来重新发送已经在电报服务器上的照片,或者使用多部分/表单数据上传一张新照片”。我的猜测是,参数注释是通用的,这种重载只接受服务器上现有文件的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,所以这些都是从检查源代码中扣除的。
发布于 2017-09-01 07:42:27
你也可以尝试像这样的=>
Bot.SendPhotoAsync(Chatid , new FileToSend(FileName,Streaminput),Caption);
此api是默认的api,用于电报机器人上带有标题的发送照片。
发布于 2017-09-11 07:08:22
如果你需要的话,你也可以这样做。
Bot.SendPhoto(int channelId,photo : "http://abc.jpeg",caption:"hii");
在电报机器人上,你是发送图像,视频像2路。首先是您可以发送喜欢,转换您的图像在流,然后发送上,第二是我是给像刚通过照片:“这里给url作为字符串”,然后电报机器人服务器自动下载此图像从网址。
https://stackoverflow.com/questions/33757887
复制相似问题