Created
November 19, 2020 14:35
-
-
Save erdnaxeli/7d69e721e4433a5a4a8a925ee3e62bd4 to your computer and use it in GitHub Desktop.
Minimal Matrix example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
import requests | |
logging.basicConfig(level=logging.INFO) | |
ACCESS_TOKEN = "YOUR ACCESS TOKEN" | |
HS_URL = "https://cloud.cervoi.se" | |
first_sync = True | |
next_batch = None | |
while True: | |
logging.info("Syncing") | |
response = requests.get( | |
HS_URL + "/_matrix/client/r0/sync", | |
headers={"Content-Type": "application/json"}, | |
params={"access_token": ACCESS_TOKEN, "since": next_batch}, | |
) | |
if first_sync: | |
# drop first sync, it may contain already seen events | |
logging.info("Skip first sync") | |
first_sync = False | |
continue | |
sync = response.json() | |
next_batch = sync["next_batch"] | |
for room_id, room in sync["rooms"]["join"].items(): | |
for event in room["timeline"]["events"]: | |
if event["type"] == "m.room.message": | |
print(f"{event['sender']} just sent:") | |
print(event["content"].get("body", None)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment