|
| 1 | +import { base, LINEStruct } from "./legyfetch.js"; |
| 2 | + |
| 3 | +// patch |
| 4 | +base.talk.sync = async function (options = {}) { |
| 5 | + const { limit, revision, individualRev, globalRev, timeout } = { |
| 6 | + limit: 100, revision: 0, globalRev: 0, individualRev: 0, |
| 7 | + timeout: base.config.longTimeout, |
| 8 | + ...options, |
| 9 | + }; |
| 10 | + return await base.request.request( |
| 11 | + LINEStruct.sync_args({ |
| 12 | + request: { |
| 13 | + lastRevision: revision, |
| 14 | + lastGlobalRevision: globalRev, |
| 15 | + lastIndividualRevision: individualRev, |
| 16 | + count: limit, |
| 17 | + }, |
| 18 | + }), |
| 19 | + "sync", 3, true, "/SYNC3", {}, timeout, |
| 20 | + ); |
| 21 | +}; |
| 22 | + |
| 23 | +const profile = await base.talk.getProfile(); |
| 24 | +console.log(`Logged in: ${profile.displayName} (${profile.mid})`); |
| 25 | +const polling = base.createPolling(); |
| 26 | +for await (const op of polling._listenTalkEvents({ pollingInterval: 1000 })) { |
| 27 | + if (op.type === "RECEIVE_MESSAGE" || op.type === "SEND_MESSAGE") { |
| 28 | + const message = op.message; |
| 29 | + let text = message?.text; |
| 30 | + if (message?.chunks) { |
| 31 | + try { |
| 32 | + const decrypted = await base.e2ee.decryptE2EEMessage(message); |
| 33 | + text = decrypted.text; |
| 34 | + } catch (e) { |
| 35 | + } |
| 36 | + } |
| 37 | + if (text === "!ping") { |
| 38 | + const replyTo = message.to === profile.mid ? message.from : message.to; |
| 39 | + await base.talk.sendMessage({ |
| 40 | + to: replyTo, |
| 41 | + text: "pong!", |
| 42 | + e2ee: !!message.chunks, |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments