Esempio n. 1
0
def main_menu(session, context):
    """Show the main menu."""
    context.query.message.edit_text(
        i18n.t("text.misc.start"),
        reply_markup=get_main_keyboard(context.user),
        parse_mode="Markdown",
        disable_web_page_preview=True,
    )
Esempio n. 2
0
def open_donations(session, context):
    """Send the donation text."""
    donation_keyboard = get_donation_keyboard()
    context.query.message.edit_text(
        i18n.t("text.misc.donations"),
        reply_markup=donation_keyboard,
        parse_mode="Markdown",
        disable_web_page_preview=True,
    )
Esempio n. 3
0
def start(bot, update, session, chat, user):
    """Send the start text."""
    if chat.is_maintenance or chat.is_newsfeed:
        update.message.chat.send_message("Hello there",
                                         reply_markup=get_main_keyboard(user))
    else:
        update.message.chat.send_message(
            i18n.t("text.misc.start"),
            reply_markup=get_main_keyboard(user),
            parse_mode="Markdown",
            disable_web_page_preview=True,
        )
Esempio n. 4
0
def initialize_set_tagging(session, bot, tg_chat, name, chat, user):
    """Initialize the set tag functionality of a chat."""
    sticker_set = StickerSet.get_or_create(session, name, chat, user)
    if sticker_set.complete is False:
        return "Sticker set {name} is currently being added."

    # Chat now expects an incoming tag for the next sticker
    chat.tag_mode = TagMode.sticker_set.value
    chat.current_sticker = sticker_set.stickers[0]

    tg_chat.send_message(i18n.t("text.tagging.send_tags"))
    send_tag_messages(chat, tg_chat, user)
Esempio n. 5
0
def get_help_text_and_keyboard(current_category):
    """Create the help message depending on the currently selected help category."""
    categories = [
        "Search",
        "Tagging",
        "Deluxe",
        "Language",
        "NSFW/Furry/Ban",
        "Bugs",
    ]

    help_texts = {}
    help_texts["Search"] = i18n.t("text.help.search", username=config["telegram"]["bot_name"])
    help_texts["Tagging"] = i18n.t("text.help.tagging")
    help_texts["Language"] = i18n.t("text.help.language")
    help_texts["NSFW/Furry/Ban"] = i18n.t("text.help.ban")
    help_texts["Bugs"] = i18n.t("text.help.bugs")
    help_texts["Deluxe"] = i18n.t("text.help.deluxe")

    text = help_texts[current_category]
    keyboard = get_help_keyboard(categories, current_category)

    return text, keyboard
        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()
Esempio n. 7
0
def tag_sticker(
    session,
    text,
    sticker,
    user,
    tg_chat=None,
    chat=None,
    message_id=None,
    replace=False,
    single_sticker=False,
):
    """Tag a single sticker."""
    # Extract all texts from message and clean/filter them
    raw_tags = get_tags_from_text(text)

    # No tags, early return
    if len(raw_tags) == 0:
        return

    # Only use the first few tags. This should prevent abuse from tag spammers.
    if len(raw_tags) > 10:
        raw_tags = raw_tags[:10]
        tg_chat.send_message(
            "Please don't send that many tags. Try to describe everything as brief as possible."
        )

    # Inform us if the user managed to hit a special count of changes
    if tg_chat and len(
            user.changes) in [10, 25, 50, 100, 250, 500, 1000, 2000, 3000]:
        achievement_message = i18n.t(
            f"text.tagging.achievements.{len(user.changes)}")
        tg_chat.send_message(achievement_message)

        sentry.capture_message(
            f"User hit {len(user.changes)} changes!",
            extra={
                "user": user.username,
                "user_id": user.id,
                "changes": len(user.changes),
            },
        )

    # List of tags that are newly added to this sticker
    new_tags = []
    # List of all new tags (raw_tags, but with resolved entities)
    # We need this, if we want to replace all tags
    incoming_tags = []

    international = user.international or sticker.sticker_set.international
    # Initialize the new tags array with the tags don't have the current language setting.
    for raw_tag in raw_tags:
        incoming_tag = Tag.get_or_create(session, raw_tag, international,
                                         False)
        incoming_tags.append(incoming_tag)

        # Add the tag to the list of new tags, if it doesn't exist on this sticker yet
        if incoming_tag not in sticker.tags:
            new_tags.append(incoming_tag)

    # We got no new tags
    if len(new_tags) == 0 and replace is False:
        session.commit()
        return

    # List of removed tags. This is only used, if we actually replace the sticker's tags

    removed_tags = []
    # Remove replace old tags
    if replace:
        # Merge the original emojis, since they should always be present on a sticker
        incoming_tags = incoming_tags + sticker.original_emojis
        # Find out, which stickers have been removed
        removed_tags = [
            tag for tag in sticker.tags if tag not in incoming_tags
        ]
        sticker.tags = incoming_tags
    else:
        for new_tag in new_tags:
            sticker.tags.append(new_tag)

    # Create a change for logging
    change = Change(
        user,
        sticker,
        international,
        new_tags,
        removed_tags,
        chat=chat,
        message_id=message_id,
    )
    session.add(change)

    session.commit()

    # Change the inline keyboard to allow fast fixing of the sticker's tags
    if tg_chat and chat and not single_sticker and chat.last_sticker_message_id:
        keyboard = get_fix_sticker_tags_keyboard(
            chat.current_sticker.file_unique_id)
        tg_chat.bot.edit_message_reply_markup(tg_chat.id,
                                              chat.last_sticker_message_id,
                                              reply_markup=keyboard)