matrix-chatgpt-bot/src/index.ts

95 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-02-04 16:58:43 +00:00
import { ChatGPTAPI } from 'chatgpt'
import Keyv from 'keyv'
import { KeyvFile } from 'keyv-file';
2022-12-09 09:41:17 +00:00
import {
2023-02-04 16:58:43 +00:00
MatrixAuth, MatrixClient, AutojoinRoomsMixin,
LogService, LogLevel, RichConsoleLogger,
ICryptoStorageProvider, RustSdkCryptoStorageProvider, IStorageProvider, SimpleFsStorageProvider,
2022-12-09 09:41:17 +00:00
} from "matrix-bot-sdk";
2022-12-29 02:48:50 +00:00
2022-12-29 02:38:29 +00:00
import * as path from "path";
2023-02-04 16:58:43 +00:00
import { DATA_PATH, KEYV_URL, OPENAI_API_KEY, MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, MATRIX_AUTOJOIN, MATRIX_BOT_PASSWORD, MATRIX_BOT_USERNAME, MATRIX_ENCRYPTION, MATRIX_THREADS, CHATGPT_CONTEXT, CHATGPT_MODEL, KEYV_BOT_ENCRYPTION, KEYV_BOT_STORAGE, KEYV_BACKEND } from './env.js'
2023-01-06 23:59:52 +00:00
import CommandHandler from "./handlers.js"
2023-02-04 16:58:43 +00:00
import { KeyvCryptoStorageProvider, KeyvStorageProvider } from './storage.js'
import { parseMatrixUsernamePretty } from './utils.js';
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;
2023-02-04 16:58:43 +00:00
if (KEYV_URL && KEYV_BACKEND === 'file') LogService.warn('config', 'KEYV_URL is ignored when KEYV_BACKEND is set to `file`')
let chatgptStore:Keyv
if (KEYV_BACKEND === 'file'){
chatgptStore = new Keyv({store: new KeyvFile({ filename: path.join(DATA_PATH, `chatgpt-bot-api.json`),})})
} else {
chatgptStore = new Keyv(KEYV_URL, { namespace: 'chatgpt-bot-api' });
}
let storage: IStorageProvider
if (KEYV_BOT_STORAGE) {
storage = new KeyvStorageProvider('chatgpt-bot-storage');
} else {
storage = new SimpleFsStorageProvider(path.join(DATA_PATH, "bot.json")); // /storage/bot.json
}
2022-12-09 09:41:17 +00:00
2022-12-29 02:38:29 +00:00
let cryptoStore: ICryptoStorageProvider;
2022-12-29 11:11:52 +00:00
if (MATRIX_ENCRYPTION) {
2023-02-04 16:58:43 +00:00
if (KEYV_BOT_ENCRYPTION) {
cryptoStore = new KeyvCryptoStorageProvider('chatgpt-bot-encryption');
} else {
cryptoStore = new RustSdkCryptoStorageProvider(path.join(DATA_PATH, "encrypted")); // /storage/encrypted
}
2022-12-29 02:38:29 +00:00
}
2022-12-09 09:41:17 +00:00
async function main() {
2022-12-29 11:11:52 +00:00
if (!MATRIX_ACCESS_TOKEN){
const botUsernameWithoutDomain = parseMatrixUsernamePretty(MATRIX_BOT_USERNAME);
2022-12-29 11:11:52 +00:00
const authedClient = await (new MatrixAuth(MATRIX_HOMESERVER_URL)).passwordLogin(botUsernameWithoutDomain, MATRIX_BOT_PASSWORD);
console.log(authedClient.homeserverUrl + " token: \n" + authedClient.accessToken)
2022-12-29 11:11:52 +00:00
console.log("Set MATRIX_ACCESS_TOKEN to above token, MATRIX_BOT_PASSWORD can now be blank")
return;
}
if (!MATRIX_THREADS && CHATGPT_CONTEXT !== "room") throw Error("You must set CHATGPT_CONTEXT to 'room' if you set MATRIX_THREADS to false")
const client: MatrixClient = new MatrixClient(MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, storage, cryptoStore);
2023-02-02 12:44:14 +00:00
const chatGPT: ChatGPTAPI = new ChatGPTAPI({
2023-02-03 10:35:49 +00:00
apiKey: OPENAI_API_KEY,
completionParams: {
2023-02-04 16:58:43 +00:00
model: CHATGPT_MODEL,
},
messageStore: chatgptStore
2022-12-09 09:41:17 +00:00
})
2022-12-09 09:41:17 +00:00
// Automatically join rooms the bot is invited to
2022-12-29 11:11:52 +00:00
if (MATRIX_AUTOJOIN) {
2022-12-29 02:38:29 +00:00
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",
2023-01-12 18:03:23 +00:00
"body": `👋 Hello, I'm the ChatGPT bot! Encrypted message support: ${MATRIX_ENCRYPTION}`,
2022-12-09 09:41:17 +00:00
});
});
2023-01-06 23:59:52 +00:00
// Prepare the command handler
const commands = new CommandHandler(client, chatGPT);
await commands.start();
2022-12-09 09:41:17 +00:00
2023-02-03 10:35:49 +00:00
LogService.info("index", `Starting bot using ChatGPT model ${CHATGPT_MODEL}`);
2022-12-09 09:41:17 +00:00
await client.start()
LogService.info("index", "Bot started!");
}
main();