Merge pull request #4 from bertybuttface/storage-fixes

Fix storage, e2e and add feature flags
This commit is contained in:
bertybuttface 2022-12-29 02:48:57 +00:00 committed by GitHub
commit bb1637e459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 9 deletions

View File

@ -66,6 +66,8 @@ RUN npm install puppeteer@19.4.1 \
&& chown -R pptruser:pptruser /home/pptruser
USER pptruser
VOLUME /storage
# We run a fake display and run our script.
# Start script on Xvfb
CMD xvfb-run --server-args="-screen 0 1024x768x24" yarn start

View File

@ -18,7 +18,7 @@ You should not be using this ChatGPT account while the bot is using it, because
If your OpenAI account uses Google Auth, you shouldn't encounter any of the more complicated Recaptchas — and can avoid using paid third-party CAPTCHA solving providers. To use Google auth, make sure your OpenAI account is using Google and then set IS_GOOGLE_LOGIN to true.
# Usage
- Create an unencrypted room
- Create an (encrypted if enabled) room
- Add the bot
- Start chatting away!
@ -44,6 +44,11 @@ IS_GOOGLE_LOGIN=true
# With the @ and :DOMAIN, ie @SOMETHING:DOMAIN
MATRIX_BOT_USERNAME=
MATRIX_BOT_PASSWORD=
MATRIX_AUTO_JOIN=true
MATRIX_ENCRYPTION=true
# needs to be ./storage/ if you aren't using Docker or /storage/ if you are.
DATA_PATH=/storage/
```
# Discussion
@ -65,7 +70,7 @@ recomend following the prompts at https://element.io/get-started to download and
```
docker build . -t matrix-chatgpt-bot
docker run --cap-add=SYS_ADMIN -it matrix-chatgpt-bot
docker run --cap-add=SYS_ADMIN -it -v ./storage:/storage matrix-chatgpt-bot
```
Note: Without -it flags in the command above you won't be able to stop the container using Ctrl-C

View File

@ -12,12 +12,21 @@ export const homeserverUrl = process.env.MATRIX_HOMESERVER_URL as string;
/** The full username: eg @bot:server.com */
export const matrixBotUsername = process.env.MATRIX_BOT_USERNAME as string;
export const matrixBotPassword = process.env.MATRIX_BOT_PASSWORD as string;
export const matrixAutojoin = process.env.MATRIX_AUTO_JOIN && process.env.MATRIX_AUTO_JOIN.toLowerCase() === "true" as string;
export const matrixEncryption = process.env.MATRIX_ENCRYPTION && process.env.MATRIX_ENCRYPTION.toLowerCase() === "true" as string;
export const dataPath = process.env.DATA_PATH as string;
/** ChatGPT specific stuff */
export const openAiEmail = process.env.OPENAI_EMAIL as string;
export const openAiPassword = process.env.OPENAI_PASSWORD as string;
export const isGoogleLogin = process.env.IS_GOOGLE_LOGIN && process.env.IS_GOOGLE_LOGIN.toLowerCase() === "true";
if(dataPath === undefined) {
console.error("DATA_PATH env variable is undefined");
process.exit(1);
}
if(homeserverUrl === undefined) {
console.error("MATRIX_HOMESERVER_URL env variable is undefined");
process.exit(1);
@ -34,6 +43,14 @@ if(accessToken === undefined) {
process.exit(1);
}
}
if(matrixAutojoin === undefined) {
console.error("MATRIX_AUTO_JOIN env variable is undefined");
process.exit(1);
}
if(matrixEncryption === undefined) {
console.error("MATRIX_ENCRYPTION env variable is undefined");
process.exit(1);
}
if(openAiEmail === undefined) {
console.error("OPENAI_EMAIL env variable is undefined");
process.exit(1);

View File

@ -2,9 +2,12 @@ import {
MatrixAuth, MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin,
LogService, LogLevel,
RichConsoleLogger,
// RustSdkCryptoStorageProvider,
ICryptoStorageProvider,
RustSdkCryptoStorageProvider,
} from "matrix-bot-sdk";
import { openAiEmail, openAiPassword, isGoogleLogin, homeserverUrl, accessToken, matrixBotPassword, matrixBotUsername } from './config.js'
import * as path from "path";
import { dataPath, openAiEmail, openAiPassword, isGoogleLogin, homeserverUrl, accessToken, matrixAutojoin, matrixBotPassword, matrixBotUsername, matrixEncryption } from './config.js'
import { parseMatrixUsernamePretty } from './utils.js';
import { handleRoomEvent } from './handlers.js';
import { ChatGPTAPIBrowser } from 'chatgpt'
@ -19,10 +22,13 @@ LogService.setLevel(LogLevel.INFO);
// LogService.muteModule("Metrics");
LogService.trace = LogService.debug;
const storage = new SimpleFsStorageProvider("./storage/bot.json");
const storage = new SimpleFsStorageProvider(path.join(dataPath, "bot.json")); // /storage/bot.json
// Still fails to decrypt sometimes
// const cryptoProvider = new RustSdkCryptoStorageProvider("./crypto/");
// Prepare a crypto store if we need that
let cryptoStore: ICryptoStorageProvider;
if (matrixEncryption) {
cryptoStore = new RustSdkCryptoStorageProvider(path.join(dataPath, "encrypted")); // /storage/encrypted
}
async function main() {
const botUsernameWithoutDomain = parseMatrixUsernamePretty(matrixBotUsername);
@ -43,7 +49,9 @@ async function main() {
await chatGPT.initSession()
// Automatically join rooms the bot is invited to
AutojoinRoomsMixin.setupOnClient(client);
if (matrixAutojoin) {
AutojoinRoomsMixin.setupOnClient(client);
}
client.on("room.failed_decryption", async (roomId, event, error) => {
// handle `m.room.encrypted` event that could not be decrypted
@ -56,7 +64,7 @@ async function main() {
await client.sendMessage(roomId, {
"msgtype": "m.notice",
"body": `👋 Hello, I'm the ChatGPT bot! I only work in unencrypted rooms at the moment.`,
"body": `👋 Hello, I'm the ChatGPT bot! Encrypted message support: ${ matrixEncryption }`,
});
});