Properly handle when there is no api_key
This commit is contained in:
parent
eceb2bfaaf
commit
4c6133f9a8
2 changed files with 20 additions and 12 deletions
29
bot.py
29
bot.py
|
@ -1,4 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
|
import asyncio
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
@ -16,7 +17,7 @@ class Bot:
|
||||||
user_id: str,
|
user_id: str,
|
||||||
password: str,
|
password: str,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
api_key: str = "",
|
api_key: Optional[str] = "",
|
||||||
room_id: Optional[str] = '',
|
room_id: Optional[str] = '',
|
||||||
):
|
):
|
||||||
self.homeserver = homeserver
|
self.homeserver = homeserver
|
||||||
|
@ -37,7 +38,8 @@ class Bot:
|
||||||
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*(.+)$")
|
||||||
# initialize chatbot
|
# initialize chatbot
|
||||||
self.chatbot = Chatbot(api_key=self.api_key)
|
if self.api_key != '':
|
||||||
|
self.chatbot = Chatbot(api_key=self.api_key)
|
||||||
|
|
||||||
# message_callback event
|
# message_callback event
|
||||||
async def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
async def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
||||||
|
@ -57,15 +59,20 @@ class Bot:
|
||||||
|
|
||||||
n = self.chat_prog.match(event.body)
|
n = self.chat_prog.match(event.body)
|
||||||
if n:
|
if n:
|
||||||
# sending typing state
|
if self.api_key != '':
|
||||||
await self.client.room_typing(room_id)
|
# sending typing state
|
||||||
prompt = n.group(1)
|
await self.client.room_typing(room_id)
|
||||||
try:
|
prompt = n.group(1)
|
||||||
text = self.chatbot.ask(prompt).strip()
|
try:
|
||||||
await send_room_message(self.client, room_id, send_text=text)
|
# run synchronous function in different thread
|
||||||
except Exception as e:
|
text = await asyncio.to_thread(self.chatbot.ask, prompt)
|
||||||
print(f"Error: {e}")
|
text = text.strip()
|
||||||
pass
|
await send_room_message(self.client, room_id, send_text=text)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
await send_room_message(self.client, room_id, send_text="API_KEY not provided")
|
||||||
|
|
||||||
# print info to console
|
# print info to console
|
||||||
# print(
|
# print(
|
||||||
|
|
3
main.py
3
main.py
|
@ -12,7 +12,8 @@ async def main():
|
||||||
password=config['password'],
|
password=config['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['api_key'])
|
api_key=config.get('api_key', ''),
|
||||||
|
)
|
||||||
await matrix_bot.login()
|
await matrix_bot.login()
|
||||||
await matrix_bot.sync_forever()
|
await matrix_bot.sync_forever()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue