Search code examples
pythonapiqr-codepy-telegram-bot-api

How to decrypt QRcode in TelegramBotAPI


I am creating a telegram bots that can generate and decrypt QRcodes. I implemented the creation of a QR code using BytesIO:

#bot creating a qrcode
@bot.message_handler(commands=['create'])
def creating_qr(message):
    bot.send_message(message.chat.id, 'Enter text and ill generate QRcode')
    @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)

I tried to decrypt qrcode using the same method, but the ApiTelegramBot documentation says photo is a list so I can't save it in a BytesIO. How can I save photo and then decrypt it?

Method that I tried:

def decrypting(message):
    detector = cv2.QRCodeDetector()
    qr_image = message.photo
    bio = BytesIO()
    qr_image.save(bio, 'JPEG')
    bio.seek(0)
    data = detector.detectAndDecode(bio)
    bot.send_message(message.chat.id, f'QR code data: \n\n{data}')

Solution

  • I haven't used the API myself, but Pyhton and Telegram :) A message can have multiple photos. So I guess you get a list of photos. In order to select the first photo, change your code like so:

    qr_image = message.photo[0]