mirror of
https://github.com/matrixgpt/matrix-chatgpt-bot.git
synced 2024-09-28 20:15:56 +00:00
Merge pull request #16 from bertybuttface/storage-fixes
This commit is contained in:
commit
57af80d5e8
@ -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
|
@ -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
|
||||
|
@ -15,7 +15,7 @@
|
||||
"typecheck": "npx tsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"chatgpt": "^3.3.1",
|
||||
"chatgpt": "^3.3.6",
|
||||
"dotenv": "^14.2.0",
|
||||
"matrix-bot-sdk": "^0.6.2",
|
||||
"puppeteer": "^19.4.1",
|
||||
|
@ -12,26 +12,43 @@ 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 = Boolean(process.env.IS_GOOGLE_LOGIN) as boolean;
|
||||
export const isGoogleLogin = process.env.IS_GOOGLE_LOGIN && process.env.IS_GOOGLE_LOGIN.toLowerCase() === "true";
|
||||
|
||||
if(accessToken === undefined) {
|
||||
console.error("MATRIX_ACCESS_TOKEN env variable is undefined");
|
||||
|
||||
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);
|
||||
}
|
||||
if(accessToken === undefined) {
|
||||
console.error("MATRIX_ACCESS_TOKEN env variable is undefined, set it to empty string to use username and password");
|
||||
process.exit(1);
|
||||
if(matrixBotUsername === undefined) {
|
||||
console.error("MATRIX_BOT_USERNAME env variable is undefined");
|
||||
console.error("MATRIX_BOT_USERNAME env variable is undefined, set it to empty string to use access token");
|
||||
process.exit(1);
|
||||
}
|
||||
if(matrixBotPassword === undefined) {
|
||||
console.error("MATRIX_BOT_PASSWORD env variable is undefined");
|
||||
console.error("MATRIX_BOT_PASSWORD env variable is undefined, set it to empty string to use access token");
|
||||
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) {
|
||||
|
27
src/index.ts
27
src/index.ts
@ -2,9 +2,12 @@ import {
|
||||
MatrixAuth, MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin,
|
||||
LogService, LogLevel,
|
||||
RichConsoleLogger,
|
||||
// RustSdkCryptoStorageProvider,
|
||||
ICryptoStorageProvider,
|
||||
RustSdkCryptoStorageProvider,
|
||||
} from "matrix-bot-sdk";
|
||||
import { openAiEmail, openAiPassword, isGoogleLogin, homeserverUrl, 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,15 +22,23 @@ 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);
|
||||
if (!accessToken){
|
||||
const authedClient = await (new MatrixAuth(homeserverUrl)).passwordLogin(botUsernameWithoutDomain, matrixBotPassword);
|
||||
const client = new MatrixClient(authedClient.homeserverUrl, authedClient.accessToken, storage);
|
||||
console.log(authedClient.homeserverUrl + " token: \n" + authedClient.accessToken)
|
||||
console.log("Set MATRIX_ACCESS_TOKEN to above token, MATRIX_ACCESS_USERNAME and MATRIX_ACCESS_PASSWORD can now be blank")
|
||||
return;
|
||||
}
|
||||
const client = new MatrixClient(homeserverUrl, accessToken, storage);
|
||||
|
||||
// use puppeteer to bypass cloudflare (headful because of captchas)
|
||||
const chatGPT = new ChatGPTAPIBrowser({
|
||||
@ -38,7 +49,9 @@ async function main() {
|
||||
await chatGPT.initSession()
|
||||
|
||||
// Automatically join rooms the bot is invited to
|
||||
if (matrixAutojoin) {
|
||||
AutojoinRoomsMixin.setupOnClient(client);
|
||||
}
|
||||
|
||||
client.on("room.failed_decryption", async (roomId, event, error) => {
|
||||
// handle `m.room.encrypted` event that could not be decrypted
|
||||
@ -51,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 }`,
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user