async def help_menu(_, m: Message): if len(m.text.split()) >= 2: help_option = (m.text.split(None, 1)[1]).lower() help_msg, help_kb = await get_help_msg(m, help_option) if not help_msg: LOGGER.error( f"No help_msg found for help_option - {help_option}!!") return LOGGER.info( f"{m.from_user.id} fetched help for '{help_option}' text in {m.chat.id}", ) if m.chat.type == "private": await m.reply_text( help_msg, parse_mode="markdown", reply_markup=ikb(help_kb), quote=True, disable_web_page_preview=True, ) else: await m.reply_text( (tlang(m, "start.public_help").format(help_option=help_option)), reply_markup=ikb([ [ ( "Help", f"t.me/{Config.BOT_USERNAME}?start={help_option}", "url", ), ], ], ), ) else: if m.chat.type == "private": keyboard = ikb([ *(await gen_cmds_kb(m)), [(f"« {(tlang(m, 'general.back_btn'))}", "start_back")], ], ) msg = tlang(m, "general.commands_available") else: keyboard = ikb([[ ("Help", f"t.me/{Config.BOT_USERNAME}?start=help", "url") ]], ) msg = tlang(m, "start.pm_for_help") await m.reply_text( msg, reply_markup=keyboard, ) return
async def set_lang(_, m: Message): args = m.text.split() if len(args) > 2: await m.reply_text(tlang(m, "langs.correct_usage")) return if len(args) == 2: lang_code = args[1] avail_langs = set(lang_dict.keys()) if lang_code not in avail_langs: await m.reply_text( f"Please choose a valid language code from: {', '.join(avail_langs)}", ) return Langs(m.chat.id).set_lang(lang_code) LOGGER.info( f"{m.from_user.id} change language to {lang_code} in {m.chat.id}") await m.reply_text( f"🌐 {((tlang(m, 'langs.changed')).format(lang_code=lang_code))}", ) return await m.reply_text( (tlang(m, "langs.changelang")), reply_markup=ikb(await gen_langs_kb()), ) return
async def gen_start_kb(q: Message or CallbackQuery): """Generate keyboard with start menu options.""" from alita import BOT_USERNAME return ikb( [ [ ( f"➕ {(tlang(q, 'start.add_chat_btn'))}", f"https://t.me/{BOT_USERNAME}?startgroup=new", "url", ), ( f"{(tlang(q, 'start.support_group'))} 👥", f"https://t.me/{SUPPORT_GROUP}", "url", ), ], [(f"📚 {(tlang(q, 'start.commands_btn'))}", "commands")], [ (f"🌐 {(tlang(q, 'start.language_btn'))}", "chlang"), ( f"🗃️ {(tlang(q, 'start.source_code'))}", "https://github.com/DivideProjects/Alita_Robot", "url", ), ], ], )
async def paste_it(_, m: Message): replymsg = await m.reply_text((tlang(m, "utils.paste.pasting")), quote=True) try: if m.reply_to_message: if m.reply_to_message.document: dl_loc = await m.reply_to_message.download() with open(dl_loc) as f: txt = f.read() remove(dl_loc) else: txt = m.reply_to_message.text else: txt = m.text.split(None, 1)[1] ur = "https://hastebin.com/documents" r = await http.post(ur, json={"content": txt}) url = f"https://hastebin.com/{r.json().get('key')}" await replymsg.edit_text( (tlang(m, "utils.paste.pasted_nekobin")), reply_markup=ikb([[((tlang(m, "utils.paste.nekobin_btn")), url, "url")]]), ) LOGGER.info(f"{m.from_user.id} used paste cmd in {m.chat.id}") except Exception as e: await replymsg.edit_text(f"Error: {e}") return return
async def unpinall_message(_, m: Message): await m.reply_text( "Do you really want to unpin all messages in this chat?", reply_markup=ikb([[("Yes", "unpin_all_in_this_chat"), ("No", "close_admin")]]), ) return
async def get_formatting_info(_, q: CallbackQuery): cmd = q.data.split(".")[1] kb = ikb([[((tlang(q, "general.back_btn")), "back.formatting")]]) if cmd == "md_formatting": await q.message.edit_text( tlang(q, "formatting.md_help"), reply_markup=kb, parse_mode="html", ) elif cmd == "fillings": await q.message.edit_text( tlang(q, "formatting.filling_help"), reply_markup=kb, parse_mode="html", ) elif cmd == "random_content": await q.message.edit_text( tlang(q, "formatting.random_help"), reply_markup=kb, parse_mode="html", ) await q.answer() return
async def gen_formatting_kb(m): return ikb([ [ ("Markdown Formatting", "formatting.md_formatting"), ("Fillings", "formatting.fillings"), ], [("Random Content", "formatting.random_content")], [(("« " + (tlang(m, "general.back_btn"))), "commands")], ], )
async def chlang_callback(_, q: CallbackQuery): kb = await gen_langs_kb() kb.append([(f"« {(tlang(q, 'general.back_btn'))}", "start_back")]) await q.message.edit_text( (tlang(q, "langs.changelang")), reply_markup=ikb(kb), ) await q.answer() return
async def rm_allfilters(_, m: Message): all_bls = db.get_all_filters(m.chat.id) if not all_bls: return await m.reply_text("No filters to stop in this chat.") return await m.reply_text( "Are you sure you want to clear all filters?", reply_markup=ikb([[("⚠️ Confirm", "rm_allfilters"), ("❌ Cancel", "close_admin")]], ), )
async def clear_allnote(_, m: Message): all_notes = {i[0] for i in db.get_all_notes(m.chat.id)} if not all_notes: await m.reply_text("No notes are there in this chat") return await m.reply_text( "Are you sure you want to clear all notes?", reply_markup=ikb([[("⚠️ Confirm", "clear_notes"), ("❌ Cancel", "close_admin")]], ), ) return
async def get_rules(_, m: Message): db = Rules(m.chat.id) msg_id = m.reply_to_message.message_id if m.reply_to_message else m.message_id rules = db.get_rules() LOGGER.info(f"{m.from_user.id} fetched rules in {m.chat.id}") if m and not m.from_user: return if not rules: await m.reply_text( (tlang(m, "rules.no_rules")), quote=True, ) return priv_rules_status = db.get_privrules() if priv_rules_status: from alita import BOT_USERNAME pm_kb = ikb( [ [ ( "Rules", f"https://t.me/{BOT_USERNAME}?start=rules_{m.chat.id}", "url", ), ], ], ) await m.reply_text( (tlang(m, "rules.pm_me")), quote=True, reply_markup=pm_kb, reply_to_message_id=msg_id, ) return formated = rules await m.reply_text( (tlang(m, "rules.get_rules")).format( chat=f"<b>{m.chat.title}</b>", rules=formated, ), disable_web_page_preview=True, reply_to_message_id=msg_id, ) return
async def unapproveall_users(_, m: Message): db = Approve(m.chat.id) all_approved = db.list_approved() if not all_approved: await m.reply_text("No one is approved in this chat.") return await m.reply_text( "Are you sure you want to remove everyone who is approved in this chat?", reply_markup=ikb([[("⚠️ Confirm", "unapprove_all"), ("❌ Cancel", "close_admin")]], ), ) return
async def rm_allblacklist(_, m: Message): db = Blacklist(m.chat.id) all_bls = db.get_blacklists() if not all_bls: await m.reply_text("No notes are blacklists in this chat") return await m.reply_text( "Are you sure you want to clear all blacklists?", reply_markup=ikb( [[("⚠️ Confirm", "rm_allblacklist"), ("❌ Cancel", "close_admin")]], ), ) return
async def clear_rules(_, m: Message): db = Rules(m.chat.id) if m and not m.from_user: return rules = db.get_rules() if not rules: await m.reply_text(tlang(m, "rules.no_rules")) return await m.reply_text( (tlang(m, "rules.clear_rules")), reply_markup=ikb([[("⚠️ Confirm", "clear_rules"), ("❌ Cancel", "close_admin")]], ), ) return
async def perma_pin(_, m: Message): if m.reply_to_message or len(m.text.split()) > 1: LOGGER.info(f"{m.from_user.id} used permampin in {m.chat.id}") if m.reply_to_message: text = m.reply_to_message.text elif len(m.text.split()) > 1: text = m.text.split(None, 1)[1] teks, button = await parse_button(text) button = await build_keyboard(button) button = ikb(button) if button else None z = await m.reply_text(teks, reply_markup=button) await z.pin() else: await m.reply_text("Reply to a message or enter text to pin it.") await m.delete() return
async def set_lang_callback(_, q: CallbackQuery): lang_code = q.data.split(".")[1] Langs(q.message.chat.id).set_lang(lang_code) await sleep(0.1) if q.message.chat.type == "private": keyboard = ikb([[(f"« {(tlang(q, 'general.back_btn'))}", "start_back")] ]) else: keyboard = None await q.message.edit_text( f"🌐 {((tlang(q, 'langs.changed')).format(lang_code=lang_code))}", reply_markup=keyboard, ) await q.answer() return
async def get_module_info(_, q: CallbackQuery): module = q.data.split(".", 1)[1] help_msg = f"**{(tlang(q, str(module)))}:**\n\n" + tlang( q, HELP_COMMANDS[module]["help_msg"], ) help_kb = HELP_COMMANDS[module]["buttons"] + [ [("« " + (tlang(q, "general.back_btn")), "commands")], ] await q.message.edit_text( help_msg, parse_mode="markdown", reply_markup=ikb(help_kb), ) await q.answer() return
async def commands_menu(_, q: CallbackQuery): keyboard = ikb([ *(await gen_cmds_kb(q)), [(f"« {(tlang(q, 'general.back_btn'))}", "start_back")], ], ) try: await q.message.edit_text( (tlang(q, "general.commands_available")), reply_markup=keyboard, ) except MessageNotModified: pass except QueryIdInvalid: await q.message.reply_text( (tlang(q, "general.commands_available")), reply_markup=keyboard, ) await q.answer() return
async def start(c: Alita, m: Message): if m.chat.type == "private": if len(m.text.split()) > 1: help_option = (m.text.split(None, 1)[1]).lower() if help_option.startswith("note") and ( help_option not in ("note", "notes") ): await get_private_note(c, m, help_option) return if help_option.startswith("rules"): LOGGER.info(f"{m.from_user.id} fetched privaterules in {m.chat.id}") await get_private_rules(c, m, help_option) return help_msg, help_kb = await get_help_msg(m, help_option) if not help_msg: return await m.reply_text( help_msg, parse_mode="markdown", reply_markup=ikb(help_kb), quote=True, disable_web_page_preview=True, ) return try: await m.reply_text( (tlang(m, "start.private")), reply_markup=(await gen_start_kb(m)), quote=True, disable_web_page_preview=True, ) except UserIsBlocked: LOGGER.warning(f"Bot blocked by {m.from_user.id}") else: await m.reply_text( (tlang(m, "start.group")), quote=True, ) return
async def local_notes(_, m: Message): LOGGER.info(f"{m.from_user.id} listed all notes in {m.chat.id}") getnotes = db.get_all_notes(m.chat.id) if not getnotes: await m.reply_text(f"There are no notes in <b>{m.chat.title}</b>.") return msg_id = m.reply_to_message.message_id if m.reply_to_message else m.message_id curr_pref = db_settings.get_privatenotes(m.chat.id) if curr_pref: from alita import BOT_USERNAME pm_kb = ikb([ [ ( "All Notes", f"https://t.me/{BOT_USERNAME}?start=notes_{m.chat.id}", "url", ), ], ], ) await m.reply_text( "Click on the button below to get notes!", quote=True, reply_markup=pm_kb, ) return rply = f"Notes in <b>{m.chat.title}</b>:\n" for x in getnotes: rply += f"-> <code>#{x[0]}</code>\n" rply += "\nYou can get a note by #notename or <code>/get notename</code>" await m.reply_text(rply, reply_to_message_id=msg_id) return
async def report_watcher(c: Alita, m: Message): if m.chat.type != "supergroup": return if not m.from_user: return me = await c.get_me() db = Reporting(m.chat.id) if (m.chat and m.reply_to_message) and (db.get_settings()): reported_msg_id = m.reply_to_message.message_id reported_user = m.reply_to_message.from_user chat_name = m.chat.title or m.chat.username admin_list = await c.get_chat_members(m.chat.id, filter="administrators") if reported_user.id == me.id: await m.reply_text("Nice try.") return if reported_user.id in SUPPORT_STAFF: await m.reply_text("Uh? You reporting my support team?") return if m.chat.username: msg = ( f"<b>⚠️ Report: </b>{m.chat.title}\n" f"<b> • Report by:</b> {(await mention_html(m.from_user.first_name, m.from_user.id))} (<code>{m.from_user.id}</code>)\n" f"<b> • Reported user:</b> {(await mention_html(reported_user.first_name, reported_user.id))} (<code>{reported_user.id}</code>)\n" ) else: msg = f"{(await mention_html(m.from_user.first_name, m.from_user.id))} is calling for admins in '{chat_name}'!\n" link_chat_id = str(m.chat.id).replace("-100", "") link = f"https://t.me/c/{link_chat_id}/{reported_msg_id}" # message link reply_markup = ikb([ [("➡ Message", link, "url")], [ ( "⚠ Kick", f"report_{m.chat.id}=kick={reported_user.id}={reported_msg_id}", ), ( "⛔️ Ban", f"report_{m.chat.id}=ban={reported_user.id}={reported_msg_id}", ), ], [ ( "❎ Delete Message", f"report_{m.chat.id}=del={reported_user.id}={reported_msg_id}", ), ], ], ) LOGGER.info( f"{m.from_user.id} reported msgid-{m.reply_to_message.message_id} to admins in {m.chat.id}", ) await m.reply_text( (f"{(await mention_html(m.from_user.first_name, m.from_user.id))} " "reported the message to the admins."), quote=True, ) for admin in admin_list: if (admin.user.is_bot or admin.user.is_deleted ): # can't message bots or deleted accounts continue if Reporting(admin.user.id).get_settings(): try: await c.send_message( admin.user.id, msg, reply_markup=reply_markup, disable_web_page_preview=True, ) try: await m.reply_to_message.forward(admin.user.id) if len(m.text.split()) > 1: await m.forward(admin.user.id) except Exception: pass except Exception: pass except RPCError as ef: LOGGER.error(ef) LOGGER.error(format_exc()) return ""
async def get_private_note(c: Alita, m: Message, help_option: str): """Get the note in pm of user, with parsing enabled.""" from alita import BOT_USERNAME help_lst = help_option.split("_") if len(help_lst) == 2: chat_id = int(help_lst[1]) all_notes = notes_db.get_all_notes(chat_id) chat_title = Chats.get_chat_info(chat_id)["chat_name"] rply = f"Notes in {chat_title}:\n\n" note_list = [ f"- [{note[0]}](https://t.me/{BOT_USERNAME}?start=note_{chat_id}_{note[1]})" for note in all_notes ] rply = "\n".join(note_list) rply += "You can retrieve these notes by tapping on the notename." await m.reply_text(rply, disable_web_page_preview=True, quote=True) return if len(help_lst) != 3: return note_hash = help_option.split("_")[2] getnotes = notes_db.get_note_by_hash(note_hash) if not getnotes: await m.reply_text("Note does not exist", quote=True) return msgtype = getnotes["msgtype"] if not msgtype: await m.reply_text( "<b>Error:</b> Cannot find a type for this note!!", quote=True, ) return try: # support for random notes texts splitter = "%%%" note_reply = getnotes["note_value"].split(splitter) note_reply = choice(note_reply) except KeyError: note_reply = "" parse_words = [ "first", "last", "fullname", "username", "id", "chatname", "mention", ] text = await escape_mentions_using_curly_brackets(m, note_reply, parse_words) if msgtype == Types.TEXT: teks, button = await parse_button(text) button = await build_keyboard(button) button = ikb(button) if button else None if button: try: await m.reply_text( teks, reply_markup=button, disable_web_page_preview=True, quote=True, ) return except RPCError as ef: await m.reply_text( "An error has occured! Cannot parse note.", quote=True, ) LOGGER.error(ef) LOGGER.error(format_exc()) return else: await m.reply_text(teks, quote=True, disable_web_page_preview=True) return elif msgtype in ( Types.STICKER, Types.VIDEO_NOTE, Types.CONTACT, Types.ANIMATED_STICKER, ): await (await send_cmd(c, msgtype))(m.chat.id, getnotes["fileid"]) else: if getnotes["note_value"]: teks, button = await parse_button(getnotes["note_value"]) button = await build_keyboard(button) button = ikb(button) if button else None else: teks = "" button = None if button: try: await (await send_cmd(c, msgtype))( m.chat.id, getnotes["fileid"], caption=teks, reply_markup=button, ) return except RPCError as ef: await m.reply_text( teks, quote=True, reply_markup=button, disable_web_page_preview=True, ) LOGGER.error(ef) LOGGER.error(format_exc()) return else: await (await send_cmd(c, msgtype))( m.chat.id, getnotes["fileid"], caption=teks, ) LOGGER.info( f"{m.from_user.id} fetched privatenote {note_hash} (type - {getnotes}) in {m.chat.id}", ) return
async def get_note_func(c: Alita, m: Message, note_name, priv_notes_status): """Get the note in normal mode, with parsing enabled.""" reply_text = m.reply_to_message.reply_text if m.reply_to_message else m.reply_text reply_msg_id = m.reply_to_message.message_id if m.reply_to_message else m.message_id if m and not m.from_user: return if priv_notes_status: from alita import BOT_USERNAME note_hash = next(i[1] for i in db.get_all_notes(m.chat.id) if i[0] == note_name) await reply_text( f"Click on the button to get the note <code>{note_name}</code>", reply_markup=ikb([ [ ( "Click Me!", f"https://t.me/{BOT_USERNAME}?start=note_{m.chat.id}_{note_hash}", "url", ), ], ], ), ) return getnotes = db.get_note(m.chat.id, note_name) msgtype = getnotes["msgtype"] if not msgtype: await reply_text("<b>Error:</b> Cannot find a type for this note!!") return try: # support for random notes texts splitter = "%%%" note_reply = getnotes["note_value"].split(splitter) note_reply = choice(note_reply) except KeyError: note_reply = "" parse_words = [ "first", "last", "fullname", "id", "username", "mention", "chatname", ] text = await escape_mentions_using_curly_brackets(m, note_reply, parse_words) teks, button = await parse_button(text) button = await build_keyboard(button) button = InlineKeyboardMarkup(button) if button else None textt = teks try: if msgtype == Types.TEXT: if button: try: await reply_text( textt, # parse_mode="markdown", reply_markup=button, disable_web_page_preview=True, quote=True, ) return except RPCError as ef: await reply_text( "An error has occured! Cannot parse note.", quote=True, ) LOGGER.error(ef) LOGGER.error(format_exc()) return else: await reply_text( textt, # parse_mode="markdown", quote=True, disable_web_page_preview=True, ) return elif msgtype in ( Types.STICKER, Types.VIDEO_NOTE, Types.CONTACT, Types.ANIMATED_STICKER, ): await (await send_cmd(c, msgtype))( m.chat.id, getnotes["fileid"], reply_markup=button, reply_to_message_id=reply_msg_id, ) elif button: try: await (await send_cmd(c, msgtype))( m.chat.id, getnotes["fileid"], caption=textt, # parse_mode="markdown", reply_markup=button, reply_to_message_id=reply_msg_id, ) return except RPCError as ef: await m.reply_text( textt, # parse_mode="markdown", reply_markup=button, disable_web_page_preview=True, reply_to_message_id=reply_msg_id, ) LOGGER.error(ef) LOGGER.error(format_exc()) return else: await (await send_cmd(c, msgtype))( m.chat.id, getnotes["fileid"], caption=textt, # parse_mode="markdown", reply_markup=button, reply_to_message_id=reply_msg_id, ) LOGGER.info( f"{m.from_user.id} fetched note {note_name} (type - {getnotes}) in {m.chat.id}", ) except Exception as e: await m.reply_text(f"Error: {e}") return