def verify(chat_id=None, user_id=None, user_name=None): """ This is responsible for verifying the captcha results (either pass or failure). It takes an optional `callback_chat_id` param. This param is passed to `HCaptchaBot.verify`. """ callback_chat_id = request.args.get("callback_chat_id", None) token = request.form["h-captcha-response"] params = urllib.parse.urlencode({ "secret": get_active_config().HCAPTCHA_SECRET, "response": token }) with urllib.request.urlopen(get_active_config().HCAPTCHA_POST_URI, params.encode("ascii")) as response: json_response = json.loads(response.read().decode("utf-8")) if json_response["success"]: current_app.logger.info(f"user_id: {user_id} solved captcha") current_app.bot_instance.verify(chat_id, user_id, user_name, callback_chat_id) return render_template("success.html") else: current_app.logger.info(f"user_id: {user_id} failed captcha") return render_template("error.html")
def handler(self, update: Update, context: CallbackContext): command = update.message.text[: get_active_config().MESSAGE_CHARS_LIMIT] + ( update.message.text[get_active_config().MESSAGE_CHARS_LIMIT :] and "..." ) context.bot.send_message( chat_id=update.message.chat_id, text=f"Sorry, didn't recognize this command:\n{command}", ) HelpCommand(self.app).handler(update, context)
def register_bot(app, config_name): app.bot_instance = HCaptchaBot(get_active_config().TELEGRAM_TOKEN, app) # Don't explicitly run the bot in testing env if config_name != "testing": app.bot_instance.setup() app.bot_instance.run()
def __init__(self, bot, app=None, minutes=None): self.bot = bot self.app = app self.minutes = (minutes if minutes is not None else int( get_active_config().CLEANUP_PERIOD_MINUTES)) schedule.every(self.minutes).minutes.do(self.cleanup)
def captcha(chat_id=None, user_id=None, user_name=None): """ This is responsible for showing the captcha. It takes an optional `callback_chat_id` param. This param is passed to the `verify` function below. """ callback_chat_id = request.args.get("callback_chat_id", None) return render_template( "captcha.html", chat_id=chat_id, user_id=user_id, user_name=user_name, callback_chat_id=callback_chat_id, site_key=get_active_config().HCAPTCHA_SITE_KEY, )
def setup(self): """ Called once when the app launches. """ # set webhook if not should_run_webhook(): return webhook_url = "{}{}".format(get_active_config().APP_URL, self.token) current_webhook_url = self.bot.get_webhook_info().url if current_webhook_url != webhook_url: logger.info(f"setting webhook url to: {webhook_url}") self.bot.set_webhook(webhook_url)
from flask import Blueprint, current_app, request from telegram import Update from app.config import get_active_config blueprint = Blueprint("webhook", __name__) @blueprint.route("/" + get_active_config().TELEGRAM_TOKEN, methods=["POST"]) def webhook(): update = request.get_json() current_app.bot_instance.update_queue.put( Update.de_json(update, current_app.bot_instance.bot)) return ("", 204)
def setUp(self): super().setUp() self.hCaptchaBot = HCaptchaBot(get_active_config().TELEGRAM_TOKEN) self.bot = self.hCaptchaBot.bot