Support login via access_token

This commit is contained in:
hibobmaster 2023-03-12 23:24:05 +08:00
parent 0a566888a2
commit 774038581f
Signed by: bobmaster
GPG key ID: 316B77D7914D713C
3 changed files with 16 additions and 7 deletions

7
bot.py
View file

@ -30,6 +30,7 @@ class Bot:
api_key: Optional[str] = "", api_key: Optional[str] = "",
room_id: Optional[str] = '', room_id: Optional[str] = '',
bing_api_endpoint: Optional[str] = '', bing_api_endpoint: Optional[str] = '',
access_token: Optional[str] = '',
): ):
self.homeserver = homeserver self.homeserver = homeserver
self.user_id = user_id self.user_id = user_id
@ -46,6 +47,8 @@ class Bot:
) )
self.client = AsyncClient(self.homeserver, user=self.user_id, device_id=self.device_id, self.client = AsyncClient(self.homeserver, user=self.user_id, device_id=self.device_id,
config=self.config, store_path=self.store_path) config=self.config, store_path=self.store_path)
if access_token != '':
self.client.access_token = access_token
# regular expression to match keyword [!gpt {prompt}] [!chat {prompt}] # regular expression to match keyword [!gpt {prompt}] [!chat {prompt}]
self.gpt_prog = re.compile(r"^\s*!gpt\s*(.+)$") self.gpt_prog = re.compile(r"^\s*!gpt\s*(.+)$")
self.chat_prog = re.compile(r"^\s*!chat\s*(.+)$") self.chat_prog = re.compile(r"^\s*!chat\s*(.+)$")
@ -138,10 +141,14 @@ class Bot:
# bot login # bot login
async def login(self) -> None: async def login(self) -> None:
try:
resp = await self.client.login(password=self.password) resp = await self.client.login(password=self.password)
if not isinstance(resp, LoginResponse): if not isinstance(resp, LoginResponse):
logger.error("Login Failed")
print(f"Login Failed: {resp}") print(f"Login Failed: {resp}")
sys.exit(1) sys.exit(1)
except Exception as e:
logger.error("Error Exception", exc_info=True)
# sync messages in the room # sync messages in the room
async def sync_forever(self, timeout=30000): async def sync_forever(self, timeout=30000):

View file

@ -5,5 +5,5 @@
"device_id": "ECYEOKVPLG", "device_id": "ECYEOKVPLG",
"room_id": "!FYCmBSkCRUNvZDBaDQ:matrix.qqs.tw", "room_id": "!FYCmBSkCRUNvZDBaDQ:matrix.qqs.tw",
"api_key": "xxxxxxxxxxxxxxxxxxxxxxxx", "api_key": "xxxxxxxxxxxxxxxxxxxxxxxx",
"bing_api_endpoint": "" "access_token": "xxxxxxx"
} }

View file

@ -9,12 +9,14 @@ async def main():
config = json.load(fp) config = json.load(fp)
matrix_bot = Bot(homeserver=config['homeserver'], matrix_bot = Bot(homeserver=config['homeserver'],
user_id=config['user_id'], user_id=config['user_id'],
password=config['password'], password=config.get('password', ''),
device_id=config['device_id'], device_id=config['device_id'],
room_id=config.get('room_id', ''), # provide a default value when the key does not exist room_id=config.get('room_id', ''), # provide a default value when the key does not exist
api_key=config.get('api_key', ''), api_key=config.get('api_key', ''),
bing_api_endpoint=config.get('bing_api_endpoint', ''), bing_api_endpoint=config.get('bing_api_endpoint', ''),
access_token=config.get('access_token', '')
) )
if config.get('access_token', '') == '':
await matrix_bot.login() await matrix_bot.login()
await matrix_bot.sync_forever() await matrix_bot.sync_forever()