Skip to content
Extraits de code Groupes Projets
Valider 4190057b rédigé par Tulir Asokan's avatar Tulir Asokan
Parcourir les fichiers

Ask for Matrix access token on first run

parent d698f058
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -6,11 +6,10 @@ A fast and simple Matrix sticker picker widget. Tested on Element Web & Android.
1. Create with `virtualenv -p python3 .`
2. Activate with `source ./bin/activate`
2. Install dependencies with `pip install -r requirements.txt`
3. Copy `example-config.json` to `config.json` and set your homeserver URL and access token
(used for uploading stickers to Matrix).
4. Run `python3 import.py <pack urls...>`
* On the first run, it'll prompt you to log in with a bot token or a telegram account.
The session data is stored in `sticker-import.session` by default.
3. Run `python3 import.py <pack urls...>`
* On the first run, it'll prompt you to log in to Matrix and Telegram.
* The Matrix URL and access token are stored in `config.json` by default.
* The Telethon session data is stored in `sticker-import.session` by default.
* By default, the pack data will be written to `web/packs/`.
* You can pass as many pack URLs as you want.
* You can re-run the command with the same URLs to update packs.
......
{
"homeserver": "https://example.com",
"access_token": "foo"
}
......@@ -31,11 +31,36 @@ parser.add_argument("--output-dir", help="Directory to write packs to", default=
parser.add_argument("pack", help="Sticker pack URLs to import", action="append", nargs="*")
args = parser.parse_args()
async def whoami(url: URL, access_token: str) -> str:
headers = {"Authorization": f"Bearer {access_token}"}
async with ClientSession() as sess, sess.get(url, headers=headers) as resp:
resp.raise_for_status()
user_id = (await resp.json())["user_id"]
print(f"Access token validated (user ID: {user_id})")
return user_id
try:
with open(args.config) as config_file:
config = json.load(config_file)
homeserver_url = config["homeserver"]
upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload"
access_token = config["access_token"]
except FileNotFoundError:
print("Matrix config file not found. Please enter your homeserver and access token.")
homeserver_url = input("Homeserver URL: ")
access_token = input("Access token: ")
whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami"
user_id = asyncio.run(whoami(whoami_url, access_token))
with open(args.config, "w") as config_file:
json.dump({
"homeserver": homeserver_url,
"user_id": user_id,
"access_token": access_token
}, config_file)
print(f"Wrote config to {args.config}")
upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload"
async def upload(data: bytes, mimetype: str, filename: str) -> str:
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter