Search code examples
botstelegramcaptchapython-telegram-bot

Getting Runtime error while creating Telegram bot with Python


I am getting Runtime error while using pyTelegramBotCAPTCHA. I am using python-telegram-bot for this project.

My current code is this:

from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
from pyTelegramBotCAPTCHA import CaptchaManager

group_id = GROUP_ID

bot = (
    ApplicationBuilder().token("TOKEN").build()
)
job_queue = bot.job_queue

captcha_manager = CaptchaManager(bot.bot.id, default_timeout=90)
captcha_manager.initialize()


async def hello(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(
        f"Hi {update.effective_user.first_name}! Hope you have a good day, to the moon 🚀"
    )


async def callback_daily(context: ContextTypes.DEFAULT_TYPE):
    await context.bot.send_message(chat_id=group_id, text="One message every hour")


## * CAPTCHA START
# Message handler for new chat members
@bot.message_handler(content_types=["new_chat_members"])
def new_member(message):
    for new_user in message.new_chat_members:
        captcha_manager.restrict_chat_member(bot, message.chat.id, new_user.id)
        captcha_manager.send_new_captcha(bot, message.chat, new_user)


# Callback query handler
@bot.callback_query_handler(func=lambda callback: True)
def on_callback(callback):
    captcha_manager.update_captcha(bot, callback)


# Handler for correct solved CAPTCHAs
@captcha_manager.on_captcha_correct
def on_correct(captcha):
    bot.send_message(captcha.chat.id, "Congrats! You solved the CAPTCHA!")
    captcha_manager.unrestrict_chat_member(bot, captcha.chat.id, captcha.user.id)
    captcha_manager.delete_captcha(bot, captcha)


# Handler for wrong solved CAPTCHAs
@captcha_manager.on_captcha_not_correct
def on_not_correct(captcha):
    if captcha.incorrect_digits == 1 and captcha.previous_tries < 2:
        captcha_manager.refresh_captcha(bot, captcha)
    else:
        bot.kick_chat_member(captcha.chat.id, captcha.user.id)
        bot.send_message(
            captcha.chat.id,
            f"{captcha.user.first_name} failed solving the CAPTCHA and was banned!",
        )
        captcha_manager.delete_captcha(bot, captcha)


# Handler for timed out CAPTCHAS
@captcha_manager.on_captcha_timeout
def on_timeout(captcha):
    bot.kick_chat_member(captcha.chat.id, captcha.user.id)
    bot.send_message(
        captcha.chat.id,
        f"{captcha.user.first_name} did not solve the CAPTCHA and was banned!",
    )
    captcha_manager.delete_captcha(bot, captcha)


## * CAPTCHA END

bot.add_handler(CommandHandler("hello", hello))

job_daily = job_queue.run_repeating(
    callback_daily, interval=3600, first=0
)  ## 9 hours = 32400 secs

bot.run_polling()

I got this error:

Traceback (most recent call last):
  File "c:\Users\user\Documents\GitHub\bc-project\tg-bot.py", line 17, in <module>
    captcha_manager = CaptchaManager(bot.bot.id, default_timeout=90)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\_bot.py", line 669, in id
    return self.bot.id
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\_bot.py", line 658, in bot
    raise RuntimeError(
RuntimeError: ExtBot is not properly initialized. Call `ExtBot.initialize` before accessing this property.

I tried to initialize the bot with initialize() function also I tried to use initialize() function for captcha_manager but it didn't work. Thanks for helping.


Solution

  • Citing the readme of pyTelegramBotCAPTCHA

    An easy to use and (hopefully useful) image CAPTCHA soltion for pyTelegramBotAPI.

    This means that pyTelegramBotCAPTCHA is an extension for the pyTelegramBotAPI library and not for python-telegram-bot. All the examples in the readme are written with pyTelegramBotAPI. I can not judge if pyTelegramBotCAPTCHA is compatible with python-telegram-bot.

    In any case, I strongly recommand that you choose one python library for the communication with Telegram (either pyTelegramBotAPI xor python-telegram-bot) and stick with that.


    Disclaimer: I'm currently the maintainer of python-telegram-bot.