-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmessages.js
61 lines (55 loc) · 1.72 KB
/
messages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import zulip from '../lib';
const stream = 'test-bot';
const config = {
username: process.env.ZULIP_USERNAME,
apiKey: process.env.ZULIP_API_KEY,
realm: process.env.ZULIP_REALM,
};
(async () => {
const z = await zulip(config);
// Send a message
const res = await z.messages.send({
to: stream,
type: 'stream',
subject: 'Testing zulip-js',
content: 'Something is wrong....',
});
// Response includes Message ID
console.log(res);
// Update the message
console.log(
await z.messages.update({
message_id: res.id,
content: 'New content',
})
);
const readParams = {
stream,
type: 'stream',
anchor: res.id,
num_before: 1,
num_after: 1,
};
// Fetch messages anchored around id (1 before, 1 after)
console.log(await z.messages.retrieve(readParams));
// Fetch most recent message
readParams.anchor = 1000000000;
console.log(await z.messages.retrieve(readParams));
// Get the id for the last message the user read
const resp = await z.users.me.pointer.retrieve();
// Fetch the messages around the last message that the user read
readParams.anchor = resp.pointer;
console.log(await z.messages.retrieve(readParams));
// Add a flag for the message that was sent
const flagParams = {
messages: [res.id],
flag: 'read',
};
console.log(await z.messages.flags.add(flagParams));
// Remove the flag for the message that was sent
console.log(await z.messages.flags.remove(flagParams));
console.log(await z.messages.getById({ message_id: 1 }));
console.log(await z.messages.getHistoryById({ message_id: 2 }));
console.log(await z.messages.deleteReactionById({ message_id: 1 }));
console.log(await z.messages.deleteById({ message_id: 1 }));
})();