我正在使用 python 和 Aiogram 编写一个电报机器人。 我无法在聊天中发送本地照片,但如果我链接来自谷歌的照片就可以了。这是为什么?
script_directory = os.path.dirname(os.path.abspath(__file__))
percorso_img = script_directory + '\\images\\volume1.jpg'
print(percorso_img)
await message.answer_photo('https://m.media-amazon.com/images/I/71Rpwr00gpS._AC_UF1000,1000_QL80_.jpg')
await message.answer_photo(percorso_img)
这是我的代码,第一张照片是在机器人上发送的,因为它是来自互联网的链接。 第二个是本地照片的路径,但不是。终端上打印的路径是正确的,单击它即可打开照片。
这是我得到的错误
TelegramBadRequest: Telegram server says - Bad Request: invalid file HTTP URL specified: Wrong port number specified in the URL
我尝试过其他方法,例如
script_directory = os.path.dirname(os.path.abspath(__file__))
percorso_immagine = os.path.join(script_directory, 'images', 'volume1.jpg')
with open(percorso_immagine, 'rb') as immagine_file:
input_file = types.InputFile(immagine_file)
await message.answer_photo(input_file)
我还收到其他错误
Errore nell'invio dell'immagine: Can't instantiate abstract class InputFile with abstract method read
也尝试过
photo=open(percorso_img, "rb")
await bot.send_photo(message.from_user.id, photo)
但是
ValidationError: 2 validation errors for SendPhoto
photo.is-instance[InputFile]
Input should be an instance of InputFile [type=is_instance_of, input_value=<_io.BufferedReader name=...m\\images\\volume1.jpg'>, input_type=BufferedReader]
For further information visit https://errors.pydantic.dev/2.3/v/is_instance_of
photo.str
Input should be a valid string [type=string_type, input_value=<_io.BufferedReader name=...m\\images\\volume1.jpg'>, input_type=BufferedReader]
For further information visit https://errors.pydantic.dev/2.3/v/string_type`
本地照片尝试用这种方式:
await message.answer_photo(
types.InputFile(path_to_photo), caption="Some kind of caption"
)
这是来自互联网的照片:
await call.message.answer_photo(
photo=URLInputFile(
url=photo_url,
filename=f"picture.png"
), caption="Some kind of caption"
)
我在aiogram版本3和Python 3.11作品上测试了这些方法的性能