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