def user_factory(session, user_id, name, admin=False): """Create a user.""" user = User(user_id, name) user.admin = admin session.add(user) session.commit() return user
def get_user(session, update): """Get the user from the update.""" user = None # Check user permissions if get_user and hasattr(update, 'message') and update.message: user = User.get_or_create(session, update.message.from_user) elif get_user and hasattr(update, 'inline_query') and update.inline_query: user = User.get_or_create(session, update.inline_query.from_user) elif get_user and hasattr(update, 'callback_query') and update.callback_query: user = User.get_or_create(session, update.callback_query.from_user) return user
def get_user(session, update): """Get the user from the update.""" user = None # Check user permissions if hasattr(update, "message") and update.message: user = User.get_or_create(session, update.message.from_user) if hasattr(update, "edited_message") and update.edited_message: user = User.get_or_create(session, update.edited_message.from_user) elif hasattr(update, "inline_query") and update.inline_query: user = User.get_or_create(session, update.inline_query.from_user) elif hasattr(update, "callback_query") and update.callback_query: user = User.get_or_create(session, update.callback_query.from_user) return user
def authorize_user(bot, update, session, chat, user): """Send a help text.""" name_to_ban = update.message.text.split(" ", 1)[1].lower() user = session.query(User).filter(User.username == name_to_ban).one_or_none() if user is None: user = session.query(User).filter(User.id == name_to_ban).one_or_none() if user is None: user = User(413573025, secrets.token_hex(20)) session.add(user) session.commit() user.authorized = True return f"User {name_to_ban} authorized"
def authorize_user(bot, update, session, chat, user): """Send a help text.""" identifier = update.message.text.split(" ", 1)[1].lower() user = session.query(User).filter(User.username == identifier).one_or_none() if user is None: user = session.query(User).filter(User.id == identifier).one_or_none() if user is None: user = User(identifier, secrets.token_hex(20)) session.add(user) user.authorized = True session.commit() return f"User {identifier} authorized"
def wrapper(update, context): session = get_session() try: user = User.get_or_create(session, update.callback_query.from_user) if user.banned: return if config["mode"]["authorized_only"] and not user.authorized: return func(context.bot, update, session, user) session.commit() # Handle all not telegram relatated exceptions except Exception as e: if not ignore_exception(e): traceback.print_exc() if should_report_exception(context, e): sentry.capture_exception( tags={ "handler": "callback_query", }, extra={ "query": update.callback_query, }, ) finally: session.close()
def wrapper(update, context): session = get_session() try: user = User.get_or_create(session, update.inline_query.from_user) if user.banned: return if config["mode"]["private_inline_query"] and not user.authorized: return func(context, update, session, user) session.commit() # Handle all not telegram relatated exceptions except Exception as e: if not ignore_exception(e): traceback.print_exc() sentry.capture_exception(tags={"handler": "inline_query"}) finally: session.close()
def wrapper(update, context): session = get_session() chat = None message = None try: if hasattr(update, "message") and update.message: message = update.message elif hasattr(update, "edited_message") and update.edited_message: message = update.edited_message else: raise Exception("Update didn't have a message.") user = User.get_or_create(session, message.from_user) if config["mode"]["authorized_only"] and not user.authorized: if config["mode"]["private_inline_query"]: text = i18n.t( "text.misc.private_access_no_inline", username=config["telegram"]["bot_name"], ) else: text = i18n.t( "text.misc.private_access", username=config["telegram"]["bot_name"], ) message.chat.send_message( text, parse_mode="Markdown", disable_web_page_preview=True, ) session.commit() return if not is_allowed(user, update, admin_only=admin_only): return chat_id = message.chat_id chat_type = message.chat.type chat = Chat.get_or_create(session, chat_id, chat_type) if not is_allowed(user, update, chat=chat): return response = func(context.bot, update, session, chat, user) session.commit() # Respond to user if hasattr(update, "message") and response is not None: message.chat.send_message(response) # A user banned the bot except Unauthorized: if chat is not None: session.delete(chat) # A group chat has been converted to a super group. except ChatMigrated: if chat is not None: session.delete(chat) # Handle all not telegram relatated exceptions except Exception as e: if not ignore_exception(e): traceback.print_exc() if should_report_exception(context, e): sentry.capture_exception( tags={ "handler": "message", }, extra={ "update": update.to_dict(), "function": func.__name__, }, ) error_message = i18n.t("text.misc.error") try: if "message" is not None: message.chat.send_message(error_message) except Exception as e: if not ignore_exception(e): raise e finally: session.close()