Files
ntfy-matrix-bridge/login.py
2026-06-17 22:24:58 -04:00

41 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""One-time helper: log in with a password and print a Matrix access token.
Run this once for the bot account, then put the printed access token + device id
into config.yaml (or export MATRIX_ACCESS_TOKEN). The token stays valid until you
log the device out, so you only need to do this again if you revoke it.
"""
import asyncio
import getpass
from nio import AsyncClient, LoginResponse
async def main() -> None:
homeserver = input("Homeserver URL [https://matrix.example.org]: ").strip()
if not homeserver:
homeserver = "https://matrix.example.org"
if not homeserver.startswith(("http://", "https://")):
homeserver = "https://" + homeserver
user_id = input("Bot user ID [@ntfybot:example.org]: ").strip()
password = getpass.getpass("Password: ")
client = AsyncClient(homeserver, user_id)
try:
resp = await client.login(password, device_name="ntfy-matrix-bridge")
if isinstance(resp, LoginResponse):
print("\nLogin successful. Add these to your config:\n")
print(f" user_id: {resp.user_id}")
print(f" device_id: {resp.device_id}")
print(f" access_token: {resp.access_token}")
else:
print(f"\nLogin failed: {resp}")
finally:
await client.close()
if __name__ == "__main__":
asyncio.run(main())