matrix-chatgpt-bot/src/index.ts

73 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-12-09 09:41:17 +00:00
import {
MatrixAuth, MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin,
LogService, LogLevel,
RichConsoleLogger,
2022-12-29 02:38:29 +00:00
ICryptoStorageProvider,
RustSdkCryptoStorageProvider,
2022-12-09 09:41:17 +00:00
} from "matrix-bot-sdk";
2022-12-29 02:38:29 +00:00
import * as path from "path";
import { dataPath, openAiEmail, openAiPassword, isGoogleLogin, homeserverUrl, accessToken, matrixAutojoin, matrixBotPassword, matrixBotUsername, matrixEncryption } from './config.js'
2022-12-09 09:41:17 +00:00
import { parseMatrixUsernamePretty } from './utils.js';
import { handleRoomEvent } from './handlers.js';
import { ChatGPTAPIBrowser } from 'chatgpt'
2022-12-09 09:41:17 +00:00
LogService.setLogger(new RichConsoleLogger());
// Shows the Matrix sync loop details - not needed most of the time
// LogService.setLevel(LogLevel.DEBUG);
LogService.setLevel(LogLevel.INFO);
// LogService.muteModule("Metrics");
LogService.trace = LogService.debug;
2022-12-29 02:38:29 +00:00
const storage = new SimpleFsStorageProvider(path.join(dataPath, "bot.json")); // /storage/bot.json
2022-12-09 09:41:17 +00:00
2022-12-29 02:38:29 +00:00
// Prepare a crypto store if we need that
let cryptoStore: ICryptoStorageProvider;
if (matrixEncryption) {
cryptoStore = new RustSdkCryptoStorageProvider(path.join(dataPath, "encrypted")); // /storage/encrypted
}
2022-12-09 09:41:17 +00:00
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);
// use puppeteer to bypass cloudflare (headful because of captchas)
const chatGPT = new ChatGPTAPIBrowser({
email: openAiEmail,
password: openAiPassword,
isGoogleLogin: isGoogleLogin
2022-12-09 09:41:17 +00:00
})
await chatGPT.initSession()
2022-12-09 09:41:17 +00:00
// Automatically join rooms the bot is invited to
2022-12-29 02:38:29 +00:00
if (matrixAutojoin) {
AutojoinRoomsMixin.setupOnClient(client);
}
2022-12-09 09:41:17 +00:00
client.on("room.failed_decryption", async (roomId, event, error) => {
// handle `m.room.encrypted` event that could not be decrypted
LogService.error("index", `Failed decryption event!\n${{ roomId, event, error }}`);
await client.sendText(roomId, `I couldn't decrypt the message :( Please add me to an unencrypted room.`);
});
client.on("room.join", async (roomId: string, _event: any) => {
LogService.info("index", `Bot joined room ${roomId}`);
await client.sendMessage(roomId, {
"msgtype": "m.notice",
2022-12-29 02:38:29 +00:00
"body": `👋 Hello, I'm the ChatGPT bot! Encrypted message support: ${ matrixEncryption }`,
2022-12-09 09:41:17 +00:00
});
});
client.on("room.event", await handleRoomEvent(client, chatGPT));
LogService.info("index", "Starting bot...");
await client.start()
LogService.info("index", "Bot started!");
}
main();