Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
0
    def _update_elaborator(self, update: dict):
        self.offset = update["update_id"] + 1
        infos = Infos(self, update)

        if infos.is_edited_message or infos.is_channel_post or infos.is_edited_channel_post:
            log.d(f"Ignoring update of type {infos.update_type}")
            return

        if not self._callback:
            self.waiting_data = {}

        if infos.user and not infos.message.is_command:
            if infos.user.is_bot_owner and self._callback:
                log.d(f"Calling callback {self._callback.__name__}")
                self._callback = self._callback(infos)
                return
        elif infos.user:
            if infos.user.is_bot_owner and self._callback:
                if infos.message.command == "cancel":
                    self._callback = None
                    infos.reply("Operation cancelled.")
                    return
            if infos.user.is_bot_owner and infos.message.command == "test":
                elaborator.elaborate_json_backup(infos)

        if infos.message.is_document:
            elaborator.elaborate_file(infos)
        elif infos.message.is_command:
            mongo_interface.increment_read_messages(self.bot_id)
            self._command_elaborator(infos)
        elif infos.is_callback_query:
            self._callback_elaborator(infos)
        elif infos.is_message:
            mongo_interface.increment_read_messages(self.bot_id)
            self._message_elaborator(infos)
Exemplo n.º 6
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
Exemplo n.º 7
0
def menu_dialogs(infos: Infos):
    if not infos.is_callback_query:
        return menu_triggers

    markup = None

    if infos.callback_query.data == "add_dialog":
        fun = add_dialog
        msg = "Please now send the dialog section"
    elif infos.callback_query.data == "del_dialog":
        fun = del_dialog
        msg = "Please now send the dialog section"
    elif infos.callback_query.data == "list_dialogs":
        fun = list_dialogs
        msg = "Please now send the dialog section"
    elif infos.callback_query.data == "menu_back":
        fun = menu_choice
        msg = f"Welcome {infos.user.name}, what do you need?"
        markup = keyboards.menu()
    else:
        infos.callback_query.answer("What...?")
        return menu_dialogs

    infos.edit(msg,
               msg_id=infos.bot.waiting_data["msg"].message_id,
               reply_markup=markup,
               parse=False)
    return fun
Exemplo n.º 8
0
def to_menu(infos: Infos, msg=None) -> Callable:
    infos.edit("Do you need something else" if not msg else msg,
               reply_markup=keyboards.menu(),
               msg_id=infos.bot.waiting_data["msg"].message_id,
               parse=False)
    # Reset waiting_data
    infos.bot.cancel_wait()

    return menu_choice
Exemplo n.º 9
0
def add_trigger(infos: Infos) -> Callable:
    if not infos.is_message:
        return add_trigger

    if not infos.message.is_text:
        return add_trigger

    infos.bot.waiting_data["section"] = infos.message.text
    infos.edit("Please now select the trigger type",
               msg_id=infos.bot.waiting_data["msg"].message_id,
               reply_markup=keyboards.trigger_type(),
               parse=False)

    return wait_trigger_type_add_reply
Exemplo n.º 10
0
def complete_dialog_sec(infos: Infos, section: str):
    log.d(f"Elaborating reply of section {section}")
    dialogs: List[Dialog] = mongo_interface.get_dialogs_of_section(
        infos.bot.bot_id, section, infos.db.language)

    if not dialogs:
        log.d(f"No dialogs set for section {section} lang {infos.db.language}")
        infos.bot.notify(
            f"No dialogs set for section {section} lang {infos.db.language}")
        return

    dialog = reply_parser.reply_choice(dialogs)

    infos.reply(dialog.reply, parse_mode=None)
    mongo_interface.increment_dialog_usages(dialog)
Exemplo n.º 11
0
def menu_delete_bot_phase2(infos: Infos):
    if not infos.is_callback_query:
        return menu_delete_bot_phase2

    if infos.callback_query.data == "delete_bot_yes":
        infos.edit("Deleting bot...\nThis can take some seconds...",
                   msg_id=infos.bot.waiting_data["msg"].message_id)
        bot_utils.delete_bot(infos)
        infos.edit("Bye...", msg_id=infos.bot.waiting_data["msg"].message_id)
        lotus_interface.detach_bot(infos.bot)

    if infos.callback_query.data == "delete_bot_no":
        return to_menu(infos, "I'm sure that you don't want to do it.")

    return menu_delete_bot_phase2
Exemplo n.º 12
0
def menu_delete_bot(infos: Infos):
    if not infos.is_callback_query:
        return menu_delete_bot

    if infos.callback_query.data == "delete_bot_yes":
        infos.edit(
            "[md]Please be REALLY sure, tap yes again if you are.\n\n*Every bot data will be lost!!*",
            msg_id=infos.bot.waiting_data["msg"].message_id,
            reply_markup=keyboards.delete_bot())
        return menu_delete_bot_phase2

    if infos.callback_query.data == "delete_bot_no":
        return to_menu(infos, "I'm sure that you don't want to do it.")

    return menu_delete_bot
Exemplo n.º 13
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
Exemplo n.º 14
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
Exemplo n.º 15
0
def menu_wait_command_symbol(infos: Infos):
    if infos.is_callback_query:
        if infos.callback_query.data == "cancel":
            return to_menu(infos,
                           "Operation cancelled\nDo you need something else?")

    if len(infos.message.text) != 1:
        infos.edit("Invalid symbol specified!\nPlease send just a symbol",
                   msg_id=infos.bot.waiting_data["msg"].message_id,
                   reply_markup=keyboards.cancel(),
                   parse=False)
        return menu_wait_command_symbol

    infos.bot.custom_command_symb = infos.message.text
    mongo_interface.update_bot(infos.bot.token, infos.bot)

    infos.edit(f"You want to edit some options, master?",
               msg_id=infos.bot.waiting_data["msg"].message_id,
               reply_markup=keyboards.menu_options(infos.bot),
               parse=False)
    return menu_options
Exemplo n.º 16
0
def menu_options(infos: Infos):
    if not infos.is_callback_query:
        return menu_options

    if infos.callback_query.data == "options_autom":
        infos.bot.automs_enabled = not infos.bot.automs_enabled
        mongo_interface.update_bot(infos.bot.token, infos.bot)
        infos.edit("Option changed!",
                   reply_markup=keyboards.menu_options(infos.bot),
                   parse=False)
        return menu_options

    if infos.callback_query.data == "options_comm_symbol":
        infos.edit("Please send a custom command symbol",
                   reply_markup=keyboards.cancel(),
                   parse=False)
        return menu_wait_command_symbol

    if infos.callback_query.data == "options_delete_bot":
        infos.edit(
            "Please be sure before doing this operation...\nTap yes if you're sure",
            reply_markup=keyboards.delete_bot())
        return menu_delete_bot

    if infos.callback_query.data == "options_back":
        return to_menu(infos)

    return menu_options
Exemplo n.º 17
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
Exemplo n.º 18
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
Exemplo n.º 19
0
def menu_choice(infos: Infos) -> Optional[Callable]:
    if not infos.is_callback_query:
        return menu_choice

    infos.bot.waiting_data["msg"] = infos.message

    if infos.callback_query.data == "menu_dialogs":
        infos.edit(f"Please choose an option",
                   reply_markup=keyboards.menu_dialogs(),
                   parse=False)
        return menu_dialogs

    if infos.callback_query.data == "menu_triggers":
        infos.edit(f"Please choose an option",
                   reply_markup=keyboards.menu_triggers(),
                   parse=False)
        return menu_triggers

    if infos.callback_query.data == "menu_sections":
        infos.edit(f"Please choose an option",
                   reply_markup=keyboards.menu_sections(),
                   parse=False)
        return menu_sections

    if infos.callback_query.data == "menu_options":
        infos.edit(f"You want to edit some options, master?",
                   reply_markup=keyboards.menu_options(infos.bot),
                   parse=False)
        return menu_options

    if infos.callback_query.data == "menu_close":
        infos.delete_message(infos.chat.cid, infos.message.message_id)
        return

    infos.callback_query.answer("What...?")
    return menu_choice
Exemplo n.º 20
0
def menu_sections(infos: Infos):
    if not infos.is_callback_query:
        return menu_triggers

    if infos.callback_query.data == "del_section":
        return del_section(infos)
    elif infos.callback_query.data == "list_sections":
        fun = menu_choice
        msg = f"[md]{make_sections_list(infos)}\n" \
            f"Do you need something else, {infos.user.name}?"
        markup = keyboards.menu()
    elif infos.callback_query.data == "menu_back":
        fun = menu_choice
        msg = f"Welcome {infos.user.name}, what do you need?"
        markup = keyboards.menu()
    else:
        infos.callback_query.answer("What...?")
        return menu_dialogs

    infos.edit(msg,
               msg_id=infos.bot.waiting_data["msg"].message_id,
               reply_markup=markup,
               parse=False)
    return fun
Exemplo n.º 21
0
def newbot(infos: Infos):

    bot_count = manager.get_user_bots_count(infos.user.uid)

    if infos.user.is_maker_owner:
        bot_count = 0

    if bot_count == 1:
        return infos.reply(f"You already have a bot..")
    if bot_count > 1:
        return infos.reply(f"You already have {bot_count} bot(s)")

    if not bot_utils.is_bot_token(infos.message.args[0]):
        return infos.reply(
            "{user.name} i think that this isn't a valid token...")

    infos.reply("Creating a new bot with this token...")
    ok = mongo_interface.register_bot(infos.message.args[0], infos.user.uid)
    if ok:
        infos.reply("Valid token! Your bot should be online now!")
    else:
        infos.reply("Something went wrong while registering the new bot...")
Exemplo n.º 22
0
def myid(infos: Infos):
    infos.reply("Your ID is: {uid}")
Exemplo n.º 23
0
def menu(infos: Infos) -> Callable:
    infos.reply(f"Welcome {infos.user.name}, what do you need?",
                markup=keyboards.menu())
    return menu_choice
def stop(infos: Infos):
    # lotus.stop()
    infos.reply("Stopped...")
Exemplo n.º 25
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