Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
        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()
Ejemplo n.º 5
0
        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()