Hotfix last PR

We still need that util function for now, can remove later. Also we don't need MATRIX_BOT_USERNAME any more if we have MATRIX_ACCESS_TOKEN set.
This commit is contained in:
bertybuttface 2023-01-07 00:30:09 +00:00
parent 16f2852303
commit 49d0ae0d60
4 changed files with 11 additions and 7 deletions

View File

@ -29,9 +29,9 @@ export const {
DATA_PATH: {schema: z.string().default("./storage"), description: "Set to /storage/ if using docker, ./storage if running without"},
/** Matrix Bot Settings */
MATRIX_HOMESERVER_URL: {schema: z.string().default("https://matrix.org")},
MATRIX_ACCESS_TOKEN: {schema: z.string().optional(), description: "Set MATRIX_BOT_USERNAME & MATRIX_BOT_PASSWORD to print ACCESS_TOKEN or follow https://webapps.stackexchange.com/questions/131056/how-to-get-an-access-token-for-element-riot-matrix"},
MATRIX_BOT_USERNAME: {schema: z.string().min(3), description: "Set full username: eg @bot:server.com"},
MATRIX_BOT_PASSWORD: {schema: z.string().optional(), description: "Set AccessToken which supersedes MATRIX_BOT_PASSWORD"},
MATRIX_ACCESS_TOKEN: {schema: z.string().optional(), description: "Set MATRIX_BOT_USERNAME & MATRIX_BOT_PASSWORD to print MATRIX_ACCESS_TOKEN or follow https://webapps.stackexchange.com/questions/131056/how-to-get-an-access-token-for-element-riot-matrix"},
MATRIX_BOT_USERNAME: {schema: z.string().optional(), description: "Set full username: eg @bot:server.com (superseded by MATRIX_ACCESS_TOKEN if set)"},
MATRIX_BOT_PASSWORD: {schema: z.string().optional(), description: "Set password (superseded by MATRIX_ACCESS_TOKEN if set)"},
/** Matrix Bot Features */
MATRIX_AUTOJOIN: {schema: z.boolean().default(true)},
MATRIX_ENCRYPTION: {schema: z.boolean().default(true)},

View File

@ -1,6 +1,6 @@
import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt";
import { LogService, MatrixClient, UserID } from "matrix-bot-sdk";
import { CHATGPT_TIMEOUT, MATRIX_BOT_USERNAME, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX} from "./env.js";
import { CHATGPT_TIMEOUT, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX} from "./env.js";
import { RelatesTo, StoredConversation, StoredConversationConfig } from "./interfaces.js";
import { sendError, sendThreadReply } from "./utils.js";
@ -31,12 +31,12 @@ export default class CommandHandler {
}
/**
* Run when *any* room message is received. The bot only sends a message if needed.
* Run when `message` room event is received. The bot only sends a message if needed.
* @returns Room event handler, which itself returnings nothing
*/
private async onMessage(roomId: string, event: any) {
try {
if (event.sender === MATRIX_BOT_USERNAME) return; // Ignore ourself
if (event.sender === this.userId) return; // Ignore ourselves
if (Date.now() - event.origin_server_ts > 10000) return; // Ignore old messages
const relatesTo: RelatesTo | undefined = event.content["m.relates_to"];
if ((relatesTo !== undefined) && (relatesTo["rel_type"] === "m.replace")) return; // Ignore edits

View File

@ -31,8 +31,8 @@ if (MATRIX_ENCRYPTION) {
}
async function main() {
const botUsernameWithoutDomain = parseMatrixUsernamePretty(MATRIX_BOT_USERNAME);
if (!MATRIX_ACCESS_TOKEN){
const botUsernameWithoutDomain = parseMatrixUsernamePretty(MATRIX_BOT_USERNAME);
const authedClient = await (new MatrixAuth(MATRIX_HOMESERVER_URL)).passwordLogin(botUsernameWithoutDomain, MATRIX_BOT_PASSWORD);
console.log(authedClient.homeserverUrl + " token: \n" + authedClient.accessToken)
console.log("Set MATRIX_ACCESS_TOKEN to above token, MATRIX_BOT_PASSWORD can now be blank")

View File

@ -9,6 +9,10 @@ export function parseMatrixUsernamePretty(matrix_username: string): string {
return withoutUrl.split('@')[1]
}
export function isEventAMessage(event: any): event is MessageEvent {
return event.type === 'm.room.message'
}
export async function sendError(client: MatrixClient, text: string, roomId: string, eventId: string): Promise<void> {
Promise.all([client.setTyping(roomId, false, 500), client.sendText(roomId, text), client.sendReadReceipt(roomId, eventId)]);
}