示例#1
0
def add_blacklist(bot: Bot, update: Update):
    msg = update.effective_message
    chat = update.effective_chat
    words = msg.text.split(None, 1)

    if len(words) > 1:
        text = words[1]
        to_blacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip()))

        for trigger in to_blacklist:
            try:
                re.compile(trigger)
            except Exception as exce:
                msg.reply_text(f"Normal ifade eklenemedi, Error: {exce}")
                return
            check = infinite_loop_check(trigger)
            if not check:
               sql.add_to_blacklist(chat.id, trigger.lower())
            else:
                msg.reply_text("Korkarım şu normal ifadeyi ekleyemiyorum.")
                return

        if len(to_blacklist) == 1:
            msg.reply_text(f"Eklendi <code>{html.escape(to_blacklist[0])}</code> to the blacklist!",
                           parse_mode=ParseMode.HTML)

        else:
            msg.reply_text(f"Added <code>{len(to_blacklist)}</code> tetikler blacklist.",
                           parse_mode=ParseMode.HTML)

    else:
        msg.reply_text("Bana hangi kelimeleri kaldırmak istediğini söyle blacklist.")
示例#2
0
def add_blacklist(bot: Bot, update: Update):
    msg = update.effective_message
    chat = update.effective_chat
    words = msg.text.split(None, 1)

    if len(words) > 1:
        text = words[1]
        to_blacklist = list(
            set(trigger.strip() for trigger in text.split("\n")
                if trigger.strip()))

        for trigger in to_blacklist:
            try:
                re.compile(trigger)
            except Exception as exce:
                msg.reply_text(f"Couldn't add regex, Error: {exce}")
                return
            check = infinite_loop_check(trigger)
            if not check:
                sql.add_to_blacklist(chat.id, trigger.lower())
            else:
                msg.reply_text("I'm afraid I can't add that regex.")
                return

        if len(to_blacklist) == 1:
            msg.reply_text(
                f"Added <code>{html.escape(to_blacklist[0])}</code> to the blacklist!",
                parse_mode=ParseMode.HTML)

        else:
            msg.reply_text(
                f"Added <code>{len(to_blacklist)}</code> triggers to the blacklist.",
                parse_mode=ParseMode.HTML)

    else:
        msg.reply_text(
            "Tell me which words you would like to remove from the blacklist.")
示例#3
0
def filters(update: Update, context: CallbackContext):
    chat = update.effective_chat
    msg = update.effective_message
    args = msg.text.split(None, 1)

    if len(args) < 2:
        return

    extracted = split_quotes(args[1])
    if len(extracted) < 1:
        return
    # set trigger -> lower, so as to avoid adding duplicate filters with different cases
    keyword = extracted[0]
    is_sticker = False
    is_document = False
    is_image = False
    is_voice = False
    is_audio = False
    is_video = False
    buttons = []

    # determine what the contents of the filter are - text, image, sticker, etc
    if len(extracted) >= 2:
        offset = len(extracted[1]) - len(
            msg.text)  # set correct offset relative to command + notename
        content, buttons = button_markdown_parser(
            extracted[1], entities=msg.parse_entities(), offset=offset)
        content = content.strip()
        if not content:
            msg.reply_text(
                "There is no note message - You can't JUST have buttons, you need a message to go with it!"
            )
            return

    elif msg.reply_to_message and msg.reply_to_message.sticker:
        content = msg.reply_to_message.sticker.file_id
        is_sticker = True

    elif msg.reply_to_message and msg.reply_to_message.document:
        content = msg.reply_to_message.document.file_id
        is_document = True

    elif msg.reply_to_message and msg.reply_to_message.photo:
        content = msg.reply_to_message.photo[
            -1].file_id  # last elem = best quality
        is_image = True

    elif msg.reply_to_message and msg.reply_to_message.audio:
        content = msg.reply_to_message.audio.file_id
        is_audio = True

    elif msg.reply_to_message and msg.reply_to_message.voice:
        content = msg.reply_to_message.voice.file_id
        is_voice = True

    elif msg.reply_to_message and msg.reply_to_message.video:
        content = msg.reply_to_message.video.file_id
        is_video = True

    else:
        msg.reply_text("You didn't specify what to reply with!")
        return
    if infinite_loop_check(keyword):
        msg.reply_text("I'm afraid I can't add that regex")
        return
    # Add the filter
    # Note: perhaps handlers can be removed somehow using sql.get_chat_filters
    for handler in dispatcher.handlers.get(HANDLER_GROUP, []):
        if handler.filters == (keyword, chat.id):
            dispatcher.remove_handler(handler, HANDLER_GROUP)

    sql.add_filter(chat.id, keyword, content, is_sticker, is_document,
                   is_image, is_audio, is_voice, is_video, buttons)

    msg.reply_text("Handler '{}' added!".format(keyword))
    raise DispatcherHandlerStop
示例#4
0
def sed(bot: Bot, update: Update):
    sed_result = separate_sed(update.effective_message.text)
    if sed_result and update.effective_message.reply_to_message:
        if update.effective_message.reply_to_message.text:
            to_fix = update.effective_message.reply_to_message.text
        elif update.effective_message.reply_to_message.caption:
            to_fix = update.effective_message.reply_to_message.caption
        else:
            return

        repl, repl_with, flags = sed_result
        if not repl:
            update.effective_message.reply_to_message.reply_text(
                "Değiştirmeye çalışıyorsun... "
                "bir şey ile hiçbir şey?")
            return

        try:
            try:
                check = regex.match(repl,
                                    to_fix,
                                    flags=regex.IGNORECASE,
                                    timeout=5)
            except TimeoutError:
                return
            if check and check.group(0).lower() == to_fix.lower():
                update.effective_message.reply_to_message.reply_text(
                    "Selam millet, {} yapmaya çalışıyor "
                    "istemediğimi söylerim "
                    "söyle!".format(update.effective_user.first_name))
                return
            if infinite_loop_check(repl):
                update.effective_message.reply_text(
                    "Korkarım şu normal ifadeyi çalıştıramam.")
                return
            if 'i' in flags and 'g' in flags:
                text = regex.sub(repl,
                                 repl_with,
                                 to_fix,
                                 flags=regex.I,
                                 timeout=3).strip()
            elif 'i' in flags:
                text = regex.sub(repl,
                                 repl_with,
                                 to_fix,
                                 count=1,
                                 flags=regex.I,
                                 timeout=3).strip()
            elif 'g' in flags:
                text = regex.sub(repl, repl_with, to_fix, timeout=3).strip()
            else:
                text = regex.sub(repl, repl_with, to_fix, count=1,
                                 timeout=3).strip()
        except TimeoutError:
            update.effective_message.reply_text('Timeout')
            return
        except sre_constants.error:
            LOGGER.warning(update.effective_message.text)
            LOGGER.exception("SRE sabit hatası")
            update.effective_message.reply_text(
                "Sed bile mi? Görünüşe göre öyle değil.")
            return

        # empty string errors -_-
        if len(text) >= telegram.MAX_MESSAGE_LENGTH:
            update.effective_message.reply_text(
                "Sed komutunun sonucu çok uzun \
                                                 telegram!")
        elif text:
            update.effective_message.reply_to_message.reply_text(text)
示例#5
0
def sed(update: Update, context: CallbackContext):
    sed_result = separate_sed(update.effective_message.text)
    if sed_result and update.effective_message.reply_to_message:
        if update.effective_message.reply_to_message.text:
            to_fix = update.effective_message.reply_to_message.text
        elif update.effective_message.reply_to_message.caption:
            to_fix = update.effective_message.reply_to_message.caption
        else:
            return

        repl, repl_with, flags = sed_result
        if not repl:
            update.effective_message.reply_to_message.reply_text(
                "You're trying to replace... " "nothing with something?",
            )
            return

        try:
            try:
                check = regex.match(repl, to_fix, flags=regex.IGNORECASE, timeout=5)
            except TimeoutError:
                return
            if check and check.group(0).lower() == to_fix.lower():
                update.effective_message.reply_to_message.reply_text(
                    "Hey everyone, {} is trying to make "
                    "me say stuff I don't wanna "
                    "say!".format(update.effective_user.first_name),
                )
                return
            if infinite_loop_check(repl):
                update.effective_message.reply_text(
                    "I'm afraid I can't run that regex.",
                )
                return
            if "i" in flags and "g" in flags:
                text = regex.sub(
                    repl,
                    repl_with,
                    to_fix,
                    flags=regex.I,
                    timeout=3,
                ).strip()
            elif "i" in flags:
                text = regex.sub(
                    repl,
                    repl_with,
                    to_fix,
                    count=1,
                    flags=regex.I,
                    timeout=3,
                ).strip()
            elif "g" in flags:
                text = regex.sub(repl, repl_with, to_fix, timeout=3).strip()
            else:
                text = regex.sub(repl, repl_with, to_fix, count=1, timeout=3).strip()
        except TimeoutError:
            update.effective_message.reply_text("Timeout")
            return
        except sre_constants.error:
            LOGGER.warning(update.effective_message.text)
            LOGGER.exception("SRE constant error")
            update.effective_message.reply_text("Do you even sed? Apparently not.")
            return

        # empty string errors -_-
        if len(text) >= telegram.MAX_MESSAGE_LENGTH:
            update.effective_message.reply_text(
                "The result of the sed command was too long for \
                                                 telegram!",
            )
        elif text:
            update.effective_message.reply_to_message.reply_text(text)