尝试将目录的所有图片发送给Telegram bot用户

时间:2019-04-28 14:52:22

标签: python telegram telegram-bot telepot

我正在尝试将文件夹的所有图片发送给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')),它可以正常工作,但是多次发送相同的图片。

1 个答案:

答案 0 :(得分:0)

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

  • sendPhoto替换为send_photo
  • 要发送文件,您需要open

赞:

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

现在您可以看到:

works

它有效,并且仅发送.jpg文件。 ({RickSanchezpng)。