def main(): test_handler = CommandHandler("test", test) start_handler = CommandHandler("start", start, pass_args=True) IMDB_HANDLER = CommandHandler('imdb', imdb, pass_args=True) IMDB_SEARCHDATAHANDLER = CallbackQueryHandler(imdb_searchdata) start_callback_handler = CallbackQueryHandler(send_start, pattern=r"bot_start") dispatcher.add_handler(start_callback_handler) help_handler = CommandHandler("help", get_help) help_callback_handler = CallbackQueryHandler(help_button, pattern=r"help_") settings_handler = CommandHandler("settings", get_settings) settings_callback_handler = CallbackQueryHandler(settings_button, pattern=r"stngs_") source_handler = CommandHandler("source", source) migrate_handler = MessageHandler(Filters.status_update.migrate, migrate_chats) M_CONNECT_BTN_HANDLER = CallbackQueryHandler(m_connect_button, pattern=r"main_connect") # dispatcher.add_handler(test_handler) dispatcher.add_handler(start_handler) dispatcher.add_handler(help_handler) dispatcher.add_handler(settings_handler) dispatcher.add_handler(help_callback_handler) dispatcher.add_handler(settings_callback_handler) dispatcher.add_handler(migrate_handler) dispatcher.add_handler(source_handler) dispatcher.add_handler(M_CONNECT_BTN_HANDLER) dispatcher.add_handler(IMDB_HANDLER) dispatcher.add_handler(IMDB_SEARCHDATAHANDLER) # dispatcher.add_error_handler(error_callback) if WEBHOOK: LOGGER.info("Using webhooks.") updater.start_webhook(listen="127.0.0.1", port=PORT, url_path=TOKEN) if CERT_PATH: updater.bot.set_webhook(url=URL + TOKEN, certificate=open(CERT_PATH, 'rb')) else: updater.bot.set_webhook(url=URL + TOKEN) else: LOGGER.info("Cinderella running...") updater.start_polling(timeout=15, read_latency=4) if len(argv) not in (1, 3, 4): telethn.disconnect() else: telethn.run_until_disconnected() updater.idle()
def start(bot: Bot, update: Update, args: List[str]): LOGGER.info("Start") chat = update.effective_chat # type: Optional[Chat] #query = update.callback_query #Unused variable if update.effective_chat.type == "private": if len(args) >= 1: if args[0].lower() == "help": send_help(update.effective_chat.id, tld(chat.id, "send-help").format( dispatcher.bot.first_name, "" if not ALLOW_EXCL else tld( chat.id, "\nTüm komutlar `/` veya `!` ile kullanılabilir.\n" ))) elif args[0].lower().startswith("stngs_"): match = re.match("stngs_(.*)", args[0].lower()) chat = dispatcher.bot.getChat(match.group(1)) if is_user_admin(chat, update.effective_user.id): send_settings(match.group(1), update.effective_user.id, update, user=False) else: send_settings(match.group(1), update.effective_user.id, update, user=True) elif args[0][1:].isdigit() and "rules" in IMPORTED: IMPORTED["rules"].send_rules(update, args[0], from_pm=True) elif args[0].lower() == "controlpanel": control_panel(bot, update) else: send_start(bot, update) else: update.effective_message.reply_text("yaşıyorum")
def __list_all_modules(): from os.path import dirname, basename, isfile import glob # This generates a list of modules in this folder for the * in __main__ to work. mod_paths = glob.glob(dirname(__file__) + "/*.py") all_modules = [ basename(f)[:-3] for f in mod_paths if isfile(f) and f.endswith(".py") and not f.endswith('__init__.py') ] if LOAD or NO_LOAD: to_load = LOAD if to_load: if not all( any(mod == module_name for module_name in all_modules) for mod in to_load): LOGGER.error("Invalid loadorder names. Quitting.") quit(1) else: to_load = all_modules if NO_LOAD: LOGGER.info("Not loading: {}".format(NO_LOAD)) return [item for item in to_load if item not in NO_LOAD] return to_load return all_modules
def shellExecute(bot: Bot, update: Update): cmd = update.message.text.split(' ', maxsplit=1) if len(cmd) == 1: sendMessage("No command provided!", bot, update) return LOGGER.info(cmd) output = shell(cmd[1]) if output[1].decode(): LOGGER.error(f"Shell: {output[1].decode()}") if len(output[0].decode()) > 4000: with open("shell.txt", 'w') as f: f.write(f"Output\n-----------\n{output[0].decode()}\n") if output[1]: f.write(f"STDError\n-----------\n{output[1].decode()}\n") with open("shell.txt", 'rb') as f: bot.send_document(document=f, filename=f.name, reply_to_message_id=update.message.message_id, chat_id=update.message.chat_id) else: if output[1].decode(): sendMessage(f"<code>{output[1].decode()}</code>", bot, update) return else: sendMessage(f"<code>{output[0].decode()}</code>", bot, update)
def main(): test_handler = CommandHandler("test", test) start_handler = CommandHandler("start", start, pass_args=True) help_handler = CommandHandler("help", get_help) help_callback_handler = CallbackQueryHandler(help_button, pattern=r"help_") start_callback_handler = CallbackQueryHandler(send_start, pattern=r"bot_start") dispatcher.add_handler(start_callback_handler) cntrl_panel = CommandHandler("controlpanel", control_panel) cntrl_panel_callback_handler = CallbackQueryHandler(control_panel, pattern=r"cntrl_panel") dispatcher.add_handler(cntrl_panel_callback_handler) dispatcher.add_handler(cntrl_panel) settings_handler = CommandHandler("settings", get_settings) settings_callback_handler = CallbackQueryHandler(settings_button, pattern=r"stngs_") migrate_handler = MessageHandler(Filters.status_update.migrate, migrate_chats) # dispatcher.add_handler(test_handler) dispatcher.add_handler(start_handler) dispatcher.add_handler(help_handler) dispatcher.add_handler(settings_handler) dispatcher.add_handler(help_callback_handler) dispatcher.add_handler(settings_callback_handler) dispatcher.add_handler(migrate_handler) # dispatcher.add_error_handler(error_callback) # add antiflood processor Dispatcher.process_update = process_update if WEBHOOK: LOGGER.info("Web kancalarını kullanma.") updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) if CERT_PATH: updater.bot.set_webhook(url=URL + TOKEN, certificate=open(CERT_PATH, 'rb')) else: updater.bot.set_webhook(url=URL + TOKEN) else: LOGGER.info("Uzun yoklama kullanma..") updater.start_polling(timeout=15, read_latency=4) updater.idle()
def migrate_chats(bot: Bot, update: Update): msg = update.effective_message # type: Optional[Message] if msg.migrate_to_chat_id: old_chat = update.effective_chat.id new_chat = msg.migrate_to_chat_id elif msg.migrate_from_chat_id: old_chat = msg.migrate_from_chat_id new_chat = update.effective_chat.id else: return LOGGER.info("Migrating from %s, to %s", str(old_chat), str(new_chat)) for mod in MIGRATEABLE: mod.__migrate__(old_chat, new_chat) LOGGER.info("Successfully migrated!") raise DispatcherHandlerStop
dispatcher.add_handler(IMDB_HANDLER) dispatcher.add_handler(IMDB_SEARCHDATAHANDLER) # dispatcher.add_error_handler(error_callback) if WEBHOOK: LOGGER.info("Using webhooks.") updater.start_webhook(listen="127.0.0.1", port=PORT, url_path=TOKEN) if CERT_PATH: updater.bot.set_webhook(url=URL + TOKEN, certificate=open(CERT_PATH, 'rb')) else: updater.bot.set_webhook(url=URL + TOKEN) else: LOGGER.info("Goin GOD-MODE") updater.start_polling(timeout=15, read_latency=4) if len(argv) not in (1, 3, 4): telethn.disconnect() else: telethn.run_until_disconnected() updater.idle() if __name__ == '__main__': LOGGER.info("Successfully loaded modules: %s", str(ALL_MODULES)) telethn.start(bot_token=TOKEN) main()
def send(msg, bot, update): LOGGER.info("OUT: '{}'".format(msg)) bot.send_message(chat_id=update.effective_chat.id, text="`{}`".format(msg), parse_mode=ParseMode.MARKDOWN)
def log_input(update): user = update.effective_user.id chat = update.effective_chat.id LOGGER.info("IN: {} (user={}, chat={})".format( update.effective_message.text, user, chat))
mod_paths = glob.glob(dirname(__file__) + "/*.py") all_modules = [ basename(f)[:-3] for f in mod_paths if isfile(f) and f.endswith(".py") and not f.endswith('__init__.py') ] if LOAD or NO_LOAD: to_load = LOAD if to_load: if not all( any(mod == module_name for module_name in all_modules) for mod in to_load): LOGGER.error("Invalid loadorder names. Quitting.") quit(1) else: to_load = all_modules if NO_LOAD: LOGGER.info("Not loading: {}".format(NO_LOAD)) return [item for item in to_load if item not in NO_LOAD] return to_load return all_modules ALL_MODULES = sorted(__list_all_modules()) LOGGER.info("Modules to load: %s", str(ALL_MODULES)) __all__ = ALL_MODULES + ["ALL_MODULES"]
def control_panel(bot, update): LOGGER.info("Control panel") chat = update.effective_chat user = update.effective_user # ONLY send help in PM if chat.type != chat.PRIVATE: update.effective_message.reply_text("Kontrol paneline erişmek için PM'de bana ulaşın.", reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text="Control Panel", url=f"t.me/{bot.username}?start=controlpanel")]])) return #Support to run from command handler query = update.callback_query if query: query.message.delete() M_match = re.match(r"cntrl_panel_M", query.data) U_match = re.match(r"cntrl_panel_U", query.data) G_match = re.match(r"cntrl_panel_G", query.data) back_match = re.match(r"help_back", query.data) LOGGER.info(query.data) else: M_match = "NightCrew En iyi bottur" #LMAO, don't uncomment if M_match: text = "*Control panel* 🛠" keyboard = [[InlineKeyboardButton(text="👤 Ayarlarım", callback_data="cntrl_panel_U(1)")]] #Show connected chat and add chat settings button conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chatG = bot.getChat(conn) #admin_list = chatG.get_administrators() #Unused variable #If user admin member = chatG.get_member(user.id) if member.status in ('administrator', 'creator'): text += f"\nBağlı sohbet - *{chatG.title}* (you {member.status})" keyboard += [[InlineKeyboardButton(text="👥 Grup Ayarları", callback_data="cntrl_panel_G_back")]] elif user.id in SUDO_USERS: text += f"\nBağlı sohbet - *{chatG.title}* (you sudo)" keyboard += [[InlineKeyboardButton(text="👥 Grup Ayarları (SUDO)", callback_data="cntrl_panel_G_back")]] else: text += f"\nBağlı sohbet - *{chatG.title}* (bir yönetici değilsiniz!)" else: text += "\nBağlı sohbet yok!" keyboard += [[InlineKeyboardButton(text="Geri", callback_data="bot_start")]] update.effective_message.reply_text(text, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode=ParseMode.MARKDOWN) elif U_match: mod_match = re.match(r"cntrl_panel_U_module\((.+?)\)", query.data) back_match = re.match(r"cntrl_panel_U\((.+?)\)", query.data) chatP = update.effective_chat # type: Optional[Chat] if mod_match: module = mod_match.group(1) R = CHAT_SETTINGS[module].__user_settings__(bot, update, user) text = " *{}* modülü için aşağıdaki ayarlara sahipsiniz:\n\n".format( CHAT_SETTINGS[module].__mod_name__) + R[0] keyboard = R[1] keyboard += [[InlineKeyboardButton(text="Back", callback_data="cntrl_panel_U(1)")]] query.message.reply_text(text=text, arse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(keyboard)) elif back_match: text = "*Kullanıcı kontrol paneli* 🛠" query.message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(paginate_modules(user.id, 0, USER_SETTINGS, "cntrl_panel_U"))) elif G_match: mod_match = re.match(r"cntrl_panel_G_module\((.+?)\)", query.data) prev_match = re.match(r"cntrl_panel_G_prev\((.+?)\)", query.data) next_match = re.match(r"cntrl_panel_G_next\((.+?)\)", query.data) back_match = re.match(r"cntrl_panel_G_back", query.data) chatP = chat conn = connected(bot, update, chat, user.id) if not conn == False: chat = bot.getChat(conn) else: query.message.reply_text(text="Sohbet bağlantısında hata") exit(1) if mod_match: module = mod_match.group(1) R = CHAT_SETTINGS[module].__chat_settings__(bot, update, chat, chatP, user) if type(R) is list: text = R[0] keyboard = R[1] else: text = R keyboard = [] text = "*{}*,*{}* modülü için şu ayarlara sahiptir:\n\n".format( escape_markdown(chat.title), CHAT_SETTINGS[module].__mod_name__) + text keyboard += [[InlineKeyboardButton(text="Geri", callback_data="cntrl_panel_G_back")]] query.message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(keyboard)) elif prev_match: chat_id = prev_match.group(1) curr_page = int(prev_match.group(2)) chat = bot.get_chat(chat_id) query.message.reply_text(tld(user.id, "grup-ayarları gönder").format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(curr_page - 1, 0, CHAT_SETTINGS, "cntrl_panel_G", chat=chat_id))) elif next_match: chat_id = next_match.group(1) next_page = int(next_match.group(2)) chat = bot.get_chat(chat_id) query.message.reply_text(tld(user.id, "grup-ayarları gönder").format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(next_page + 1, 0, CHAT_SETTINGS, "cntrl_panel_G", chat=chat_id))) elif back_match: text = "Test" query.message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(paginate_modules(user.id, 0, CHAT_SETTINGS, "cntrl_panel_G")))
for handler in (x for x in self.handlers[group] if x.check_update(update)): handler.handle_update(update, self) break # Stop processing with any other handler. except DispatcherHandlerStop: self.logger.debug('DispatcherHandlerStop nedeniyle diğer işleyicileri durdurma') break # Dispatch any error. except TelegramError as te: self.logger.warning('Güncelleme işlenirken bir Telegram Hatası oluştu') try: self.dispatch_error(update, te) except DispatcherHandlerStop: self.logger.debug('Hata işleyici diğer işleyicileri durdurdu') break except Exception: self.logger.exception('Hatayı işlerken yakalanmamış bir hata ortaya çıktı') # Errors should not stop the thread. except Exception: self.logger.exception('Güncelleme işlenirken yakalanmamış bir hata ortaya çıktı') if __name__ == '__main__': LOGGER.info("Başarıyla yüklenen modüller: " + str(ALL_MODULES)) LOGGER.info("Başarıyla yüklendi") main()