Enable logging in using an access token (#21)
We do a `client.load_store()` to restore a previous session. If both token and password are defined, token is preferred, since it wont create a new device for the bot. One or the other needs to be defined. Requires https://github.com/anoadragon453/nio-template/pull/20 for the configuration change since password and access token must both be optional (but one must be given).
This commit is contained in:
parent
ff008a6aac
commit
b808119a73
|
@ -85,7 +85,11 @@ class Config(object):
|
||||||
if not re.match("@.*:.*", self.user_id):
|
if not re.match("@.*:.*", self.user_id):
|
||||||
raise ConfigError("matrix.user_id must be in the form @name:domain")
|
raise ConfigError("matrix.user_id must be in the form @name:domain")
|
||||||
|
|
||||||
self.user_password = self._get_cfg(["matrix", "user_password"], required=True)
|
self.user_password = self._get_cfg(["matrix", "user_password"], required=False)
|
||||||
|
self.user_token = self._get_cfg(["matrix", "user_token"], required=False)
|
||||||
|
if not self.user_token and not self.user_password:
|
||||||
|
raise ConfigError("Must supply either user token or password")
|
||||||
|
|
||||||
self.device_id = self._get_cfg(["matrix", "device_id"], required=True)
|
self.device_id = self._get_cfg(["matrix", "device_id"], required=True)
|
||||||
self.device_name = self._get_cfg(
|
self.device_name = self._get_cfg(
|
||||||
["matrix", "device_name"], default="nio-template"
|
["matrix", "device_name"], default="nio-template"
|
||||||
|
|
|
@ -51,6 +51,10 @@ async def main():
|
||||||
config=client_config,
|
config=client_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if config.user_token:
|
||||||
|
client.access_token = config.user_token
|
||||||
|
client.user_id = config.user_id
|
||||||
|
|
||||||
# Set up event callbacks
|
# Set up event callbacks
|
||||||
callbacks = Callbacks(client, store, config)
|
callbacks = Callbacks(client, store, config)
|
||||||
client.add_event_callback(callbacks.message, (RoomMessageText,))
|
client.add_event_callback(callbacks.message, (RoomMessageText,))
|
||||||
|
@ -59,6 +63,14 @@ async def main():
|
||||||
# Keep trying to reconnect on failure (with some time in-between)
|
# Keep trying to reconnect on failure (with some time in-between)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
if config.user_token:
|
||||||
|
# Use token to log in
|
||||||
|
client.load_store()
|
||||||
|
|
||||||
|
# Sync encryption keys with the server
|
||||||
|
if client.should_upload_keys:
|
||||||
|
await client.keys_upload()
|
||||||
|
else:
|
||||||
# Try to login with the configured username/password
|
# Try to login with the configured username/password
|
||||||
try:
|
try:
|
||||||
login_response = await client.login(
|
login_response = await client.login(
|
||||||
|
|
|
@ -9,8 +9,10 @@ command_prefix: "!c"
|
||||||
matrix:
|
matrix:
|
||||||
# The Matrix User ID of the bot account
|
# The Matrix User ID of the bot account
|
||||||
user_id: "@bot:example.com"
|
user_id: "@bot:example.com"
|
||||||
# Matrix account password
|
# Matrix account password (optional if access token used)
|
||||||
user_password: ""
|
user_password: ""
|
||||||
|
# Matrix account access token (optional if password used)
|
||||||
|
#user_token: ""
|
||||||
# The URL of the homeserver to connect to
|
# The URL of the homeserver to connect to
|
||||||
homeserver_url: https://example.com
|
homeserver_url: https://example.com
|
||||||
# The device ID that is **non pre-existing** device
|
# The device ID that is **non pre-existing** device
|
||||||
|
|
Loading…
Reference in New Issue