U
U
usiting2021-11-24 15:41:44
Python
usiting, 2021-11-24 15:41:44

QRcode output by TelegramBotAPI image?

I am writing a simple pet project and ran into a problem, qrcode is displayed not as a picture, but as a message, the source code and a screenshot of the result are below

import telebot
from telebot import types
import qrcode

bot = telebot.TeleBot('TOKEN')

@bot.message_handler(commands=['старт'])
def starting_bot(message):
    bot.send_message(message.chat.id, 'Привет, я бот, который создаёт QRкоды \n\n/помощь для просмотра умений бота')

@bot.message_handler(commands=['помощь'])
def helping_user(message):
    bot.send_message(message.chat.id, 'Чтобы создать QRкод напиши мне /создать')

@bot.message_handler(commands=['создать'])
def creating_qr(message):
    bot.send_message(message.chat.id, 'Пришли мне текст, который нужно преобразовать в QRcod')
    @bot.message_handler(content_types=['text'])
    def creating_qr(message):
        for_qr = message.text
        img_create_qr = qrcode.make(for_qr)
        bot.send_message(message.chat.id, f'Твой QRкод: \n\n{img_create_qr}')
        
bot.polling(none_stop = True)


qr code is displayed like this:
619e31a098479184996081.jpeg

I tried to rewrite the code using the pillow library
@bot.message_handler(commands=['создать'])
def creating_qr(message):
    bot.send_message(message.chat.id, 'Пришли мне текст, который нужно преобразовать в QRcod')
    @bot.message_handler(content_types=['text'])
    def creating_qr(message):
        for_qr = message.text
        img_create_qr = qrcode.make(for_qr)
        img_create_qr.save('qr1.jpg','JPEG')
        bot.send_photo('qr1.jpg')


when using bot.send_photo, the terminal gives an error:
619e32baa9d32005165005.jpeg

Please help me solve this problem, I will be very grateful to you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-11-24
@usiting

You can send without saving

from io import BytesIO

qr_img = qrcode.make('Starting!')
bio = BytesIO()
qr_img.save(bio, 'JPEG')
bio.seek(0)
bot.send_photo(message.chat.id, bio)

U
usiting, 2021-11-24
@usiting

Who is interested in the full code of the project, please: (PS the answer to my question was suggested above)

import telebot
from telebot import types
import qrcode
from io import BytesIO

bot = telebot.TeleBot('TOKEN')

@bot.message_handler(commands=['старт'])
def starting_bot(message):
    bot.send_message(message.chat.id, 'Привет, я бот, который создаёт QRкоды \n\n/помощь для просмотра умений бота')

@bot.message_handler(commands=['помощь'])
def helping_user(message):
    bot.send_message(message.chat.id, 'Чтобы создать QRкод напиши мне /создать')

@bot.message_handler(commands=['создать'])
def creating_qr(message):
    bot.send_message(message.chat.id, 'Пришли мне текст, который нужно преобразовать в QRcod')
    @bot.message_handler(content_types=['text'])
    def creating_qr(message):
        qr_img = qrcode.make(message.text)
        bio = BytesIO()
        qr_img.save(bio, 'JPEG')
        bio.seek(0)
        bot.send_photo(message.chat.id, bio)


bot.polling(none_stop = True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question