Exemplo n.º 1
0
def del_blacklist(update, context):
	chat = update.effective_chat  # type: Optional[Chat]
	message = update.effective_message  # type: Optional[Message]
	user = update.effective_user
	bot = context.bot
	to_match = extract_text(message)
	if not to_match:
		return

	getmode, value = sql.get_blacklist_setting(chat.id)

	chat_filters = sql.get_chat_blacklist(chat.id)
	for trigger in chat_filters:
		pattern = r"( |^|[^\w])" + re.escape(trigger) + r"( |$|[^\w])"
		if re.search(pattern, to_match, flags=re.IGNORECASE):
			try:
				if getmode == 0:
					return
				elif getmode == 1:
					message.delete()
				elif getmode == 2:
					message.delete()
					warn(update.effective_user, chat, tl(update.effective_message, "Mengatakan kata '{}' yang ada di daftar hitam").format(trigger), message, update.effective_user, conn=False)
					return
				elif getmode == 3:
					message.delete()
					bot.restrict_chat_member(chat.id, update.effective_user.id, can_send_messages=False)
					bot.sendMessage(chat.id, tl(update.effective_message, "{} di bisukan karena mengatakan kata '{}' yang ada di daftar hitam").format(mention_markdown(user.id, user.first_name), trigger), parse_mode="markdown")
					return
				elif getmode == 4:
					message.delete()
					res = chat.unban_member(update.effective_user.id)
					if res:
						bot.sendMessage(chat.id, tl(update.effective_message, "{} di tendang karena mengatakan kata '{}' yang ada di daftar hitam").format(mention_markdown(user.id, user.first_name), trigger), parse_mode="markdown")
					return
				elif getmode == 5:
					message.delete()
					chat.kick_member(user.id)
					bot.sendMessage(chat.id, tl(update.effective_message, "{} di blokir karena mengatakan kata '{}' yang ada di daftar hitam").format(mention_markdown(user.id, user.first_name), trigger), parse_mode="markdown")
					return
				elif getmode == 6:
					message.delete()
					bantime = extract_time(message, value)
					chat.kick_member(user.id, until_date=bantime)
					bot.sendMessage(chat.id, tl(update.effective_message, "{} di blokir selama {} karena mengatakan kata '{}' yang ada di daftar hitam").format(mention_markdown(user.id, user.first_name), value, trigger), parse_mode="markdown")
					return
				elif getmode == 7:
					message.delete()
					mutetime = extract_time(message, value)
					bot.restrict_chat_member(chat.id, user.id, until_date=mutetime, can_send_messages=False)
					bot.sendMessage(chat.id, tl(update.effective_message, "{} di bisukan selama {} karena mengatakan kata '{}' yang ada di daftar hitam").format(mention_markdown(user.id, user.first_name), value, trigger), parse_mode="markdown")
					return
			except BadRequest as excp:
				if excp.message == "Message to delete not found":
					pass
				else:
					LOGGER.exception("Error while deleting blacklist message.")
			break
Exemplo n.º 2
0
def reply_filter(update, context) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message  # type: Optional[Message]

    chat_warn_filters = sql.get_chat_warn_triggers(chat.id)
    to_match = extract_text(message)
    if not to_match:
        return ""

    for keyword in chat_warn_filters:
        pattern = r"( |^|[^\w])" + re.escape(keyword) + r"( |$|[^\w])"
        if re.search(pattern, to_match, flags=re.IGNORECASE):
            user = update.effective_user  # type: Optional[User]
            warn_filter = sql.get_warn_filter(chat.id, keyword)
            return warn(user, chat, warn_filter.reply, message)
    return ""
Exemplo n.º 3
0
def reply_filter(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message  # type: Optional[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.reply == "there is should be a new reply":
                buttons = sql.get_buttons(chat.id, filt.keyword)
                keyb = build_keyboard_parser(context.bot, chat.id, buttons)
                keyboard = InlineKeyboardMarkup(keyb)

                VALID_WELCOME_FORMATTERS = [
                    'first', 'last', 'fullname', 'username', 'id', 'chatname',
                    'mention'
                ]
                if filt.reply_text:
                    valid_format = escape_invalid_curly_brackets(
                        filt.reply_text, VALID_WELCOME_FORMATTERS)
                    if valid_format:
                        filtext = valid_format.format(
                            first=escape_markdown(
                                message.from_user.first_name),
                            last=escape_markdown(
                                message.from_user.last_name
                                or message.from_user.first_name),
                            fullname=escape_markdown(
                                " ".join([
                                    message.from_user.first_name, message.
                                    from_user.last_name
                                ] if message.from_user.last_name else
                                         [message.from_user.first_name])),
                            username="******" + message.from_user.username if
                            message.from_user.username else mention_markdown(
                                message.from_user.id,
                                message.from_user.first_name),
                            mention=mention_markdown(
                                message.from_user.id,
                                message.from_user.first_name),
                            chatname=escape_markdown(
                                message.chat.title if message.chat.type !=
                                "private" else message.from_user.first_name),
                            id=message.from_user.id)
                    else:
                        filtext = ""
                else:
                    filtext = ""

                if filt.file_type in (sql.Types.BUTTON_TEXT, sql.Types.TEXT):
                    try:
                        context.bot.send_message(
                            chat.id,
                            filtext,
                            reply_to_message_id=message.message_id,
                            parse_mode="markdown",
                            disable_web_page_preview=True,
                            reply_markup=keyboard)
                    except BadRequest as excp:
                        error_catch = get_exception(excp, filt, chat)
                        if error_catch == "noreply":
                            try:
                                context.bot.send_message(
                                    chat.id,
                                    filtext,
                                    parse_mode="markdown",
                                    disable_web_page_preview=True,
                                    reply_markup=keyboard)
                            except BadRequest as excp:
                                LOGGER.exception("Gagal mengirim pesan: " +
                                                 excp.message)
                                send_message(
                                    update.effective_message,
                                    tl(update.effective_message,
                                       get_exception(excp, filt, chat)))
                                pass
                        else:
                            try:
                                send_message(
                                    update.effective_message,
                                    tl(update.effective_message,
                                       get_exception(excp, filt, chat)))
                            except BadRequest as excp:
                                LOGGER.exception("Gagal mengirim pesan: " +
                                                 excp.message)
                                pass
                else:
                    ENUM_FUNC_MAP[filt.file_type](
                        chat.id,
                        filt.file_id,
                        caption=filtext,
                        reply_to_message_id=message.message_id,
                        parse_mode="markdown",
                        disable_web_page_preview=True,
                        reply_markup=keyboard)
                break
            else:
                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_parser(context.bot, chat.id, buttons)
                    keyboard = InlineKeyboardMarkup(keyb)

                    try:
                        send_message(update.effective_message,
                                     filt.reply,
                                     parse_mode=ParseMode.MARKDOWN,
                                     disable_web_page_preview=True,
                                     reply_markup=keyboard)
                    except BadRequest as excp:
                        if excp.message == "Unsupported url protocol":
                            try:
                                send_message(
                                    update.effective_message,
                                    tl(
                                        update.effective_message,
                                        "Anda tampaknya mencoba menggunakan protokol url yang tidak didukung. Telegram "
                                        "tidak mendukung tombol untuk beberapa protokol, seperti tg://. Silakan coba "
                                        "lagi."))
                            except BadRequest as excp:
                                LOGGER.exception("Gagal mengirim pesan: " +
                                                 excp.message)
                                pass
                        elif excp.message == "Reply message not found":
                            try:
                                context.bot.send_message(
                                    chat.id,
                                    filt.reply,
                                    parse_mode=ParseMode.MARKDOWN,
                                    disable_web_page_preview=True,
                                    reply_markup=keyboard)
                            except BadRequest as excp:
                                LOGGER.exception("Gagal mengirim pesan: " +
                                                 excp.message)
                                pass
                        else:
                            try:
                                send_message(
                                    update.effective_message,
                                    tl(
                                        update.effective_message,
                                        "Catatan ini tidak dapat dikirim karena formatnya salah."
                                    ))
                            except BadRequest as excp:
                                LOGGER.exception("Gagal mengirim pesan: " +
                                                 excp.message)
                                pass
                            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.
                    try:
                        send_message(update.effective_message, filt.reply)
                    except BadRequest as excp:
                        LOGGER.exception("Gagal mengirim pesan: " +
                                         excp.message)
                        pass
                break
Exemplo n.º 4
0
def reply_filter(bot: Bot, update: Update):
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message  # type: Optional[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 == "Unsupported url protocol":
                        try:
                            message.reply_text(
                                "Anda tampaknya mencoba menggunakan protokol url yang tidak didukung. Telegram "
                                "tidak mendukung tombol untuk beberapa protokol, seperti tg://. Silakan coba "
                                "lagi.")
                        except BadRequest as excp:
                            LOGGER.exception("Gagal mengirim pesan: " +
                                             excp.message)
                            pass
                    elif excp.message == "Reply message not found":
                        try:
                            bot.send_message(chat.id,
                                             filt.reply,
                                             parse_mode=ParseMode.MARKDOWN,
                                             disable_web_page_preview=True,
                                             reply_markup=keyboard)
                        except BadRequest as excp:
                            LOGGER.exception("Gagal mengirim pesan: " +
                                             excp.message)
                            pass
                    else:
                        try:
                            message.reply_text(
                                "Catatan ini tidak dapat dikirim karena formatnya salah."
                            )
                        except BadRequest as excp:
                            LOGGER.exception("Gagal mengirim pesan: " +
                                             excp.message)
                            pass
                        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.
                try:
                    message.reply_text(filt.reply)
                except BadRequest as excp:
                    LOGGER.exception("Gagal mengirim pesan: " + excp.message)
                    pass
            break