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) 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("Megumin running...") 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
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))
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("Megumin running...") updater.start_polling(timeout=15, read_latency=4) updater.idle() if __name__ == '__main__': LOGGER.info("Successfully loaded modules: " + str(ALL_MODULES)) main()