我在使用 pyTelegramBotCAPTCHA 时遇到运行时错误。我在这个项目中使用 python-telegram-bot。
我当前的代码是这样的:
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()
我收到此错误:
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.
我尝试使用initialize()函数初始化机器人,我也尝试使用captcha_manager的initialize()函数,但它不起作用。谢谢你的帮助。
适用于 pyTelegramBotAPI 的易于使用且(希望有用)的图像验证码解决方案。
这意味着
pyTelegramBotCAPTCHA
是 pyTelegramBotAPI
库的扩展,而不是 python-telegram-bot
的扩展。自述文件中的所有示例均以 pyTelegramBotAPI
书写。我无法判断pyTelegramBotCAPTCHA
是否与python-telegram-bot
兼容。
无论如何,我强烈建议您选择 one python 库来与 Telegram 进行通信(
pyTelegramBotAPI
x 或 python-telegram-bot
)并坚持下去。
免责声明:我目前是
python-telegram-bot
的维护者。