Answer the question
In order to leave comments, you need to log in
How to send a random photo from a folder in a project, Telegram bot - python?
Greetings. How to implement the function
Let's say there is a /photo command, when you click on the command, a button is sent to the user - the text on the button for example: get a photo
When you click on the button, a random picture is sent to the user from a folder in the pycharm ce project
I would be grateful for any help. Thank you . telegrambotapi
Answer the question
In order to leave comments, you need to log in
import telebot
from telebot import types
import os
import random
bot = telebot.TeleBot("token")
@bot.message_handler(content_types=['text'])
def start(message):
if message.text.lower() == '/photo':
kbPhoto = telebot.types.ReplyKeyboardMarkup()
kbPhoto.row('Получить фото')
kbPhoto.row('Выйти')
bot.send_message(message.from_user.id, 'Нажмите на кнопку ниже, чтоб получить фотографию.', reply_markup = kbPhoto)
if message.text.lower() == 'получить фото':
photo = open('test/' + random.choice(os.listdir('test')), 'rb')
bot.send_photo(message.from_user.id, photo, caption = 'Лови')
if message.text.lower() == 'выйти':
bot.send_message(message.from_user.id, 'Клавиатура убрана.', reply_markup = types.ReplyKeyboardRemove())
bot.polling( none_stop = True, interval=0 )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question