def decrease(update: Update, context: CallbackContext): msg = update.effective_message user1 = update.effective_user chat = update.effective_chat if not settings.chat_should_reputate(chat.id): return if msg.reply_to_message: user2 = msg.reply_to_message.from_user if not settings.user_should_reputate(user1.id): msg.reply_text( "You have opted out of reputations, so you are not able to change others reputation neither." ) return if not settings.user_should_reputate(user2.id): msg.reply_text(f"{user2.full_name} opted out of reputations," f" so you are not able to change their reputation.") return if user1.id != user2.id: LOGGER.debug( f"{user2.id} : {sql.get_reputation(chat.id, user2.id)}") sql.decrease_reputation(chat.id, user2.id) new_msg = msg.reply_text( f"<b>{user1.first_name}</b> ({sql.get_reputation(chat.id, user1.id)}) has decreased reputation of <b>{user2.first_name}</b> ({sql.get_reputation(chat.id, user2.id)})", parse_mode=ParseMode.HTML).message_id try: context.bot.delete_message(chat.id, sql.get_latest_rep_message(chat.id)) except BadRequest as err: LOGGER.debug("Could not delete that message.") sql.set_latest_rep_message(chat.id, new_msg)
def add_warn_filter(update: Update, context: CallbackContext): LOGGER.debug("WTH") chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] args = msg.text.split( None, 1) # use python's maxsplit to separate Cmd, keyword, and reply_text if len(args) < 2: return extracted = split_quotes(args[1]) if len(extracted) >= 2: # set trigger -> lower, so as to avoid adding duplicate filters with different cases keyword = extracted[0].lower() content = extracted[1] else: return # Note: perhaps handlers can be removed somehow using sql.get_chat_filters for handler in dispatcher.handlers.get(WARN_HANDLER_GROUP, []): if handler.filters == (keyword, chat.id): dispatcher.remove_handler(handler, WARN_HANDLER_GROUP) sql.add_warn_filter(chat.id, keyword, content) update.effective_message.reply_text( "Warn handler added for '{}'!".format(keyword)) # MSG_FILTER_ADDED raise DispatcherHandlerStop
def load_api(): if is_module_loaded("rest"): import tg_bot.restapi as restapi LOGGER.debug("Loading API...") LOGGER.warning("BE CAREFULLY!") LOGGER.warning("Rest API is still in early development and considered unstable. Only enable it if you " "really know what you're doing. You have been warned.") p = Process(target=restapi.app.run()) p.start() p.join() else: LOGGER.debug("Not loading API")
def spam_filter(bot: Bot, update: Update): """ Filter messages with spam into message's text """ msg = update.effective_message.text message_entities = update.effective_message.parse_entities() message_caption_entities = update.effective_message.parse_caption_entities() found = False for descriptor, entity in message_entities.items(): LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}") if descriptor["type"] in FORBIDDEN_ENTITY_TYPES: found = True if found: spam_action(update)
def main(): LOGGER.debug(os.getcwd()) # test_handler = CommandHandler("test", test) start_handler = CommandHandler("start", start) help_handler = CommandHandler("help", get_help) help_callback_handler = CallbackQueryHandler(help_button, pattern=r"help_") settings_handler = CommandHandler("settings", get_settings) about_handler = CommandHandler("about", about, pass_args=True) 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_handler(about_handler) # dispatcher.add_error_handler(error_callback) if WEBHOOK: LOGGER.info(get_string("main", "WEBHOOKS", DEFAULT_LANG)) # 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(get_string("main", "LONG_POLLING", DEFAULT_LANG)) # LONG_POLLING updater.start_polling(timeout=15, read_latency=4) load_api() updater.idle()
def log_resource(bot: Bot, update: Update): entities = update.effective_message.parse_entities() caption_entities = update.effective_message.parse_caption_entities() chat = update.effective_chat # type: Optional[Chat] log_chat = sql.get_chat_log_channel(chat.id) if log_chat: result = f'<b>Risorsa inviata da @{update.effective_user.username}:</b>\n' for descriptor, entity in entities.items(): result = f'<b>Risorsa inviata da @{update.effective_user.username}:</b>\n' if descriptor['type'] in ['url', 'text_link']: result += f'{entity}' LOGGER.debug( f"Found message entity: {descriptor['type']} {entity}") send_log(bot, log_chat, chat.id, result) for descriptor, entity in caption_entities.items(): result = '<b>Risorsa inviata da @{update.effective_user.username}:</b>\n' if descriptor['type'] in ['url', 'text_link']: result += f'{entity}' LOGGER.debug( f"Found message entity: {descriptor['type']} {entity}") send_log(bot, log_chat, chat.id, result) else: send_log(bot, log_chat, chat.id, result)
HELPABLE = {} STATS = [] USER_INFO = [] DATA_IMPORT = [] DATA_EXPORT = [] CHAT_SETTINGS = {} USER_SETTINGS = {} GDPR = [] for module_name in ALL_MODULES: imported_module = importlib.import_module("tg_bot.modules." + module_name) if not hasattr(imported_module, "__mod_name__"): imported_module.__mod_name__ = imported_module.__name__ LOGGER.debug("Loaded Module {}".format(imported_module.__mod_name__)) if not imported_module.__mod_name__.lower() in IMPORTED: IMPORTED[imported_module.__mod_name__.lower()] = imported_module else: raise Exception(get_string("main", "NO_TWO_MODULES", DEFAULT_LANG)) # NO_TWO_MODULES if hasattr(imported_module, "__help__") and imported_module.__help__: HELPABLE[imported_module.__mod_name__.lower()] = imported_module # Chats to migrate on chat_migrated events if hasattr(imported_module, "__migrate__"): MIGRATEABLE.append(imported_module) if hasattr(imported_module, "__stats__"): STATS.append(imported_module)