send_photo方法不允许从本地api发送图片给用户
使用send_photo()方法时出现这样的问题,也许有人知道一个简洁的解决方案。
有一个本地 api 存储新闻,它有一个包含图像 url 的字段。
使用 get 请求获取此数据没有任何问题,但将图片的 url 传递给 send_photo 方法时我的机器人会挂起。
我的结论是send_photo使用telegram服务器通过url搜索图片,因此无法从互联网上的本地api找到图片。
如何正确纠正这种情况?
有一个想法是从 API 下载图片到一个单独的文件夹,然后将它们从那里传输到 send_photo 方法,然后从存储中删除它们,但在我看来,有一个更简单的解决方案。
另外,我还是不知道如何在python中通过url下载图片。此方法也可能出现此问题。
async def get_news(message : types.Message):
try:
if message.text.lower() == 'news':
r = requests.get("http://127.0.0.1:8000/api/news")
data = r.json()
storageURL = "http://127.0.0.1:8000/storage/"
photoURL = ""
i = 0
while i < len(data):
photoURL = storageURL + data[i]["preview"]
await bot.send_photo(
chat_id=message.chat.id,
photo=photoURL,
caption=data[i]["title"]
)
i += 1
except Exception as ex:
print(ex)
There was such a problem when using the send_photo() method, maybe someone knows a concise solution.
There is a local api that stores the news, it has a field with the url of the image.
This data is obtained without any problems using the get request, but passing the url of the picture to the send_photo method my bot hangs.
I concluded that send_photo searches for pictures by url using the telegram server and therefore cannot find pictures from the local api on the Internet.
How can this be properly corrected?
There was an idea to download pictures from the API to a separate folder and transfer them from there to the send_photo method, and then delete them from the storage, but it seems to me that there is a simpler solution.
In addition, I still do not know how to download images by url in python. This problem can also arise with this approach.
async def get_news(message : types.Message):
try:
if message.text.lower() == 'news':
r = requests.get("http://127.0.0.1:8000/api/news")
data = r.json()
storageURL = "http://127.0.0.1:8000/storage/"
photoURL = ""
i = 0
while i < len(data):
photoURL = storageURL + data[i]["preview"]
await bot.send_photo(
chat_id=message.chat.id,
photo=photoURL,
caption=data[i]["title"]
)
i += 1
except Exception as ex:
print(ex)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
毕竟,我通过下载和删除图片解决了问题。
也许我的方式不是美丽,但它起作用。
我希望这对某人有用。
I solved the problem after all by downloading and deleting pictures.
Maybe my way is not beautiful, but it works.
I hope it will be useful to someone.