telebot: как отправлять фото рандомно?
Как сделать чтобы при отправке команды бот отправлял рандомные фото из перечисленных?
PS Я новичок в программировании и мои навыки не очень позволяют создать эту команду самому.
попробовал такой код, не получается всё равно. ( фото расположены правильно по дериктории )
elif message.text == 'ran':
img_list = ['c://tgbot/1500x500.jpg', 'c://tgbot/23.jpg', 'c://tgbot/0R9gJA9IuyU.jpg']
img_path = random.choice(img_list)
bot.send_photo(message.chat.id, img_path)
пишет в консоли ошибки:
2020-12-26 12:23:43,629 (util.py:68 WorkerThread2) ERROR - TeleBot: "ApiException occurred, args=('A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:\n[b\'{"ok":false,"error_code":400,"description":"Bad Request: unsupported URL protocol"}\']',)
Traceback (most recent call last):
File "C:\Users\timur\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\util.py", line 62, in run
task(*args, **kwargs)
File "c://tgbot/bot.py", line 120, in get_text_messages
bot.send_photo(message.chat.id, img_path)
File "C:\Users\timur\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\__init__.py", line 699, in send_photo
apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup,
File "C:\Users\timur\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\apihelper.py", line 315, in send_photo
return _make_request(token, method_url, params=payload, files=files, method='post')
File "C:\Users\timur\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\apihelper.py", line 69, in _make_request
return _check_result(method_name, result)['result']
File "C:\Users\timur\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\apihelper.py", line 88, in _check_result
raise ApiException(msg, method_name, result)
telebot.apihelper.ApiException: A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400,"description":"Bad Request: unsupported URL protocol"}']
"
2020-12-26 12:23:43,630 (__init__.py:455 MainThread) ERROR - TeleBot: "A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400,"description":"Bad Request: unsupported URL protocol"}']"
Ответы (1 шт):
Автор решения: Jack_oS
→ Ссылка
Используйте встроенный модуль рандом: random.choice()
выбрать путь к изображению:
import random
img_list = ['path/to/img_1.jpg', 'path/to/img_2.jpg', 'path/to/img_3.jpg']
img_path = random.choice(img_list)
отправить:
bot.send_photo(message.chat.id, photo=open(img_path, 'rb'))