我正在使用机器人向电报频道发送消息。
使用 webhook 方法。
我通过链接发送 file_id。我从频道帖子中获取了 file_id。
对于某些文件,如 GIF 和视频格式 (MP4),
当我使用此代码时:
$url = 'https://api.telegram.org/bot'.token.'/sendVideo?chat_id='.uid."&video=".$file."&caption="
.urlencode($caption);
file_get_contents($url);
我收到这样的错误:
{"ok":false,"error_code":400,"description":"Bad Request: wrong file identifier/HTTP URL specified"}
我真的不知道为什么我会得到这个, 这就像错误是随机的,因为我猜代码不依赖任何东西。
我使用从频道帖子中获得的 file_id。
该错误的原因是什么? 错误请求:指定了错误的文件标识符/HTTP URL
如文档中所述,造成这种情况的可能原因有很多:
你的Awnser在这里@farzad
通过file_id发送
file_id 对于每个机器人来说都是唯一的,不能从一个机器人转移到另一个机器人。
转到 @webpagebot 并向他发送该文件的 URL。电报的缓存将失效,这应该可以工作。看来是服务器上的错误。
就我而言,我无法上传图像(作为贴纸),
http://.../blabla.webp
不能通过电报应用程序,不能通过电报机器人API。
遇到同样的错误,发生的原因是:
Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
https://core.telegram.org/bots/api#sendvideo
尝试发布另一个具有文件大小的视频<= 50 MB.
如果从电报服务器上看不到您的网址或者您的网址不正确,则会出现此错误。
您也可以使用多部分 html post 方法将数据发送到此网址(请填写 {YourBotToken} 和 {your_channel_name_with_Atsign} 值):
<form action="https://api.telegram.org/bot{YourBotToken}/sendVideo" method="POST" enctype="application/x-www-form-urlencoded">
<input type="file" name="video" />
<input type="hidden" name="chat_id" value="{your_channel_name_with_Atsign}" />
<button type="submit" >send</button>
</form>
c# 示例代码为:
public bool sendVideo(string filename,string sendTo)
{
try
{
var botToken="{YourBotToken}";
var sendTo="{your_channel_name_with_Atsign}";
var filePath = "C:\\sample\\" + filename;
var sendTo, ="@YourChannelNameWithAtSign";
var bytesOfFile = System.IO.File.ReadAllBytes(filePath);
var url = $"https://api.telegram.org/bot{botToken}/sendVideo";
var res =Upload(url, sendTo, "video", bytesOfFile, filename).Result;
}
catch (Exception ex)
{
return false;
}
return true;
}
private static async Task<string> Upload(string actionUrl,string chat_id,string fileParamName, byte[] paramFileStream, string filename)
{
var formContent = new MultipartFormDataContent
{
{new StringContent(chat_id),"chat_id"},
{new StreamContent(new MemoryStream(paramFileStream)),fileParamName,filename}
};
var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(actionUrl.ToString(), formContent);
string stringContent = await response.Content.ReadAsStringAsync();
return stringContent;
}
这种方式不需要有网站,您可以从独立系统中使用它。
如果您将文件(照片、音频等)转发给机器人,您将获得该文件(对于您的机器人)的有效
file_id
。使用此 ID 发送文件应该是安全的,但似乎它不适用于某些文件(音频、视频……)! (可能是 Telegram API 错误)。
您可以下载文件并将其重新上传到您的机器人,以获得新的
file_id
,并且此 id 将起作用。
就我而言,发生此错误是因为我尝试发送图像两次(如果第一次不起作用,我会将大小减小到 5mb 以下并再次发送)。 显然,Telegram 缓存了先前的请求,因此当发送与第一个图像同名的另一张图像的新请求到达时,它只会返回先前的错误,而不会尝试发送它。
为了解决这个问题,我给调整大小的图像一个新名称(使用随机 + md5 来获取唯一的字符串)。
在sendDocument中,通过URL发送目前仅适用于GIF、PDF和ZIP文件。
https://core.telegram.org/bots/api#sending-files
如果您想发送视频,仅允许.MP4
另一种文件类型,我使用了 sendmessage (但文本内有链接)。
就我而言,由于某种原因,当图像 url 的扩展名全大写时出现错误。
这不起作用:
https://example.com/image.JPG
这有效:
https://example.com/image.jpg
您的哑剧类型视频不正确,请更改它