|
| 1 | +import { |
| 2 | + EncryptionAlgorithm, |
| 3 | + FileMessageEventContent, |
| 4 | + LogLevel, |
| 5 | + LogService, |
| 6 | + MatrixClient, MessageEvent, |
| 7 | + RichConsoleLogger, |
| 8 | + SimpleFsStorageProvider, |
| 9 | +} from "../src"; |
| 10 | +import { SqliteCryptoStorageProvider } from "../src/storage/SqliteCryptoStorageProvider"; |
| 11 | +import * as fs from "fs"; |
| 12 | + |
| 13 | +LogService.setLogger(new RichConsoleLogger()); |
| 14 | +LogService.setLevel(LogLevel.TRACE); |
| 15 | +LogService.muteModule("Metrics"); |
| 16 | +LogService.trace = LogService.debug; |
| 17 | + |
| 18 | +let creds = null; |
| 19 | +try { |
| 20 | + creds = require("../../examples/storage/encryption_bot.creds.json"); |
| 21 | +} catch (e) { |
| 22 | + // ignore |
| 23 | +} |
| 24 | + |
| 25 | +const dmTarget = creds?.['dmTarget'] ?? "@admin:localhost"; |
| 26 | +const homeserverUrl = creds?.['homeserverUrl'] ?? "http://localhost:8008"; |
| 27 | +const accessToken = creds?.['accessToken'] ?? 'YOUR_TOKEN'; |
| 28 | +const storage = new SimpleFsStorageProvider("./examples/storage/encryption_bot.json"); |
| 29 | +const crypto = new SqliteCryptoStorageProvider("./examples/storage/encryption_bot.db"); |
| 30 | +const worksImage = fs.readFileSync("./examples/static/it-works.png"); |
| 31 | + |
| 32 | +const client = new MatrixClient(homeserverUrl, accessToken, storage, crypto); |
| 33 | + |
| 34 | +(async function() { |
| 35 | + let encryptedRoomId: string; |
| 36 | + const joinedRooms = await client.getJoinedRooms(); |
| 37 | + await client.crypto.prepare(joinedRooms); // init crypto because we're doing things before the client is started |
| 38 | + for (const roomId of joinedRooms) { |
| 39 | + if (await client.crypto.isRoomEncrypted(roomId)) { |
| 40 | + encryptedRoomId = roomId; |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + if (!encryptedRoomId) { |
| 45 | + encryptedRoomId = await client.createRoom({ |
| 46 | + invite: [dmTarget], |
| 47 | + is_direct: true, |
| 48 | + visibility: "private", |
| 49 | + preset: "trusted_private_chat", |
| 50 | + initial_state: [ |
| 51 | + {type: "m.room.encryption", state_key: "", content: {algorithm: EncryptionAlgorithm.MegolmV1AesSha2}}, |
| 52 | + {type: "m.room.guest_access", state_key: "", content: {guest_access: "can_join"}}, |
| 53 | + ], |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + client.on("room.failed_decryption", async (roomId: string, event: any, e: Error) => { |
| 58 | + LogService.error("index", `Failed to decrypt ${roomId} ${event['event_id']} because `, e); |
| 59 | + }); |
| 60 | + |
| 61 | + client.on("room.message", async (roomId: string, event: any) => { |
| 62 | + if (roomId !== encryptedRoomId) return; |
| 63 | + |
| 64 | + const message = new MessageEvent(event); |
| 65 | + |
| 66 | + if (message.sender === (await client.getUserId()) && message.messageType === "m.notice") { |
| 67 | + // yay, we decrypted our own message. Communicate that back for testing purposes. |
| 68 | + const encrypted = await client.crypto.encryptMedia(Buffer.from(worksImage)); |
| 69 | + const mxc = await client.uploadContent(encrypted.buffer); |
| 70 | + await client.sendMessage(roomId, { |
| 71 | + msgtype: "m.image", |
| 72 | + body: "it-works.png", |
| 73 | + info: { |
| 74 | + // XXX: We know these details, so have hardcoded them. |
| 75 | + w: 256, |
| 76 | + h: 256, |
| 77 | + mimetype: "image/png", |
| 78 | + size: worksImage.length, |
| 79 | + }, |
| 80 | + file: { |
| 81 | + url: mxc, |
| 82 | + ...encrypted.file, |
| 83 | + }, |
| 84 | + }); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (message.messageType === "m.image") { |
| 89 | + const fileEvent = new MessageEvent<FileMessageEventContent>(message.raw); |
| 90 | + const decrypted = await client.crypto.decryptMedia(fileEvent.content.file); |
| 91 | + fs.writeFileSync('./examples/storage/decrypted.png', decrypted); |
| 92 | + await client.unstableApis.addReactionToEvent(roomId, fileEvent.eventId, 'Decrypted'); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (message.messageType !== "m.text") return; |
| 97 | + if (message.textBody.startsWith("!ping")) { |
| 98 | + await client.replyNotice(roomId, event, "Pong"); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + LogService.info("index", "Starting bot..."); |
| 103 | + await client.start(); |
| 104 | +})(); |
0 commit comments