12 Fetches the bot token and returns it, by requesting it from the server if expired
13 or doesn't exist, but loads from a local file if it exists and isn't expired.
16 if Path(BOT_TOKEN_PATH).exists():
17 with open(BOT_TOKEN_PATH,
"r")
as token_file:
18 token_json: dict = load(token_file)
19 token_expiry: datetime = datetime.fromisoformat(token_json[
"expiresAt"])
20 if datetime.now(UTC) < token_expiry:
21 return token_json[
"token"]
23 token_res: Response = post(
"https://classic.talkomatic.co/api/v1/bot-tokens/request")
24 if token_res.status_code != 201:
25 raise Exception(str(token_res.json()))
26 token: str = token_res.json()[
"token"]
27 with open(BOT_TOKEN_PATH,
"w")
as token_file:
28 dump(token_res.json(), token_file)