Merge branch 'feature/error-handling' into 'master'

check environment variables. handle invalid access token

See merge request cfdisk/hemppa!5
This commit is contained in:
plocki 2020-02-05 23:51:50 +00:00
commit be1652db4a
1 changed files with 38 additions and 9 deletions

47
bot.py
View File

@ -190,33 +190,62 @@ class Bot:
await asyncio.sleep(10) await asyncio.sleep(10)
def set_account_data(self, data): def set_account_data(self, data):
userid = urllib.parse.quote(os.environ['MATRIX_USER']) userid = urllib.parse.quote(self.matrix_user)
ad_url = f"{self.client.homeserver}/_matrix/client/r0/user/{userid}/account_data/{self.appid}?access_token={self.client.access_token}" ad_url = f"{self.client.homeserver}/_matrix/client/r0/user/{userid}/account_data/{self.appid}?access_token={self.client.access_token}"
response = requests.put(ad_url, json.dumps(data)) response = requests.put(ad_url, json.dumps(data))
self.__handle_error_response(response)
if response.status_code != 200: if response.status_code != 200:
print('Setting account data failed:', response, response.json()) print('Setting account data failed:', response, response.json())
def get_account_data(self): def get_account_data(self):
userid = urllib.parse.quote(os.environ['MATRIX_USER']) userid = urllib.parse.quote(self.matrix_user)
ad_url = f"{self.client.homeserver}/_matrix/client/r0/user/{userid}/account_data/{self.appid}?access_token={self.client.access_token}" ad_url = f"{self.client.homeserver}/_matrix/client/r0/user/{userid}/account_data/{self.appid}?access_token={self.client.access_token}"
response = requests.get(ad_url) response = requests.get(ad_url)
self.__handle_error_response(response)
if response.status_code == 200: if response.status_code == 200:
return response.json() return response.json()
print( print(
f'Getting account data failed: {response} {response.json()} - this is normal if you have not saved any settings yet.') f'Getting account data failed: {response} {response.json()} - this is normal if you have not saved any settings yet.')
return None return None
def __handle_error_response(self, response):
if response.status_code == 401:
print("ERROR: access token is invalid or missing")
print("NOTE: check MATRIX_ACCESS_TOKEN or set MATRIX_PASSWORD")
sys.exit(2)
def init(self): def init(self):
self.client = AsyncClient(
os.environ['MATRIX_SERVER'], os.environ['MATRIX_USER']) self.matrix_user = os.getenv('MATRIX_USER')
self.client.access_token = os.getenv('MATRIX_ACCESS_TOKEN') self.matrix_pass = os.getenv('MATRIX_PASSWORD')
self.join_on_invite = os.getenv("JOIN_ON_INVITE") is not None matrix_server = os.getenv('MATRIX_SERVER')
self.owners = os.environ['BOT_OWNERS'].split(',') bot_owners = os.getenv('BOT_OWNERS')
self.get_modules() access_token = os.getenv('MATRIX_ACCESS_TOKEN')
join_on_invite = os.getenv('JOIN_ON_INVITE')
if matrix_server and self.matrix_user and bot_owners:
self.client = AsyncClient(matrix_server, self.matrix_user)
self.client.access_token = access_token
if self.client.access_token is None:
if self.matrix_pass is None:
print("Either MATRIX_ACCESS_TOKEN or MATRIX_PASSWORD need to be set")
sys.exit(1)
self.join_on_invite = join_on_invite is not None
self.owners = bot_owners.split(',')
self.get_modules()
else:
print("The environment variables MATRIX_SERVER, MATRIX_USER and BOT_OWNERS are mandatory")
sys.exit(1)
def start(self): def start(self):
print(f'Starting {len(self.modules)} modules..') print(f'Starting {len(self.modules)} modules..')
@ -240,7 +269,7 @@ class Bot:
async def run(self): async def run(self):
if not self.client.access_token: if not self.client.access_token:
await self.client.login(os.environ['MATRIX_PASSWORD']) await self.client.login(self.matrix_pass)
last_16 = self.client.access_token[-16:] last_16 = self.client.access_token[-16:]
print(f"Logged in with password, access token: ...{last_16}") print(f"Logged in with password, access token: ...{last_16}")