我在Telegram bot 中通过 url发送照片。对于某些照片,我收到来自 Telegram 的错误消息:
{"ok":false,"error_code":400,"description":"Bad Request: wrong file identifier/HTTP URL specified"}
Run Code Online (Sandbox Code Playgroud)
例如:发送这张照片作品:
https://api.telegram.org/bot<BOT_KEY>/sendPhoto?chat_id=<CHAT_ID>&photo=https%3A%2F%2Fdrscdn.500px.org%2Fphoto%2F153590277%2Fq%253D80_m%253D2000%2Fv2%3Fwebp%3Dtrue%26sig%3D8b429a27872dfdb4f68ddc5edd488ce9e6a57977415fa323178cd62c5100a3ff
Run Code Online (Sandbox Code Playgroud)
但这个文件奇怪地不起作用:
https://api.telegram.org/bot<BOT_KEY>/sendPhoto?chat_id=<CHAT_ID>&photo=https%3A%2F%2Fdrscdn.500px.org%2Fphoto%2F247611167%2Fq%253D80_m%253D1500%2Fv2%3Fwebp%3Dtrue%26sig%3Dcfa117f225962250323c1202797abe8d45b47d59da12d780f4bf5231687c4331
Run Code Online (Sandbox Code Playgroud)
请注意,对于这两个示例:
难道我做错了什么?或者对这个问题有什么想法?
谢谢,
可能的解决方案:
将具有随机值的无用属性添加到文件 url 并重试,直到成功。
我也遇到了这个问题。我刚刚发现具有几乎相同 url 的几乎相同的文件具有不同的行为。它让我震惊。我不认为这是我的文件的错,所以我通过添加一个无用的属性并调整其值来修改文件 url。重试了很多次,成功了!
您可以通过运行此代码段来尝试一下。唯一的区别是&random=58和&random=64在每个文件 url 的末尾。
<h5>Your Bot Token</h5>
<input id="token" type="text" style="width: 400px;" value="{YourBotToken}" />
<h3>200 OK Example</h3>
<form action="https://api.telegram.org/bot{YourBotToken}/sendPhoto" method="POST" enctype="application/x-www-form-urlencoded">
<textarea type="text/html" name="photo" rows="4" cols="70" readonly="readonly">http://api.map.baidu.com/staticimage?center=140.50,36.15&width=1024&height=576&zoom=6&copyright=1&markers=140.50,36.15&markerStyles=l&random=58</textarea>
<input type="text" name="chat_id" value="@{YourChatID}" />
<input type="submit" value="Submit" onclick="this.form.action='https://api.telegram.org/bot'+document.getElementById('token').value+'/sendPhoto';" />
</form>
<br><br>
<h3>400 Bad Request Example</h3>
<form action="https://api.telegram.org/bot{YourBotToken}/sendPhoto" method="POST" enctype="application/x-www-form-urlencoded">
<textarea type="text/html" name="photo" rows="4" cols="70" readonly="readonly">http://api.map.baidu.com/staticimage?center=140.50,36.15&width=1024&height=576&zoom=6&copyright=1&markers=140.50,36.15&markerStyles=l&random=64</textarea>
<input type="text" name="chat_id" value="@{YourChatID}" />
<input type="submit" value="Submit" onclick="this.form.action='https://api.telegram.org/bot'+document.getElementById('token').value+'/sendPhoto';" />
</form>Run Code Online (Sandbox Code Playgroud)