mirror of
https://github.com/matrixgpt/matrix-chatgpt-bot.git
synced 2024-10-01 05:25:41 +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
|
&& chown -R pptruser:pptruser /home/pptruser
|
||||||
USER pptruser
|
USER pptruser
|
||||||
|
|
||||||
|
VOLUME /storage
|
||||||
|
|
||||||
# We run a fake display and run our script.
|
# We run a fake display and run our script.
|
||||||
# Start script on Xvfb
|
# Start script on Xvfb
|
||||||
CMD xvfb-run --server-args="-screen 0 1024x768x24" yarn start
|
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.
|
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
|
# Usage
|
||||||
- Create an unencrypted room
|
- Create an (encrypted if enabled) room
|
||||||
- Add the bot
|
- Add the bot
|
||||||
- Start chatting away!
|
- Start chatting away!
|
||||||
|
|
||||||
@ -44,6 +44,11 @@ IS_GOOGLE_LOGIN=true
|
|||||||
# With the @ and :DOMAIN, ie @SOMETHING:DOMAIN
|
# With the @ and :DOMAIN, ie @SOMETHING:DOMAIN
|
||||||
MATRIX_BOT_USERNAME=
|
MATRIX_BOT_USERNAME=
|
||||||
MATRIX_BOT_PASSWORD=
|
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
|
# 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 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
|
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"
|
"typecheck": "npx tsc"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chatgpt": "^3.3.1",
|
"chatgpt": "^3.3.6",
|
||||||
"dotenv": "^14.2.0",
|
"dotenv": "^14.2.0",
|
||||||
"matrix-bot-sdk": "^0.6.2",
|
"matrix-bot-sdk": "^0.6.2",
|
||||||
"puppeteer": "^19.4.1",
|
"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 */
|
/** The full username: eg @bot:server.com */
|
||||||
export const matrixBotUsername = process.env.MATRIX_BOT_USERNAME as string;
|
export const matrixBotUsername = process.env.MATRIX_BOT_USERNAME as string;
|
||||||
export const matrixBotPassword = process.env.MATRIX_BOT_PASSWORD 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 */
|
/** ChatGPT specific stuff */
|
||||||
export const openAiEmail = process.env.OPENAI_EMAIL as string;
|
export const openAiEmail = process.env.OPENAI_EMAIL as string;
|
||||||
export const openAiPassword = process.env.OPENAI_PASSWORD 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);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
if(homeserverUrl === undefined) {
|
if(homeserverUrl === undefined) {
|
||||||
console.error("MATRIX_HOMESERVER_URL env variable is undefined");
|
console.error("MATRIX_HOMESERVER_URL env variable is undefined");
|
||||||
process.exit(1);
|
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) {
|
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);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
if(matrixBotPassword === undefined) {
|
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);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
if(openAiEmail === undefined) {
|
if(openAiEmail === undefined) {
|
||||||
|
27
src/index.ts
27
src/index.ts
@ -2,9 +2,12 @@ import {
|
|||||||
MatrixAuth, MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin,
|
MatrixAuth, MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin,
|
||||||
LogService, LogLevel,
|
LogService, LogLevel,
|
||||||
RichConsoleLogger,
|
RichConsoleLogger,
|
||||||
// RustSdkCryptoStorageProvider,
|
ICryptoStorageProvider,
|
||||||
|
RustSdkCryptoStorageProvider,
|
||||||
} from "matrix-bot-sdk";
|
} 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 { parseMatrixUsernamePretty } from './utils.js';
|
||||||
import { handleRoomEvent } from './handlers.js';
|
import { handleRoomEvent } from './handlers.js';
|
||||||
import { ChatGPTAPIBrowser } from 'chatgpt'
|
import { ChatGPTAPIBrowser } from 'chatgpt'
|
||||||
@ -19,15 +22,23 @@ LogService.setLevel(LogLevel.INFO);
|
|||||||
// LogService.muteModule("Metrics");
|
// LogService.muteModule("Metrics");
|
||||||
LogService.trace = LogService.debug;
|
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
|
// Prepare a crypto store if we need that
|
||||||
// const cryptoProvider = new RustSdkCryptoStorageProvider("./crypto/");
|
let cryptoStore: ICryptoStorageProvider;
|
||||||
|
if (matrixEncryption) {
|
||||||
|
cryptoStore = new RustSdkCryptoStorageProvider(path.join(dataPath, "encrypted")); // /storage/encrypted
|
||||||
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const botUsernameWithoutDomain = parseMatrixUsernamePretty(matrixBotUsername);
|
const botUsernameWithoutDomain = parseMatrixUsernamePretty(matrixBotUsername);
|
||||||
|
if (!accessToken){
|
||||||
const authedClient = await (new MatrixAuth(homeserverUrl)).passwordLogin(botUsernameWithoutDomain, matrixBotPassword);
|
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)
|
// use puppeteer to bypass cloudflare (headful because of captchas)
|
||||||
const chatGPT = new ChatGPTAPIBrowser({
|
const chatGPT = new ChatGPTAPIBrowser({
|
||||||
@ -38,7 +49,9 @@ async function main() {
|
|||||||
await chatGPT.initSession()
|
await chatGPT.initSession()
|
||||||
|
|
||||||
// Automatically join rooms the bot is invited to
|
// Automatically join rooms the bot is invited to
|
||||||
|
if (matrixAutojoin) {
|
||||||
AutojoinRoomsMixin.setupOnClient(client);
|
AutojoinRoomsMixin.setupOnClient(client);
|
||||||
|
}
|
||||||
|
|
||||||
client.on("room.failed_decryption", async (roomId, event, error) => {
|
client.on("room.failed_decryption", async (roomId, event, error) => {
|
||||||
// handle `m.room.encrypted` event that could not be decrypted
|
// handle `m.room.encrypted` event that could not be decrypted
|
||||||
@ -51,7 +64,7 @@ async function main() {
|
|||||||
|
|
||||||
await client.sendMessage(roomId, {
|
await client.sendMessage(roomId, {
|
||||||
"msgtype": "m.notice",
|
"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