|
5 | 5 | import mimetypes |
6 | 6 | import os |
7 | 7 | import markdown |
8 | | - |
| 8 | +import aiohttp |
| 9 | +import ast |
9 | 10 | from nio.responses import UploadResponse |
| 11 | +import nio |
10 | 12 |
|
11 | 13 |
|
12 | 14 | class Api: |
@@ -37,30 +39,36 @@ async def login(self): |
37 | 39 |
|
38 | 40 | """ |
39 | 41 |
|
40 | | - self.creds.session_read_file() |
| 42 | + if not self.creds.homeserver: |
| 43 | + raise ValueError("Missing homeserver") |
| 44 | + if not self.creds.username: |
| 45 | + raise ValueError("Missing Username") |
| 46 | + if not (self.creds.password or self.creds.login_token or self.creds.access_token): |
| 47 | + raise ValueError("Missing password, login token, access token. Either password, login token or access token must be provided") |
41 | 48 |
|
42 | | - self.async_client = AsyncClient(self.creds.homeserver, |
43 | | - self.creds.username) |
| 49 | + self.async_client = AsyncClient(homeserver=self.creds.homeserver, user=self.creds.username, device_id=self.creds.device_id) |
44 | 50 |
|
45 | | - if self.creds.device_id: |
46 | | - self.async_client.device_id = self.creds.device_id |
| 51 | + if self.creds.password: |
| 52 | + resp = await self.async_client.login(password=self.creds.password, device_name=self.creds.device_name) |
47 | 53 |
|
48 | | - if self.creds.password or self.creds.login_token: |
49 | | - response = await self.async_client.login( |
50 | | - password=self.creds.password, |
51 | | - device_name='Bot Client built with Simple-Matrix-Bot-Lib', |
52 | | - token=self.creds.login_token) |
53 | | - print(response) |
| 54 | + elif self.creds.access_token: |
| 55 | + self.async_client.access_token = self.creds.access_token |
54 | 56 |
|
55 | | - self.creds.device_id = response.device_id |
56 | | - self.creds.access_token = response.access_token |
| 57 | + async with aiohttp.ClientSession() as session: |
| 58 | + async with session.get(f'https://matrix.org/_matrix/client/r0/account/whoami?access_token={self.creds.access_token}') as response: |
| 59 | + device_id = ast.literal_eval((await response.text()).replace(":false,", ":\"false\","))['device_id'] |
| 60 | + user_id = ast.literal_eval((await response.text()).replace(":false,", ":\"false\","))['user_id'] |
| 61 | + |
| 62 | + self.async_client.device_id, self.creds.device_id = device_id, device_id |
| 63 | + self.async_client.user_id, self.creds.user_id = user_id, user_id |
| 64 | + resp = None |
57 | 65 |
|
58 | | - else: |
59 | | - self.async_client.access_token = self.creds.access_token |
60 | | - self.async_client.user_id = f"@{self.creds.username}:{self.creds.homeserver}" |
61 | | - self.async_client.device_id = self.creds.device_id |
| 66 | + elif self.creds.login_token: |
| 67 | + resp = await self.async_client.login(token=self.creds.login_token, device_name=self.creds.device_name) |
| 68 | + |
| 69 | + if isinstance(resp, nio.responses.LoginError): |
| 70 | + raise Exception(resp) |
62 | 71 |
|
63 | | - self.creds.session_write_file() |
64 | 72 |
|
65 | 73 | async def send_text_message(self, room_id, message): |
66 | 74 | """ |
@@ -159,3 +167,5 @@ async def send_markdown_message(self, room_id, message): |
159 | 167 | "formatted_body": |
160 | 168 | markdown.markdown(message) |
161 | 169 | }) |
| 170 | + |
| 171 | + |
0 commit comments