Compare commits
1 Commits
main
...
bfe7cb2790
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bfe7cb2790 |
@@ -5,5 +5,9 @@ MATRIX_HOME_SERVER=https://matrix.org
|
||||
MATRIX_ACCESS_TOKEN=your_real_access_token_here
|
||||
BOT_COMMAND=!quote
|
||||
|
||||
# Stable device ID for the bot's E2EE identity. Keep this consistent
|
||||
# across restarts so the bot reuses its encryption keys.
|
||||
MATRIX_DEVICE_ID=ANCESTORBOT
|
||||
|
||||
# If you want debug logging, set DEBUG=true
|
||||
DEBUG=false
|
||||
|
||||
58
index.js
58
index.js
@@ -1,16 +1,17 @@
|
||||
import * as dotenv from 'dotenv';
|
||||
import { MatrixClient } from 'matrix-js-sdk';
|
||||
import { MemoryStore } from 'matrix-js-sdk/lib/store/memory.js';
|
||||
import fs from 'fs';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
console.log('🚀 Starting Ancestor Quote Bot...\n');
|
||||
|
||||
const deviceId = process.env.MATRIX_DEVICE_ID || 'ANCESTORBOT';
|
||||
|
||||
const client = new MatrixClient({
|
||||
accessToken: process.env.MATRIX_ACCESS_TOKEN,
|
||||
baseUrl: process.env.MATRIX_HOME_SERVER,
|
||||
store: new MemoryStore(),
|
||||
deviceId,
|
||||
});
|
||||
|
||||
function loadQuotes() {
|
||||
@@ -56,7 +57,14 @@ function loadQuotes() {
|
||||
}
|
||||
|
||||
client.credentials.userId = userId;
|
||||
console.log(`👤 Bot Identity: ${userId}`);
|
||||
console.log(`👤 Bot Identity: ${userId} (device ${deviceId})`);
|
||||
|
||||
// Enable end-to-end encryption so the bot can read and reply in
|
||||
// encrypted rooms. The Rust/Olm WASM backend needs no native
|
||||
// compile. useIndexedDB=false keeps everything in memory (no
|
||||
// IndexedDB in Node).
|
||||
await client.initRustCrypto({ useIndexedDB: false });
|
||||
console.log('🔐 E2EE crypto initialised (Rust/Olm WASM)');
|
||||
|
||||
await client.startClient();
|
||||
console.log('✅ MatrixClient connected and authenticated\n');
|
||||
@@ -69,7 +77,6 @@ function loadQuotes() {
|
||||
console.log('');
|
||||
|
||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||
console.log(`🔊 Listening for command: "${cmd}"`);
|
||||
|
||||
// Auto-accept invites so the bot can be invited into rooms.
|
||||
client.on('RoomMember.membership', (event, member) => {
|
||||
@@ -85,48 +92,37 @@ function loadQuotes() {
|
||||
// Respond to messages in any room this bot is a member of.
|
||||
client.on('event', (event) => {
|
||||
const type = event.getType();
|
||||
const eventRoomId = event.getRoomId();
|
||||
|
||||
// Encrypted rooms: the bot has no E2EE support, so command text
|
||||
// never arrives as plaintext and cannot be read or answered.
|
||||
// If an event still arrives encrypted, decryption failed — the
|
||||
// room is likely set to only share keys with verified sessions.
|
||||
if (type === 'm.room.encrypted') {
|
||||
console.log(`🔒 [${eventRoomId}] encrypted message received. ` +
|
||||
`The bot cannot read E2EE rooms; invite it to an unencrypted room instead.`);
|
||||
console.log(`🔒 [${event.getRoomId()}] could not decrypt message. ` +
|
||||
`The room may be set to share keys only with verified sessions; ` +
|
||||
`change room encryption settings to include this bot's device, or verify it.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (type !== 'm.room.message') return;
|
||||
|
||||
const sender = event.getSender();
|
||||
if (sender === userId) return; // skip replies from ourselves
|
||||
if (sender === userId) return;
|
||||
|
||||
const room = eventRoomId ? client.getRoom(eventRoomId) : null;
|
||||
if (!room) {
|
||||
console.log(`ℹ️ [debug] got m.room.message for unknown room: ${eventRoomId}`);
|
||||
return;
|
||||
}
|
||||
const room = client.getRoom(event.getRoomId());
|
||||
if (!room) return;
|
||||
|
||||
const membership = room.getMyMembership();
|
||||
if (membership !== 'join') {
|
||||
console.log(`ℹ️ [debug] [${room.roomId}] m.room.message while membership="${membership}" — not replying`);
|
||||
return;
|
||||
}
|
||||
if (membership !== 'join') return;
|
||||
|
||||
const content = event.getContent();
|
||||
if (!content || typeof content.body !== 'string') {
|
||||
console.log(`ℹ️ [debug] [${room.roomId}] m.room.message without text body`);
|
||||
return;
|
||||
}
|
||||
if (!content || typeof content.body !== 'string') return;
|
||||
|
||||
const lower = content.body.toLowerCase().trim();
|
||||
const matches = lower === cmd || lower.startsWith(cmd + ' ');
|
||||
console.log(`💬 [${room.roomId}] <${sender}> "${content.body}" (listening for "${cmd}", match=${matches})`);
|
||||
if (!matches) return;
|
||||
|
||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||
client.sendTextMessage(room.roomId, quote)
|
||||
.then(() => console.log(`🎃 [${room.roomId}] ${quote}`))
|
||||
.catch((err) => console.error('Failed to send quote:', err.message));
|
||||
if (lower === cmd || lower.startsWith(cmd + ' ')) {
|
||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||
client.sendTextMessage(room.roomId, quote)
|
||||
.then(() => console.log(`🎃 [${room.roomId}] ${quote}`))
|
||||
.catch((err) => console.error('Failed to send quote:', err.message));
|
||||
}
|
||||
});
|
||||
|
||||
console.log('🤖 Bot is running!');
|
||||
|
||||
Reference in New Issue
Block a user