|
| 1 | +import './setup-logger'; // This should be first |
| 2 | +import { RoomMemberEvent, RoomEvent, createClient } from 'matrix-js-sdk'; |
| 3 | +import { PgAdapter, PgQueuePublisher } from '@cardstack/postgres'; |
| 4 | +import { logger } from '@cardstack/runtime-common'; |
| 5 | + |
| 6 | +const log = logger('bot-runner'); |
| 7 | +const startTime = Date.now(); |
| 8 | + |
| 9 | +const matrixUrl = process.env.MATRIX_URL || 'http://localhost:8008'; |
| 10 | +const botUsername = process.env.BOT_RUNNER_USERNAME || 'bot-runner'; |
| 11 | +const botPassword = process.env.BOT_RUNNER_PASSWORD || 'password'; |
| 12 | + |
| 13 | +(async () => { |
| 14 | + let client = createClient({ |
| 15 | + baseUrl: matrixUrl, |
| 16 | + }); |
| 17 | + |
| 18 | + let auth = await client.loginWithPassword(botUsername, botPassword).catch( |
| 19 | + (error) => { |
| 20 | + log.error(error); |
| 21 | + log.error( |
| 22 | + `Bot runner could not login to Matrix at ${matrixUrl}. Check credentials and server availability.`, |
| 23 | + ); |
| 24 | + process.exit(1); |
| 25 | + }, |
| 26 | + ); |
| 27 | + |
| 28 | + log.info(`logged in as ${auth.user_id}`); |
| 29 | + |
| 30 | + let dbAdapter = new PgAdapter(); |
| 31 | + let queuePublisher = new PgQueuePublisher(dbAdapter); |
| 32 | + |
| 33 | + const shutdown = async () => { |
| 34 | + log.info('shutting down bot runner...'); |
| 35 | + try { |
| 36 | + await queuePublisher.destroy(); |
| 37 | + await dbAdapter.close(); |
| 38 | + } catch (error) { |
| 39 | + log.error('error during shutdown', error); |
| 40 | + process.exit(1); |
| 41 | + } |
| 42 | + process.exit(0); |
| 43 | + }; |
| 44 | + |
| 45 | + process.on('SIGINT', shutdown); |
| 46 | + process.on('SIGTERM', shutdown); |
| 47 | + |
| 48 | + client.on(RoomMemberEvent.Membership, function (event, member) { |
| 49 | + if (event.event.origin_server_ts! < startTime) { |
| 50 | + return; |
| 51 | + } |
| 52 | + if (member.membership === 'invite' && member.userId === auth.user_id) { |
| 53 | + client |
| 54 | + .joinRoom(member.roomId) |
| 55 | + .then(function () { |
| 56 | + log.info('%s auto-joined %s', member.name, member.roomId); |
| 57 | + }) |
| 58 | + .catch(function (err) { |
| 59 | + log.info( |
| 60 | + 'Error joining room after invite (user may have left before join)', |
| 61 | + err, |
| 62 | + ); |
| 63 | + }); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + client.on(RoomEvent.Timeline, async (event, room, toStartOfTimeline) => { |
| 68 | + if (!room || toStartOfTimeline) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + let senderMatrixUserId = event.getSender(); |
| 73 | + if (!senderMatrixUserId || senderMatrixUserId === auth.user_id) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + let eventBody = event.getContent()?.body || ''; |
| 78 | + log.info( |
| 79 | + 'received event in room %s (%s): %s', |
| 80 | + room.name, |
| 81 | + room.roomId, |
| 82 | + eventBody, |
| 83 | + ); |
| 84 | + |
| 85 | + // TODO: enqueue bot command job via queuePublisher.publish(...) |
| 86 | + }); |
| 87 | + |
| 88 | + client.startClient(); |
| 89 | + log.info('bot runner listening for Matrix events'); |
| 90 | +})().catch((error) => { |
| 91 | + log.error('bot runner failed to start', error); |
| 92 | + process.exit(1); |
| 93 | +}); |
0 commit comments