From c5793ec12b555bf8b6cb65742299265309e9908a Mon Sep 17 00:00:00 2001 From: bertybuttface <110790513+bertybuttface@users.noreply.github.com> Date: Thu, 29 Dec 2022 01:14:31 +0000 Subject: [PATCH] Use matrix access token if we have it, print one if we don't. --- src/config.ts | 20 ++++++++++---------- src/index.ts | 11 ++++++++--- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/config.ts b/src/config.ts index 57a1796..d047ab6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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"); - process.exit(1); -} -if(matrixBotPassword === undefined) { - console.error("MATRIX_BOT_PASSWORD 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(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"); diff --git a/src/index.ts b/src/index.ts index 386df51..4398a96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); - const authedClient = await (new MatrixAuth(homeserverUrl)).passwordLogin(botUsernameWithoutDomain, matrixBotPassword); - const client = new MatrixClient(authedClient.homeserverUrl, authedClient.accessToken, storage); + if (!accessToken){ + const authedClient = await (new MatrixAuth(homeserverUrl)).passwordLogin(botUsernameWithoutDomain, matrixBotPassword); + 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({