allow user to be added via id

This commit is contained in:
2026-05-11 11:34:38 +01:00
parent 5b434abd06
commit 7a2db35495
8 changed files with 170 additions and 9 deletions

View File

@@ -9,6 +9,112 @@ load_dotenv()
pb = PocketBase(os.getenv('POCKETBASE_URL'))
admin_data = pb.admins.auth_with_password(os.getenv('POCKETBASE_ADMIN_EMAIL'), os.getenv('POCKETBASE_ADMIN_PASSWORD'))
show_debug_msg = False
TELEGRAM_COLLECTION_NAME = 'Telegram'
def _record_value(record, key, default=None):
def _lookup(mapping):
if not isinstance(mapping, dict):
return default
if key in mapping:
return mapping.get(key, default)
lower_key = key.lower()
for existing_key, existing_value in mapping.items():
if str(existing_key).lower() == lower_key:
return existing_value
return default
if isinstance(record, dict):
return _lookup(record)
if hasattr(record, 'get'):
try:
value = record.get(key, default)
if value is not default:
return value
except Exception:
pass
if hasattr(record, 'data'):
try:
value = _lookup(record.data)
if value is not default:
return value
except Exception:
pass
if hasattr(record, 'model_dump'):
try:
value = _lookup(record.model_dump())
if value is not default:
return value
except Exception:
pass
if hasattr(record, 'to_dict'):
try:
value = _lookup(record.to_dict())
if value is not default:
return value
except Exception:
pass
if hasattr(record, '__dict__'):
value = _lookup(record.__dict__)
if value is not default:
return value
if hasattr(record, key):
return getattr(record, key)
return default
def _normalize_telegram_id(value):
if value is None:
return None
try:
return int(str(value).strip())
except (TypeError, ValueError):
return None
def get_telegram_user_ids():
records = pb.collection(TELEGRAM_COLLECTION_NAME).get_full_list()
telegram_ids = set()
for record in records:
raw_telegram_id = _record_value(record, 'TGID')
telegram_id = _normalize_telegram_id(raw_telegram_id)
if telegram_id is not None:
telegram_ids.add(telegram_id)
record_id = _normalize_telegram_id(_record_value(record, 'id'))
if record_id is not None:
telegram_ids.add(record_id)
return sorted(telegram_ids)
def add_telegram_user_id(user_id: int):
collection = pb.collection(TELEGRAM_COLLECTION_NAME)
payload_candidates = [
{'TGID': user_id},
{'id': str(user_id)},
]
last_error = None
for payload in payload_candidates:
try:
return collection.create(payload)
except Exception as exc:
last_error = exc
raise last_error
def convert_datetime_to_pocketbase(date_time_str):
"""
@@ -52,7 +158,7 @@ def upload_entry(data, entry_type='opportunity', url=None):
entry_type: 'opportunity' or 'event'
url: The source URL of the entry
"""
print(f"[DEBUG] Uploading {entry_type} entry. Data: {data["title"]}")
print(f"[DEBUG] Uploading {entry_type} entry. Data: {data['title']}")
data = dict(data)
# Add URL to data if provided