Esempio n. 1
0
def rules(update: Update, _: CallbackContext) -> None:
    """Load and send the appropriate rules based on which group we're in"""
    message = cast(Message, update.effective_message)
    if message.chat.username == ONTOPIC_USERNAME:
        message.reply_text(ONTOPIC_RULES, quote=False)
        try_to_delete(message)
    elif message.chat.username == OFFTOPIC_USERNAME:
        message.reply_text(OFFTOPIC_RULES, quote=False)
        try_to_delete(message)
    else:
        message.reply_text("Hmm. You're not in a python-telegram-bot group, "
                           "and I don't know the rules around here.")
Esempio n. 2
0
async def rules(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Load and send the appropriate rules based on which group we're in"""
    message = cast(Message, update.effective_message)
    if message.chat.username == ONTOPIC_USERNAME:
        await message.reply_text(ONTOPIC_RULES, quote=False)
        context.application.create_task(try_to_delete(message), update=update)
    elif message.chat.username == OFFTOPIC_USERNAME:
        await message.reply_text(OFFTOPIC_RULES, quote=False)
        context.application.create_task(try_to_delete(message), update=update)
    else:
        await message.reply_text(
            "Hmm. You're not in a python-telegram-bot group, "
            "and I don't know the rules around here.")
Esempio n. 3
0
def help_callback(update: Update, context: CallbackContext) -> None:
    """ Link to rules readme """
    message = cast(Message, update.effective_message)
    text = (
        f"You can find an explanation of @{html.escape(context.bot.username)}'s functionality "
        'wiki on <a href="https://github.com/python-telegram-bot/rules-bot/blob/master/README.md">'
        "GitHub</a>.")
    message.reply_text(
        text,
        quote=False,
        reply_to_message_id=get_reply_id(update),
    )
    try_to_delete(message)
Esempio n. 4
0
def wiki(update: Update, _: CallbackContext) -> None:
    """ Wiki link """
    message = cast(Message, update.effective_message)
    text = (
        "You can find our wiki on "
        "[GitHub](https://github.com/python-telegram-bot/python-telegram-bot/wiki)"
    )
    message.reply_text(
        text,
        parse_mode="Markdown",
        quote=False,
        reply_to_message_id=get_reply_id(update),
    )
    try_to_delete(message)
Esempio n. 5
0
def docs(update: Update, _: CallbackContext) -> None:
    """ Documentation link """
    message = cast(Message, update.effective_message)
    text = (
        "You can find our documentation at "
        "[Read the Docs](https://python-telegram-bot.readthedocs.io/en/stable/)"
    )
    reply_id = message.reply_to_message.message_id if message.reply_to_message else None
    message.reply_text(
        text,
        parse_mode="Markdown",
        quote=False,
        reply_to_message_id=reply_id,
    )
    try_to_delete(message)
Esempio n. 6
0
async def off_on_topic(update: Update,
                       context: ContextTypes.DEFAULT_TYPE) -> None:
    """Redirect users to the off-topic or on-topic group"""
    # Minimal effort LRU cache
    # We store the newest 64 messages that lead to redirection to minimize the chance that
    # editing a message falsely triggers the redirect again
    parsed_messages = cast(Dict, context.chat_data).setdefault(
        "redirect_messages", deque(maxlen=64))

    message = cast(Message, update.effective_message)
    if message.message_id in parsed_messages:
        return

    # Standalone on/off-topic commands don't make any sense
    # But we only delete them if they contain nothing but the command
    if not message.reply_to_message:
        entities = message.parse_entities()
        if len(entities) == 1:
            entity, text = entities.popitem()
            if entity.type == MessageEntity.BOT_COMMAND and text == message.text:
                context.application.create_task(try_to_delete(message),
                                                update=update)
        return

    chat_username = cast(Chat, update.effective_chat).username
    group_one = cast(Match, context.match).group(1)
    if chat_username == ONTOPIC_USERNAME and group_one.lower() == "off":
        reply = message.reply_to_message
        if reply.text:
            issued_reply = get_reply_id(update)

            if reply.from_user:
                if reply.from_user.username:
                    name = "@" + reply.from_user.username
                else:
                    name = reply.from_user.first_name
            else:
                # Probably never happens anyway ...
                name = "Somebody"

            replied_message_text = reply.text_html
            replied_message_id = reply.message_id

            text = (
                f'{name} <a href="t.me/{ONTOPIC_USERNAME}/{replied_message_id}">wrote</a>:\n'
                f"{replied_message_text}\n\n"
                f"⬇️ ᴘʟᴇᴀsᴇ ᴄᴏɴᴛɪɴᴜᴇ ʜᴇʀᴇ ⬇️")

            offtopic_msg = await context.bot.send_message(
                OFFTOPIC_CHAT_ID, text)

            await message.reply_text(
                text=
                ('I moved this discussion to the <a href="https://t.me/'
                 f'{OFFTOPIC_USERNAME}/{offtopic_msg.message_id}">off-topic group</a>.'
                 ),
                reply_to_message_id=issued_reply,
            )

        else:
            await message.reply_text(
                f'The off-topic group is <a href="https://t.me/{OFFTOPIC_USERNAME}">here</a>. '
                "Come join us!", )

    elif chat_username == OFFTOPIC_USERNAME and group_one.lower() == "on":
        await message.reply_text(
            f'The on-topic group is <a href="https://t.me/{ONTOPIC_USERNAME}">here</a>. '
            "Come join us!", )

    parsed_messages.append(message.message_id)
Esempio n. 7
0
def ban_sender_channels(update: Update, _: CallbackContext) -> None:
    message = cast(Message, update.effective_message)
    cast(Chat, update.effective_chat).ban_sender_chat(
        cast(Chat, message.sender_chat).id)
    try_to_delete(message)
Esempio n. 8
0
        # Merge keyboards into one
        if entry_kb := hint.inline_keyboard:
            if keyboard is None:
                keyboard = deepcopy(entry_kb)
            else:
                keyboard.inline_keyboard.extend(entry_kb.inline_keyboard)

    effective_text = "\n➖\n".join(messages)
    message.reply_text(
        effective_text,
        reply_markup=keyboard,
        reply_to_message_id=reply_to.message_id if reply_to else None,
    )

    if reply_to and first_match == 0:
        try_to_delete(message)


def ban_sender_channels(update: Update, _: CallbackContext) -> None:
    message = cast(Message, update.effective_message)
    cast(Chat, update.effective_chat).ban_sender_chat(
        cast(Chat, message.sender_chat).id)
    try_to_delete(message)


def say_potato_job(context: CallbackContext) -> None:
    user_id, message, who_banned = cast(Tuple[int, Message, User],
                                        cast(Job, context.job).context)
    context.bot.ban_chat_member(chat_id=ONTOPIC_CHAT_ID, user_id=user_id)
    context.bot.ban_chat_member(chat_id=OFFTOPIC_CHAT_ID, user_id=user_id)