diff --git a/wallamanta/account_checker.py b/wallamanta/account_checker.py index c36d8cf..5edf3dc 100644 --- a/wallamanta/account_checker.py +++ b/wallamanta/account_checker.py @@ -9,13 +9,18 @@ logging.basicConfig( ) async def work(sleep_time): + await asyncio.sleep(30) while True: user_list = walladb.get_user_list() for user in user_list: if walladb.is_user_valid(user['telegram_user_id']): if walladb.is_user_expired(user['telegram_user_id']): message = "¡Hola!\n\nTu cuenta ha caducado. Si quieres seguir usando @wallamanta_bot ponte en contacto con @jocarduck\n\n¡Gracias!" - await helpers.send_message(user['telegram_user_id'], message) + try: + await asyncio.sleep(1) + await helpers.send_message(user['telegram_user_id'], message) + except Exception as e: + logging.error(f"Couldn't sent message to {user['telegram_user_id']}. Reason: {e}") walladb.deactivate_user(user['telegram_user_id']) await asyncio.sleep(sleep_time) diff --git a/wallamanta/walladb.py b/wallamanta/walladb.py index 5409263..883dc7c 100644 --- a/wallamanta/walladb.py +++ b/wallamanta/walladb.py @@ -147,13 +147,15 @@ def add_test_user(telegram_user_id, telegram_name, until): con = connect_db() cur = con.cursor(prepared=True) params = (telegram_user_id,) - res = cur.execute(f"SELECT * FROM users WHERE telegram_user_id=%s", params) - if res.fetchone() is None: + cur.execute(f"SELECT * FROM users WHERE telegram_user_id=%s", params) + try: + cur.fetchone() params = (telegram_user_id, True, 'testing', until, telegram_name.first_name) cur.execute("INSERT INTO users VALUES (%s, %s, %s, %s, %s)", params) con.commit() - else: found = True + except Exception as e: + logging.error(f"Couldn't find username with id {telegram_user_id}: {e}") con.close() logging.info(f"Added test user {telegram_user_id} until {until}") return not found diff --git a/wallamanta/wallamanta.py b/wallamanta/wallamanta.py index c7a06d9..4fa17b1 100644 --- a/wallamanta/wallamanta.py +++ b/wallamanta/wallamanta.py @@ -387,7 +387,8 @@ async def status_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> async def test_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: telegram_user_id = helpers.get_telegram_user_id(update) telegram_user_name = helpers.get_telegram_user_name(update) - if not walladb.is_user_valid(telegram_user_id): + #if not walladb.is_user_valid(telegram_user_id): + if walladb.get_user(telegram_user_id) == "NoName": until = helpers.get_date_ahead(7) walladb.add_test_user(telegram_user_id, telegram_user_name, until) message = f"Periodo de prueba activado hasta el {helpers.get_spanish_date(until)}."