def process_delete_email(message): markup = types.ReplyKeyboardHide(selective=False) try: if not re.match(r"[^@]+@[^@]+\.[^@]+", message.text): bot.reply_to(message, 'Email incorrecto.') bot.register_next_step_handler(msg, process_delete_email) else: user = db.get_user(message.from_user.id)[0] emails = [] if 'emails' not in user else user['emails'] registered = False new_emails = [] for email in emails: if email['email'] == message.text: registered = True else: new_emails += [email] if registered: db.register_emails(user['id'], new_emails) else: bot.reply_to(message, 'Este email no está registrado. Se ignora.') bot.reply_to(message, 'Se ha borrado dicho dicho email.', reply_markup=markup) except Exception, e: HSHBOT_ADMIN_ID = int(os.environ['HSHBOT_ADMIN_ID']) if HSHBOT_ADMIN_ID: bot.send_message(HSHBOT_ADMIN_ID, 'ERROR', reply_markup=markup)
def handle_checkall(message): _logger.info("/checkall") user = db.get_user(message.from_user.id)[0] emails = [] if 'emails' not in user else user['emails'] if not emails: bot.reply_to(message, 'No has registrado emails todavía. Utiliza /newemail para ello.') flag_update_urls = False for email in emails: urls = [] if 'urls' not in email else email['urls'] urls_data = [url['url'] for url in urls] json_data = check_email(email['email']) lines = show_data(json_data) if lines: bot.reply_to(message, "%s con resultados..." % email['email']) for line in lines: registered = False if line[1] in urls_data: registered = True break if not registered: urls += [{'url': line[1]}] flag_update_urls =True line_str = ' '.join(line) bot.send_message(user['id'], line_str) email['urls'] = urls else: bot.send_message(user['id'], "%s Todo OK!" % email['email']) if flag_update_urls: db.register_emails(user['id'], emails)
def process_register_email(message): try: if not re.match(r"[^@]+@[^@]+\.[^@]+", message.text): bot.reply_to(message, 'Email incorrecto. Vuelve a introducirlo.') bot.register_next_step_handler(msg, process_register_email) else: user = db.get_user(message.from_user.id)[0] emails = [] if 'emails' not in user else user['emails'] registered = False for email in emails: if email['email'] == message.text: registered = True break if not registered: emails += [{ 'email': message.text, 'urls': [{ 'url': '', }], }] db.register_emails(user['id'], emails) bot.reply_to(message, 'Registrado!') else: bot.reply_to(message, 'Este email ya lo has registrado. Se ignora.') except Exception, e: bot.reply_to(message, 'ERROR')
def handle_delemails(message): _logger.info("/delemails") user = db.get_user(message.from_user.id)[0] if user['emails'] != []: db.register_emails(user['id'], []) bot.reply_to(message, 'Se han borrado todos los emails registrados.') else: bot.reply_to(message, 'No has registrado emails todavía. Utiliza /newemail para ello.') return False
def check(bot=False, confile=False): if not bot and confile: Config = ConfigParser.ConfigParser() Config.read(confile) HSHBOT_TOKEN = Config.get('options', 'api_token') bot = telebot.TeleBot(HSHBOT_TOKEN) HSHBOT_ADMIN_ID = int(Config.get('options', 'admin_id') or False) elif bot: HSHBOT_ADMIN_ID = int(os.environ['HSHBOT_ADMIN_ID']) else: _logger.error("Cron execution error, bot and confile not found!") sys.exit() _logger.info("cron:: check()") if HSHBOT_ADMIN_ID: bot.send_message(HSHBOT_ADMIN_ID, '[INFO] Iniciando cron.check()...') users = db.get_users() emails_checked = {} try: for user in users: user_emails = [] if 'emails' not in user else user['emails'] if not user_emails: bot.send_message( user['id'], "Se ha ejecutado el checkeo de emails, y veo que no tienes ninguno registrado " "aún...\nRecuerda que puedes hacerlo con /newemail" ) flag_update_urls = False for user_email in user_emails: email_urls = [] if 'urls' not in user_email else user_email['urls'] email_urls_data = [email_url['url'] for email_url in email_urls] flag_notification = False if user_email['email'] not in emails_checked: try: json_data = check_email(user_email['email']) except Exception, e: if HSHBOT_ADMIN_ID: bot.send_message(HSHBOT_ADMIN_ID, '[ERROR] HeSidoHackeadoBot is down!') raise e emails_checked[user_email['email']] = show_data(json_data) if emails_checked[user_email['email']]: for line in emails_checked[user_email['email']]: registered = False if line[1] in email_urls_data: registered = True break if not registered: line_str = ' '.join(line) if not flag_notification: flag_notification = True bot.send_message(user['id'], "%s con resultados..." % user_email['email']) bot.send_message(user['id'], line_str) email_urls += [{'url': line[1]}] flag_update_urls = True user_email['urls'] = email_urls # if not flag_notification: # bot.send_message(user['id'], "%s Todo OK!" % user_email['email']) if flag_update_urls: db.register_emails(user['id'], user_emails) except Exception, e: pass