Пример #1
0
def read_trigger(infos: Infos) -> Callable:
    if infos.is_callback_query:
        if infos.callback_query.data == "done":
            return to_menu(infos)

    new_trigger = infos.message.text
    t_type = infos.bot.waiting_data["type"]
    section = infos.bot.waiting_data["section"]

    t = Trigger(t_type, new_trigger, section, infos.bot.bot_id,
                infos.db.language)
    mongo_interface.add_trigger(t)

    triggers = mongo_interface.get_triggers_of_type_and_section(
        infos.bot.bot_id, t_type, section, infos.db.language)

    msg = "Now send the triggers as replies."
    if triggers:
        triggs = make_trigger_list(triggers)
        msg = f"[md]Triggers of type `{t_type}` in section `{section}`:\n{triggs}\n" + msg

    infos.edit(msg,
               msg_id=infos.bot.waiting_data["msg"].message_id,
               reply_markup=keyboards.done(),
               parse=False)

    return read_trigger
Пример #2
0
def add_dialog(infos: Infos) -> Callable:
    # Waiting for a message (section)
    if not infos.is_message:
        return list_dialogs

    if not infos.message.is_text:
        return add_dialog

    section = infos.message.text
    infos.bot.waiting_data["section"] = section

    dialogs = mongo_interface.get_dialogs_of_section(infos.bot.bot_id, section,
                                                     infos.db.language)

    # Final message to append
    f_msg = "Please send the replies you want!"

    if not dialogs:
        msg = f"[md]No dialogs for section `{section}`\n{f_msg}"
    else:
        dials = make_dialogs_list(dialogs)
        msg = f"[md]Dialogs for section `{section}`:\n{dials}\n{f_msg}"

    infos.edit(msg,
               reply_markup=keyboards.done(),
               msg_id=infos.bot.waiting_data["msg"].message_id,
               parse=False)

    return wait_add_dialog_reply
Пример #3
0
def del_dialog(infos: Infos) -> Callable:
    # Waiting for a message (section)
    if not infos.is_message:
        return del_dialog

    if not infos.message.is_text:
        return del_dialog

    section = infos.message.text
    dialogs = mongo_interface.get_dialogs_of_section(infos.bot.bot_id, section,
                                                     infos.db.language)

    # Final message to append
    f_msg = "Please send the number of the dialog you want to delete."

    if not dialogs:
        msg = f"[md]No dialogs for section `{section}`\nDo you need something else?"
        return to_menu(infos, msg)

    dials = make_dialogs_list(dialogs)
    msg = f"[md]Dialogs for section `{section}`:\n{dials}\n\n{f_msg}"

    infos.edit(msg,
               reply_markup=keyboards.done(),
               msg_id=infos.bot.waiting_data["msg"].message_id,
               parse=False)

    infos.bot.waiting_data["section"] = section
    return wait_del_dialog_number
Пример #4
0
def wait_trigger_type_add_reply(infos: Infos) -> Callable:
    if not infos.is_callback_query:
        return wait_trigger_type_add_reply

    if infos.callback_query.data == "cancel":
        return to_menu(
            infos,
            f"Operation cancelled, do you need something else, {infos.user.name}?"
        )

    sel_type = select_trigger_type(infos)
    if not sel_type:
        return wait_trigger_type_add_reply

    section = infos.bot.waiting_data["section"]
    triggers = mongo_interface.get_triggers_of_type_and_section(
        infos.bot.bot_id, sel_type, section, infos.db.language)
    triggs = make_trigger_list(triggers)

    infos.bot.waiting_data["type"] = sel_type
    infos.edit(
        f"[md]Trigger of type `{sel_type}` in section `{section}`:\n"
        f"{triggs}\n"
        "Now send the triggers as replies.",
        msg_id=infos.bot.waiting_data["msg"].message_id,
        reply_markup=keyboards.done(),
        parse=False)

    return read_trigger
Пример #5
0
def wait_trigger_type_del_trigger(infos: Infos) -> Callable:
    if not infos.is_callback_query:
        return wait_trigger_type_del_trigger

    sel_type = select_trigger_type(infos)
    if not sel_type:
        return wait_trigger_type_del_trigger

    infos.bot.waiting_data["type"] = sel_type

    triggers = mongo_interface.get_triggers_of_type(infos.bot.bot_id, sel_type,
                                                    infos.db.language)

    if not triggers:
        return to_menu(
            infos, f"No triggers of type {sel_type}.\n"
            "Do you need something else?")

    triggs = make_trigger_list(triggers)

    infos.edit(
        f"[md]Trigger of type `{sel_type}`:\n"
        f"{triggs}\n"
        "Please send the number of the trigger to delete",
        msg_id=infos.bot.waiting_data["msg"].message_id,
        reply_markup=keyboards.done(),
        parse=False)

    return wait_del_trigger_index
Пример #6
0
def wait_del_section_number(infos: Infos) -> Callable:
    if infos.is_callback_query:
        if infos.callback_query.data == "done":
            return to_menu(infos)

    to_delete: List[str] = []

    sections = mongo_interface.get_sections(infos.bot.bot_id,
                                            infos.db.language)

    indexes: List[str] = infos.message.text.split("," if "," in
                                                  infos.message.text else " ")

    for string_index in indexes:
        try:
            string_index = string_index.strip()
            index = int(string_index)
        except ValueError:
            infos.reply(f"`{string_index}` is not a valid number.")
            return wait_del_section_number

        if index <= 0:
            infos.reply("The minimum index is 1!")
            return wait_del_section_number

        if index - 1 > len(sections):
            infos.reply(f"You've selected section n°{index} but "
                        f"there are only {len(sections) + 1} sections")
            return wait_del_section_number

        section = sections[index - 1]
        to_delete.append(section)

    for section in to_delete:
        mongo_interface.delete_dialogs_of_section(infos.bot.bot_id, section,
                                                  infos.db.language)
        mongo_interface.delete_triggers_of_section(infos.bot.bot_id, section,
                                                   infos.db.language)
        sections.remove(section)

    if not sections:
        msg = f"I don't have anymore sections\nDo you need something else?"
        return to_menu(infos, msg)

    infos.edit(
        f"[md]Current sections:\n{make_sections_list(infos)}"
        f"\n\nPlease send the number of the section you want to delete.\n"
        f"*Remember that deleting a section means deleting every dialog/trigger linked to it!!*",
        reply_markup=keyboards.done(),
        msg_id=infos.bot.waiting_data["msg"].message_id,
        parse=False)

    return wait_del_section_number
Пример #7
0
def wait_del_trigger_index(infos: Infos) -> Callable:
    if infos.is_callback_query:
        if infos.callback_query.data == "done":
            return to_menu(infos)

    to_remove: List[Trigger] = []

    sel_type = infos.bot.waiting_data["type"]
    triggers = mongo_interface.get_triggers_of_type(infos.bot.bot_id, sel_type,
                                                    infos.db.language)

    indexes: List[str] = infos.message.text.split("," if "," in
                                                  infos.message.text else " ")
    for stringIndex in indexes:
        try:
            index = int(stringIndex.strip())
        except ValueError:
            infos.reply(f"{infos.message.text} is not a valid index.")
            return wait_del_trigger_index

        if index < 1:
            infos.reply("Index can't be lesser than one.")
            return wait_del_trigger_index

        if index - 1 > len(triggers):
            infos.reply(f"{index} is too high, max: {len(triggers)}")
            return wait_del_trigger_index

        trigger = triggers[index - 1]
        to_remove.append(trigger)

    for trigger in to_remove:
        triggers.remove(trigger)
        mongo_interface.delete_trigger(trigger)

    if not triggers:
        return to_menu(
            infos, f"No more triggers of type {sel_type}\n"
            f"Do you need something else?")

    triggs = make_trigger_list(triggers)

    infos.edit(
        f"[md]Trigger of type `{sel_type}`:\n"
        f"{triggs}\n"
        "Please send the number of the trigger to delete",
        msg_id=infos.bot.waiting_data["msg"].message_id,
        reply_markup=keyboards.done(),
        parse=False)

    return wait_del_trigger_index
Пример #8
0
def wait_del_dialog_number(infos: Infos) -> Callable:
    if infos.is_callback_query:
        if infos.callback_query.data == "done":
            return to_menu(infos)

    to_delete: List[Dialog] = []

    section = infos.bot.waiting_data["section"]
    dialogs = mongo_interface.get_dialogs_of_section(infos.bot.bot_id, section,
                                                     infos.db.language)

    indexes: List[str] = infos.message.text.split("," if "," in
                                                  infos.message.text else " ")

    for string_index in indexes:
        try:
            string_index = string_index.strip()
            index = int(string_index)
        except ValueError:
            infos.reply(f"[md]`{string_index}` is not a valid number.")
            return wait_del_dialog_number

        if index <= 0:
            infos.reply("The minimum index is 1!")
            return wait_del_dialog_number

        if index - 1 > len(dialogs):
            infos.reply(f"You've selected dialog n°{index} but "
                        f"there are only {len(dialogs) + 1} dialogs")
            return wait_del_dialog_number

        dialog = dialogs[index - 1]
        to_delete.append(dialog)

    for dialog in to_delete:
        mongo_interface.delete_dialog(dialog)
        dialogs.remove(dialog)

    if not dialogs:
        msg = f"[md]No more dialogs for section `{section}`\nDo you need something else?"
        return to_menu(infos, msg)

    infos.edit(
        f"[md]Dialogs for section `{section}`:\n{make_dialogs_list(dialogs)}"
        f"\n\nPlease send the number of the dialog you want to delete.",
        reply_markup=keyboards.done(),
        msg_id=infos.bot.waiting_data["msg"].message_id,
        parse=False)

    return wait_del_dialog_number
Пример #9
0
def del_section(infos: Infos) -> Callable:
    # Waiting for a message (section)
    if not infos.is_callback_query:
        return del_section

    sections = mongo_interface.get_sections(infos.bot.bot_id,
                                            infos.db.language)

    if not sections:
        msg = f"I don't have any section\nDo you need something else?"
        return to_menu(infos, msg)

    msg = f"[md]Here's the list of my sections:\n" \
            f"\n{make_sections_list(infos)}\n" \
            f"\nPlease now send the section to delete\n" \
            f"*Remember that deleting a sections means deleting every message/trigger linked to it!!*"

    infos.edit(msg,
               reply_markup=keyboards.done(),
               msg_id=infos.bot.waiting_data["msg"].message_id,
               parse=False)

    return wait_del_section_number
Пример #10
0
def wait_add_dialog_reply(infos: Infos) -> Callable:
    # Here we can handle both text and callbacks
    if infos.is_callback_query:
        if infos.callback_query.data == "done":
            return to_menu(infos)

    if infos.message.is_sticker:
        reply = "{media:stk," + infos.message.sticker.stkid + "}"
    elif infos.message.is_photo:
        reply = "{media:pht," + infos.message.photo.phtid
        if infos.message.photo.caption:
            reply += "," + infos.message.photo.caption + "}"
        else:
            reply += "}"
    elif infos.message.is_audio:
        reply = "{media:aud," + infos.message.audio.audid + "}"
    elif infos.message.is_voice:
        reply = "{media:voe," + infos.message.voice.voiceid + "}"
    elif infos.message.is_document:
        reply = "{media:doc," + infos.message.document.docid + "}"
    elif infos.message.is_text:
        reply = infos.message.text
    else:
        infos.reply("Unsupported.")
        return wait_add_dialog_reply

    section = infos.bot.waiting_data["section"]

    reg = re.compile(r"!!\s*(\d)\s*(->|=>)\s*(.+)")
    match = reg.search(reply)
    if match:
        if not bot_utils.handle_add_reply_command(infos, match, section):
            return wait_add_dialog_reply
    else:
        probability, reply = regex_utils.get_dialog_probability(reply)
        reply = reply.replace("\\`", "`")
        if probability is None:
            probability = 100

        section = infos.bot.waiting_data["section"]

        dialog = Dialog(reply, section, infos.db.language, infos.bot.bot_id, 0,
                        probability)
        mongo_interface.add_dialog(dialog)

    dialogs = mongo_interface.get_dialogs_of_section(infos.bot.bot_id, section,
                                                     infos.db.language)

    # Final message to append
    f_msg = "Please send the replies you want!"
    if not dialogs:
        msg = f"[md]No dialogs for section `{section}`\n{f_msg}"
    else:
        dials = make_dialogs_list(dialogs)
        msg = f"[md]Dialogs for section `{section}`:\n{dials}\n{f_msg}"

    infos.edit(msg,
               reply_markup=keyboards.done(),
               msg_id=infos.bot.waiting_data["msg"].message_id,
               parse=False)

    return wait_add_dialog_reply