Use matrix access token if we have it, print one if we don't.

This commit is contained in:
bertybuttface 2022-12-29 01:14:31 +00:00
parent 9bd97b2453
commit c5793ec12b
2 changed files with 18 additions and 13 deletions

View File

@ -18,21 +18,21 @@ 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;
if(accessToken === undefined) {
console.error("MATRIX_ACCESS_TOKEN env variable is undefined");
process.exit(1);
}
if(homeserverUrl === undefined) {
console.error("MATRIX_HOMESERVER_URL env variable is undefined");
process.exit(1);
}
if(matrixBotUsername === undefined) {
console.error("MATRIX_BOT_USERNAME env variable is undefined");
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(matrixBotPassword === undefined) {
console.error("MATRIX_BOT_PASSWORD env variable is undefined");
if(matrixBotUsername === 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, set it to empty string to use access token");
process.exit(1);
}
}
if(openAiEmail === undefined) {
console.error("OPENAI_EMAIL env variable is undefined");

View File

@ -4,7 +4,7 @@ import {
RichConsoleLogger,
// RustSdkCryptoStorageProvider,
} from "matrix-bot-sdk";
import { openAiEmail, openAiPassword, isGoogleLogin, homeserverUrl, matrixBotPassword, matrixBotUsername } from './config.js'
import { openAiEmail, openAiPassword, isGoogleLogin, homeserverUrl, accessToken, matrixBotPassword, matrixBotUsername } from './config.js'
import { parseMatrixUsernamePretty } from './utils.js';
import { handleRoomEvent } from './handlers.js';
import { ChatGPTAPIBrowser } from 'chatgpt'
@ -26,8 +26,13 @@ const storage = new SimpleFsStorageProvider("./storage/bot.json");
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({