Skip to content

Commit dbdc6e5

Browse files
authored
Implement polling and message handling in legypolling.js (#122)
1 parent e4e4454 commit dbdc6e5

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

example/legy/legypolling.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)