Skip to content

Commit

Permalink
✨ Support several whitelist room_id
Browse files Browse the repository at this point in the history
  • Loading branch information
hibobmaster committed Jun 22, 2024
1 parent e32b96c commit 7da6ddb
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .full-env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ USER_ID="@lullap:xxxxxxxxxxxxx.xxx"
PASSWORD="xxxxxxxxxxxxxxx"
ACCESS_TOKEN="xxxxxxxxxxx"
DEVICE_ID="xxxxxxxxxxxxxx"
ROOM_ID="!FYCmBSkCRUXXXXXXXXX:matrix.XXX.XXX"
ROOM_ID="!aaaaaaa:XXX.XXX,!bbbbbb:XXX.XXX"
IMPORT_KEYS_PATH="element-keys.txt"
IMPORT_KEYS_PASSWORD="xxxxxxxxxxxx"
OPENAI_API_KEY="xxxxxxxxxxxxxxxxx"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.8.0
- Support custom help message
- Support several whitelist room_id

## 1.7.2
- Refactor gpt vision trigger method
Expand Down
4 changes: 2 additions & 2 deletions custom_help_message.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Hi there, welcome to our chat room!
Our bot is powered by opensource project [matrix_chatgpt_bot](https://github.com/hibobmaster/matrix_chatgpt_bot).
Here are some commands you can use to interact with the bot.
!gpt [prompt], generate a one time response without context conversation
!chat [prompt], chat with context conversation
!pic [prompt], Image generation by DALL-E-3 or LocalAI or stable-diffusion-webui
!new + chat, start a new conversation
!lc [prompt], chat using langchain api
quote a image and @bot with prompt, gpt vision function
@bot with prompt, create a thread level chatting
!help, help message

Our bot is powered by opensource project [matrix_chatgpt_bot](https://github.com/hibobmaster/matrix_chatgpt_bot).
2 changes: 1 addition & 1 deletion full-config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"password": "xxxxxxxxxxxxxxxxxx",
"access_token": "xxxxxxxxxxxxxx",
"device_id": "MatrixChatGPTBot",
"room_id": "!xxxxxxxxxxxxxxxxxxxxxx:xxxxx.org",
"room_id": ["!xxxxxxxxxxxxxxxxxxxxxx:xxxxx.org"],
"import_keys_path": "element-keys.txt",
"import_keys_password": "xxxxxxxxxxxxxxxxxxxx",
"openai_api_key": "xxxxxxxxxxxxxxxxxxxxxxxx",
Expand Down
21 changes: 12 additions & 9 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(
device_id: str,
password: Union[str, None] = None,
access_token: Union[str, None] = None,
room_id: Union[str, None] = None,
whitelist_room_id: Optional[list[str]] = None,
import_keys_path: Optional[str] = None,
import_keys_password: Optional[str] = None,
openai_api_key: Union[str, None] = None,
Expand Down Expand Up @@ -110,7 +110,6 @@ def __init__(
self.password: str = password
self.access_token: str = access_token
self.device_id: str = device_id
self.room_id: str = room_id

self.openai_api_key: str = openai_api_key
self.gpt_api_endpoint: str = (
Expand Down Expand Up @@ -159,6 +158,14 @@ def __init__(

self.base_path = Path(os.path.dirname(__file__)).parent

self.whitelist_room_id = whitelist_room_id
if whitelist_room_id is not None:
if isinstance(whitelist_room_id, str):
whitelist_room_id_list = list()
for room_id in whitelist_room_id.split(","):
whitelist_room_id_list.append(room_id.strip())
self.whitelist_room_id = whitelist_room_id_list

if lc_admin is not None:
if isinstance(lc_admin, str):
lc_admin = list(filter(None, lc_admin.split(",")))
Expand Down Expand Up @@ -243,13 +250,9 @@ async def close(self, task: asyncio.Task) -> None:

# message_callback RoomMessageText event
async def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None:
if self.room_id is None:
room_id = room.room_id
else:
# if event room id does not match the room id in config, return
if room.room_id != self.room_id:
return
room_id = self.room_id
if room.room_id not in self.whitelist_room_id:
return
room_id = room.room_id

# reply event_id
reply_to_event_id = event.event_id
Expand Down
4 changes: 2 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def main():
password=config.get("password"),
access_token=config.get("access_token"),
device_id=config.get("device_id"),
room_id=config.get("room_id"),
whitelist_room_id=config.get("room_id"),
import_keys_path=config.get("import_keys_path"),
import_keys_password=config.get("import_keys_password"),
openai_api_key=config.get("openai_api_key"),
Expand Down Expand Up @@ -83,7 +83,7 @@ async def main():
password=os.environ.get("PASSWORD"),
access_token=os.environ.get("ACCESS_TOKEN"),
device_id=os.environ.get("DEVICE_ID"),
room_id=os.environ.get("ROOM_ID"),
whitelist_room_id=os.environ.get("ROOM_ID"),
import_keys_path=os.environ.get("IMPORT_KEYS_PATH"),
import_keys_password=os.environ.get("IMPORT_KEYS_PASSWORD"),
openai_api_key=os.environ.get("OPENAI_API_KEY"),
Expand Down

0 comments on commit 7da6ddb

Please sign in to comment.