@@ -22,6 +22,7 @@ class Api:
2222 creds : simplematrixbotlib.Creds
2323
2424 """
25+
2526 def __init__ (self , creds ):
2627 """
2728 Initializes the simplematrixbotlib.Api class.
@@ -43,44 +44,63 @@ async def login(self):
4344 raise ValueError ("Missing homeserver" )
4445 if not self .creds .username :
4546 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" )
47+ if not (self .creds .password or self .creds .login_token
48+ or self .creds .access_token ):
49+ raise ValueError (
50+ "Missing password, login token, access token. Either password, login token or access token must be provided"
51+ )
4852
49- self .async_client = AsyncClient (homeserver = self .creds .homeserver , user = self .creds .username , device_id = self .creds .device_id )
53+ self .async_client = AsyncClient (homeserver = self .creds .homeserver ,
54+ user = self .creds .username ,
55+ device_id = self .creds .device_id )
5056
5157 if self .creds .password :
52- resp = await self .async_client .login (password = self .creds .password , device_name = self .creds .device_name )
58+ resp = await self .async_client .login (
59+ password = self .creds .password ,
60+ device_name = self .creds .device_name )
5361
5462 elif self .creds .access_token :
5563 self .async_client .access_token = self .creds .access_token
5664
5765 async with aiohttp .ClientSession () as session :
58- async with session .get (f'{ self .creds .homeserver } /_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-
66+ async with session .get (
67+ f'{ self .creds .homeserver } /_matrix/client/r0/account/whoami?access_token={ self .creds .access_token } '
68+ ) as response :
69+ device_id = ast .literal_eval (
70+ (await
71+ response .text ()).replace (":false," ,
72+ ":\" false\" ," ))['device_id' ]
73+ user_id = ast .literal_eval (
74+ (await
75+ response .text ()).replace (":false," ,
76+ ":\" false\" ," ))['user_id' ]
77+
6278 self .async_client .device_id , self .creds .device_id = device_id , device_id
6379 self .async_client .user_id , self .creds .user_id = user_id , user_id
6480 resp = None
6581
6682 elif self .creds .login_token :
67- resp = await self .async_client .login (token = self .creds .login_token , device_name = self .creds .device_name )
68-
83+ resp = await self .async_client .login (
84+ token = self .creds .login_token ,
85+ device_name = self .creds .device_name )
86+
6987 if isinstance (resp , nio .responses .LoginError ):
7088 raise Exception (resp )
71-
89+
7290 async def check_valid_homeserver (self , homeserver : str ) -> bool :
73- if not (homeserver .startswith ('http://' ) or homeserver .startswith ('https://' )):
91+ if not (homeserver .startswith ('http://' )
92+ or homeserver .startswith ('https://' )):
7493 return False
75-
94+
7695 async with aiohttp .ClientSession () as session :
7796 try :
78- async with session .get (f'{ homeserver } /_matrix/client/versions' ) as response :
97+ async with session .get (
98+ f'{ homeserver } /_matrix/client/versions' ) as response :
7999 if response .status == 200 :
80100 return True
81101 except aiohttp .client_exceptions .ClientConnectorError :
82102 return False
83-
103+
84104 return False
85105
86106 async def send_text_message (self , room_id , message , msgtype = 'm.text' ):
@@ -184,7 +204,7 @@ async def send_markdown_message(self, room_id, message, msgtype='m.text'):
184204 "format" :
185205 "org.matrix.custom.html" ,
186206 "formatted_body" :
187- markdown .markdown (message , extensions = ['nl2br' ])
207+ markdown .markdown (
208+ message ,
209+ extensions = ['nl2br' ])
188210 })
189-
190-
0 commit comments