Add a prefix to make the bot reply

This commit is contained in:
Juan Carlos Romero 2022-12-29 00:16:21 +01:00 committed by bertybuttface
parent bb1637e459
commit 42c8ce02f1
3 changed files with 18 additions and 2 deletions

View File

@ -46,9 +46,12 @@ MATRIX_BOT_USERNAME=
MATRIX_BOT_PASSWORD=
MATRIX_AUTO_JOIN=true
MATRIX_ENCRYPTION=true
# Leave prefix blank to reply to all messages
MATRIX_PREFIX=
# needs to be ./storage/ if you aren't using Docker or /storage/ if you are.
DATA_PATH=/storage/
```
# Discussion

View File

@ -16,6 +16,7 @@ export const matrixAutojoin = process.env.MATRIX_AUTO_JOIN && process.env.MATRIX
export const matrixEncryption = process.env.MATRIX_ENCRYPTION && process.env.MATRIX_ENCRYPTION.toLowerCase() === "true" as string;
export const dataPath = process.env.DATA_PATH as string;
export const matrixPrefix = process.env.MATRIX_PREFIX as string;
/** ChatGPT specific stuff */
export const openAiEmail = process.env.OPENAI_EMAIL as string;
@ -51,6 +52,10 @@ if(matrixEncryption === undefined) {
console.error("MATRIX_ENCRYPTION env variable is undefined");
process.exit(1);
}
if(matrixPrefix === undefined) {
console.error("MATRIX_PREFIX env variable is undefined");
process.exit(1);
}
if(openAiEmail === undefined) {
console.error("OPENAI_EMAIL env variable is undefined");
process.exit(1);

View File

@ -1,6 +1,6 @@
import { ChatGPTAPIBrowser } from "chatgpt";
import { MatrixClient } from "matrix-bot-sdk";
import { matrixBotUsername } from "./config.js";
import { matrixBotUsername, matrixPrefix } from "./config.js";
import { isEventAMessage } from "./utils.js";
/**
@ -19,7 +19,15 @@ export async function handleRoomEvent(client: MatrixClient, chatGPT: ChatGPTAPIB
}
if (isEventAMessage(event)) {
const question: string = event.content.body;
let question: string;
if (matrixPrefix){
if (!event.content.body.startsWith(matrixPrefix)){
return;
}
question = event.content.body.slice(matrixPrefix.length).trimStart();
} else {
question = event.content.body;
}
if (question === undefined) {
await client.sendReadReceipt(roomId, event.event_id);
await client.sendText(roomId,