def __init__(self): try: if Config.get('PROXY.ENABLED', False): self.updater = Updater(token=Config.get('PROXY.ENABLED'), request_kwargs={ 'proxy_url': Config.get('PROXY.URL', 'http://localhost:8123') }) else: self.updater = Updater(token=Config.get('GENERAL.TOKEN')) self.dispatcher = self.updater.dispatcher except Exception as e: log.error(__file__, '__init__', e, True)
def group_link(bot, update): # TODO: we can turn off/on it with dynamic methods like send a command to turn off it if Config.get('OPTIONS.GROUP_LINK_ENABLE', False): # Get Group link from templates invite_link = bot.getChat( chat_id=update.message.chat_id ).invite_link # if link doesn't exist for this bot so we create it if invite_link is None: invite_link = bot.export_chat_invite_link( chat_id=update.message.chat_id ) # pass invite link to template content = GROUP_LINK.read().format(INVITE_LINK=invite_link) else: # TODO: revoke the previous invite link # bot.export_chat_invite_link( # chat_id=update.message.chat_id # ) content = GROUP_LINK_DISABLED.read() __passContent(bot, update, content)
def bots(bot, update, job_queue): # Multi User Invited Support for user in update.message.new_chat_members: # Remove Bot if user.is_bot: # Kick the bot bot.kick_chat_member( chat_id=update.message.chat_id, user_id=user.id ) # if user not admin if update.message.from_user.id not in getGroupAdminsId(bot, update): # Kick User who add bot ! bot.kick_chat_member( chat_id=update.message.chat_id, user_id=update.message.from_user.id ) else: if Config.get('OPTIONS.LOGIN_RESTRICTION', False): # restrict user restrictUser(bot, update, user) # check user is bot or not (verify a question) registration_verification(bot, update, job_queue, user)
def detect_nudity_deepai_api(image_path: str, api_key: str = None) -> int: if api_key is None: api_key = Config.get("NUDITY_DETECTION.API_KEY", None) try: req = requests.post(API_URL, files={ 'image': open(image_path, 'rb'), }, headers={'api-key': api_key}) if str(req.status_code) == "200": return req.json().get('output', {}).get('nsfw_score', 0) except Exception as e: log.error(__file__, "nudity_detection", e) return 0
def download_image_async(bot, update, file_id): bot_file = bot.getFile(file_id=file_id) image_location = os.path.join(gettempdir(), file_id) bot_file.download(image_location) try: # pass image to check nudity (thread) accuracy = detect_nudity_deepai_api(image_location) # decision delete message or not if accuracy >= Config.getFloat("NUDITY_DETECTION.ACCURACY_THRESHHOLD"): messageRemover(bot, update.message) except Exception as e: log.error(__file__, 'download_image_async', e) # remove downloaded image if found if os.path.exists(image_location): os.remove(image_location)
def report(bot, update): # get tagged Message message = update.message.reply_to_message # delete ![command] message messageRemover(bot, update.message) try: # get chat_id of admins group admins_group_chat_id = Config.getInt('GENERAL.ADMINS_GROUP_CHAT_ID', 0) if admins_group_chat_id != 0: forwarded_message = bot.forward_message( chat_id=admins_group_chat_id, from_chat_id=update.message.chat_id, message_id=message.message_id ) reported_message_delete(bot, update, admins_group_chat_id, forwarded_message, message) # TODO: Replace Method # user = message.from_user # user_info = "[{}](tg://user?id={})".format( # user.username if user.username else user.first_name, # user.id # ) # # TODO: video, sticker # if bool(message.photo): # bot.send_photo( # # ) # else: # bot.send_message( # chat_id=admins_group_chat_id, # text=REPORT_MESSAGE.read().format( # user=user_info, # message=message.text # ), # parse_mode=telegram.ParseMode.MARKDOWN, # ) else: log.error(__file__, 'report', "you don't set ADMINS_GROUP_CHAT_ID in configuration yet") except Exception as e: log.error(__file__, 'report', e)
def registration_verification(bot, update, job_queue, user): # define button commands = [ [InlineKeyboardButton( REGISTRATION_VERIFY_BUTTON.read(), callback_data=str(user.id) )], ] # create a markup reply_markup = InlineKeyboardMarkup(commands) # TODO: is it safe ? others can get information about logged in user # Mitigation: it's better that show first name of joined user user_info = "[{}](tg://user?id={})".format( user.username if user.username else user.first_name, user.id ) # timer should be a number (we wanna use integer) timer = Config.getInt('OPTIONS.REGISTER_TIMER_MINUTES', 1) # send verification message message = bot.send_message( chat_id=update.message.chat_id, text=WELCOME.read().format( USER=user_info, TIME=timer ), reply_markup=reply_markup, parse_mode=telegram.ParseMode.MARKDOWN ) # add to job queue job_queue.run_once( register_timer, timer * 60, context={ "chat_id": update.message.chat_id, "message": message, "user_id": user.id } )
def telegram_link_remover(bot, update): # TODO: We should be handle url shorter later! if not Config.get('OPTIONS.TELEGRAM_LINK_REMOVER', False): return None # if message has a text type if bool(update.message.text): text = update.message.text # if message has a forward type elif bool(update.message.forward_date): text = update.message.caption else: return # find Telegram Links if link_finder(text): # Remove Message if GROUP_LINK.read() != text.strip(): messageRemover(bot, update.message)
def wrapper(*args, **kwargs): try: # TODO: i think it's not safe bot, update = args[0:2] # don't effect self if update.message.left_chat_member: if bot.id == update.message.left_chat_member.id: return None for user in update.message.new_chat_members: if bot.id == user.id: return None if Config.get('OPTIONS.REMOVE_STATUS_MESSAGES', False): # TODO: this has overlap with other usage of new_chat_members in functions that using this decorator # joined/leave/remove members messages if len(update.message.new_chat_members ) > 0 or update.message.left_chat_member: messageRemover(bot, update.message) except Exception as e: log.error(__file__, 'remove_joined_leave_message', e) return func(*args, **kwargs)
def start(self, webhook=Config.get('WEB_HOOK.ENABLED', False)): try: if webhook: # Run Bot as webhook self.updater.start_webhook( listen=Config.get('WEB_HOOK.LISTEN', '0.0.0.0'), port=Config.get('WEB_HOOK.PORT', '8443'), url_path=Config.get('GENERAL.TOKEN')) self.updater.bot.set_webhook('{}{}'.format( Config.get('WEB_HOOK.ADDRESS', 'localhost'), Config.get('GENERAL.TOKEN'))) self.updater.idle() else: self.updater.start_polling() log.info("Bot Started...") if Config.get('GENERAL.ENABLE_GET_CHAT_ID', False): log.info( "pass command `!id` in the admins group to get group_id here (not in the telegram) " "and set it in ADMINS_GROUP_CHAT_ID variable " "in the config.json file.") except Exception as e: log.error(__file__, 'start', e, True)
import lib.triggers.features.nudity_detection as nudity from lib.loader import Config FEATURES = [] if Config.get("NUDITY_DETECTION.ENABLED", False): FEATURES.append(nudity.handler) def features_handler(bot, update): # features controlled here for feature_handler in FEATURES: feature_handler(bot, update)
# Ask Question in one Message Please (!ask) Command("ask", ask_question), # Don't Use Kali (!kali) Command("kali", kali), # Grub Repair (!grub) Command("grub", grub_repair), # About (!about) Command("about", about), # Bot Usage (!usage) Command("usage", usage), ] if Config.get('GENERAL.ENABLE_GET_CHAT_ID', False): COMMANDS.append(Command("id", __get_chat_id)) # Listen on Messages def dispatcher(bot, update): # Link Remover telegram_link_remover(bot, update) # Commands Handler if str(update.message.text).startswith("!"): for command in COMMANDS: if command.cmd == update.message.text: command.run(bot, update)