2

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

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'))了,它有效,但它多次发送相同的图片。

4

1 回答 1

0

问题在于这一行: 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)。

于 2019-04-29T08:36:04.963 回答