Ejemplo n.º 1
0
def stop_filter(update: Update, _):
    chat = update.effective_chat
    msg = update.effective_message
    args = msg.text.split(None, 1)

    if len(args) < 2:
        return

    chat_filters = sql.get_chat_triggers(chat.id)

    if not chat_filters:
        msg.reply_text("No filters are active here!")
        return

    for keyword in chat_filters:
        if keyword == args[1]:
            sql.remove_filter(chat.id, args[1])
            msg.reply_text("Yep, I'll stop replying to that.")
            raise DispatcherHandlerStop

    msg.reply_text(
        "That's not a current filter - run /filters for all active filters.")
Ejemplo n.º 2
0
def list_handlers(update: Update, _):
    chat = update.effective_chat
    all_handlers = sql.get_chat_triggers(chat.id)

    update_chat_title = chat.title
    message_chat_title = update.effective_message.chat.title

    if update_chat_title == message_chat_title:
        BASIC_FILTER_STRING = "<b>Filters in this chat:</b>\n"
    else:
        BASIC_FILTER_STRING = f"<b>Filters in {update_chat_title}</b>:\n"

    if not all_handlers:
        if update_chat_title == message_chat_title:
            update.effective_message.reply_text("No filters are active here!")
        else:
            update.effective_message.reply_text(
                f"No filters are active in <b>{update_chat_title}</b>!",
                parse_mode=telegram.ParseMode.HTML,
            )
        return

    filter_list = ""
    for keyword in all_handlers:
        entry = f" - {escape_markdown(keyword)}\n"
        if (len(entry) + len(filter_list) + len(BASIC_FILTER_STRING) >
                telegram.MAX_MESSAGE_LENGTH):
            filter_list = BASIC_FILTER_STRING + html.escape(filter_list)
            update.effective_message.reply_text(
                filter_list, parse_mode=telegram.ParseMode.HTML)
            filter_list = entry
        else:
            filter_list += entry

    if filter_list != BASIC_FILTER_STRING:
        filter_list = BASIC_FILTER_STRING + html.escape(filter_list)
        update.effective_message.reply_text(filter_list,
                                            parse_mode=telegram.ParseMode.HTML)
Ejemplo n.º 3
0
def __chat_settings__(chat_id, _user_id):
    cust_filters = sql.get_chat_triggers(chat_id)
    return "There are currently `{}` custom filters here.".format(
        len(cust_filters))
Ejemplo n.º 4
0
def reply_filter(update: Update, context: CallbackContext):
    chat = update.effective_chat
    message = update.effective_message
    to_match = extract_text(message)

    if not to_match:
        return

    chat_filters = sql.get_chat_triggers(chat.id)
    for keyword in chat_filters:
        pattern = r"( |^|[^\w])" + re.escape(keyword) + r"( |$|[^\w])"
        if re.search(pattern, to_match, flags=re.IGNORECASE):
            filt = sql.get_filter(chat.id, keyword)
            if filt.is_sticker:
                message.reply_sticker(filt.reply)
            elif filt.is_document:
                message.reply_document(filt.reply)
            elif filt.is_image:
                message.reply_photo(filt.reply)
            elif filt.is_audio:
                message.reply_audio(filt.reply)
            elif filt.is_voice:
                message.reply_voice(filt.reply)
            elif filt.is_video:
                message.reply_video(filt.reply)
            elif filt.has_markdown:
                buttons = sql.get_buttons(chat.id, filt.keyword)
                keyb = build_keyboard(buttons)
                keyboard = InlineKeyboardMarkup(keyb)

                try:
                    message.reply_text(
                        filt.reply,
                        parse_mode=ParseMode.MARKDOWN,
                        disable_web_page_preview=True,
                        reply_markup=keyboard,
                    )
                except BadRequest as excp:
                    if excp.message == "Reply message not found":
                        context.bot.send_message(
                            chat.id,
                            filt.reply,
                            parse_mode=ParseMode.MARKDOWN,
                            disable_web_page_preview=True,
                            reply_markup=keyboard,
                        )
                    elif excp.message == "Unsupported url protocol":
                        message.reply_text(
                            "You seem to be trying to use an unsupported url protocol. Telegram "
                            "doesn't support buttons for some protocols, such as tg://. Please try "
                            "again, or ask @LyndaEagleSupport for help.")
                    else:
                        message.reply_text(
                            "This note could not be sent, as it is incorrectly formatted. Ask in "
                            "@LyndaEagleSupport if you can't figure out why!")
                        LOGGER.warning("Message %s could not be parsed",
                                       str(filt.reply))
                        LOGGER.exception(
                            "Could not parse filter %s in chat %s",
                            str(filt.keyword),
                            str(chat.id),
                        )

            else:
                # LEGACY - all new filters will have has_markdown set to True.
                message.reply_text(filt.reply)
            break