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)
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)
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...")
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
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
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
def menu(infos: Infos) -> Callable: infos.reply(f"Welcome {infos.user.name}, what do you need?", markup=keyboards.menu()) return menu_choice
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
def stop(infos: Infos): # lotus.stop() infos.reply("Stopped...")
def myid(infos: Infos): infos.reply("Your ID is: {uid}")