首页 > 解决方案 > 试图将目录的所有图片发送给 Telegram bot 用户

问题描述

我正在尝试将文件夹的所有图片发送给机器人用户。这是我尝试过的,但它不起作用,甚至没有出现任何错误。

path = '~/Documents/mypath/pics'

files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if '.jpg' in file:
            files.append(os.path.join(r, file))
for f in files:
        telegram_bot.sendPhoto (chat_id, f)

这里有什么问题以及如何解决?

更新:我试过telegram_bot.sendPhoto(chat_id, open(f , 'rb'))了,它有效,但它多次发送相同的图片。

标签: pythontelegramtelegram-bottelepot

解决方案


问题在于这一行: telegram_bot.sendPhoto (chat_id, f)

  • 替换sendPhotosend_photo
  • open当您要发送文件时,您需要该文件。

像这样:

telegram_bot.send_photo(chat_id=update.message.chat.id, photo=open(f, 'rb'))

现在你可以看到:

作品

它工作,它只发送.jpg文件。(RickSanchezpng)。


推荐阅读