commit 404d9fc820f2db8f1ace04f4bdad35fef60a5c39 Author: Ilia Miheev Date: Sat Dec 28 13:52:00 2024 +0300 The bot is capable of generating QR codes from text diff --git a/bot_qr.py b/bot_qr.py new file mode 100644 index 0000000..8113c48 --- /dev/null +++ b/bot_qr.py @@ -0,0 +1,49 @@ +from dotenv import load_dotenv +from os import remove, getenv +import telebot +import qrcode + + +# Загрузить переменные из файла .env +load_dotenv() +# Получить значение переменной +API_TOKEN = getenv("API_TOKEN") + + +bot = telebot.TeleBot(API_TOKEN) + + +@bot.message_handler(commands=['start']) +def start(mess): + bot.send_message(mess.chat.id, 'Привет. Я генерирую QR коды. Отправь мне текст и я его преобразую') + + +def gen_qr(text): + # Создаем экземпляр класса QRCode + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_H, + box_size=10, + border=1, + ) + # Добавляем данные в QR-код + qr.add_data(text) + qr.make(fit=True) + # Создаем изображение QR-кода + img = qr.make_image(fill_color="black", back_color="white") + # Сохраняем изображение QR-кода + img.save("qrcode.png") + + +@bot.message_handler(content_types=['text']) +def generate_qr_code(mess): + gen_qr(mess.text) + text = f'Сообщение: {mess.text}\nСлов в сообщении: {len(mess.text.split())}\nСимволов в сообщении: {len(mess.text)}' + with open('qrcode.png', 'rb') as qr_photo: + bot.send_photo(mess.chat.id, qr_photo) + bot.send_message(mess.chat.id, text, parse_mode='html') + remove('qrcode.png') + +print('Бот запущен') +bot.polling() +print('Бот отключён') \ No newline at end of file