2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-02 02:22:10 +02:00
|
|
|
/*
|
2020-09-15 00:24:01 +02:00
|
|
|
Zulip's OpenAPI-based API documentation system is documented at
|
|
|
|
https://zulip.readthedocs.io/en/latest/documentation/api.html
|
|
|
|
|
|
|
|
This file contains example code fenced off by comments, and is
|
|
|
|
designed to be run as part of Zulip's test-api test suite to verify
|
|
|
|
that the documented examples are all correct, runnable code.
|
2020-07-02 02:22:10 +02:00
|
|
|
*/
|
|
|
|
|
2020-12-11 04:26:23 +01:00
|
|
|
const examples_handler = function () {
|
2020-05-17 11:37:18 +02:00
|
|
|
const config = {
|
|
|
|
username: process.env.ZULIP_USERNAME,
|
|
|
|
apiKey: process.env.ZULIP_API_KEY,
|
|
|
|
realm: process.env.ZULIP_REALM,
|
|
|
|
};
|
2020-05-19 18:39:17 +02:00
|
|
|
const examples = {};
|
|
|
|
const response_data = [];
|
2020-05-17 11:37:18 +02:00
|
|
|
|
|
|
|
const make_result_object = (example, result, count = false) => {
|
|
|
|
const name = count !== false ? `${example.name}_${count}` : example.name;
|
|
|
|
return {
|
|
|
|
name,
|
2020-07-15 01:29:15 +02:00
|
|
|
endpoint: example.endpoint.split(":")[0],
|
|
|
|
method: example.endpoint.split(":")[1],
|
2020-05-17 11:37:18 +02:00
|
|
|
status_code: example.status_code.toString(),
|
|
|
|
result,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-19 18:39:17 +02:00
|
|
|
const generate_validation_data = async (client, example) => {
|
2020-12-11 05:39:23 +01:00
|
|
|
let count = 0;
|
|
|
|
const console = {
|
|
|
|
log(result) {
|
|
|
|
response_data.push(make_result_object(example, result, count));
|
|
|
|
count += 1;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
await example.func(client, console);
|
2020-05-17 11:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const main = async () => {
|
2020-12-11 04:26:23 +01:00
|
|
|
const zulipInit = require("zulip-js");
|
|
|
|
const client = await zulipInit(config);
|
2020-05-19 18:39:17 +02:00
|
|
|
|
|
|
|
await generate_validation_data(client, examples.send_message);
|
|
|
|
await generate_validation_data(client, examples.create_user);
|
2020-06-21 20:43:09 +02:00
|
|
|
await generate_validation_data(client, examples.get_custom_emoji);
|
2020-06-21 20:51:21 +02:00
|
|
|
await generate_validation_data(client, examples.delete_queue);
|
2020-06-21 21:46:53 +02:00
|
|
|
await generate_validation_data(client, examples.get_messages);
|
2020-06-21 21:58:07 +02:00
|
|
|
await generate_validation_data(client, examples.get_own_user);
|
2020-06-21 22:25:48 +02:00
|
|
|
await generate_validation_data(client, examples.get_stream_id);
|
2020-06-21 22:32:42 +02:00
|
|
|
await generate_validation_data(client, examples.get_stream_topics);
|
2020-06-21 22:41:13 +02:00
|
|
|
await generate_validation_data(client, examples.get_subscriptions);
|
2020-06-21 22:51:47 +02:00
|
|
|
await generate_validation_data(client, examples.get_users);
|
2020-06-21 22:59:39 +02:00
|
|
|
await generate_validation_data(client, examples.register_queue);
|
2020-06-21 23:18:59 +02:00
|
|
|
await generate_validation_data(client, examples.render_message);
|
2020-06-21 23:25:38 +02:00
|
|
|
await generate_validation_data(client, examples.set_typing_status);
|
2020-06-21 23:37:53 +02:00
|
|
|
await generate_validation_data(client, examples.add_subscriptions);
|
2020-06-21 23:46:13 +02:00
|
|
|
await generate_validation_data(client, examples.remove_subscriptions);
|
2020-06-22 00:13:00 +02:00
|
|
|
await generate_validation_data(client, examples.update_message_flags);
|
2020-06-22 00:31:56 +02:00
|
|
|
await generate_validation_data(client, examples.update_message);
|
2020-06-29 18:14:47 +02:00
|
|
|
await generate_validation_data(client, examples.get_events);
|
2020-06-22 00:45:16 +02:00
|
|
|
await generate_validation_data(client, examples.get_streams);
|
2020-05-19 18:39:17 +02:00
|
|
|
|
|
|
|
console.log(JSON.stringify(response_data));
|
|
|
|
return;
|
2020-05-17 11:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const add_example = (name, endpoint, status_code, func) => {
|
2020-05-19 18:39:17 +02:00
|
|
|
const example = {
|
2020-05-17 11:37:18 +02:00
|
|
|
name,
|
|
|
|
endpoint,
|
|
|
|
status_code,
|
|
|
|
func,
|
2020-05-19 18:39:17 +02:00
|
|
|
};
|
|
|
|
examples[name] = example;
|
2020-05-17 11:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
main,
|
|
|
|
add_example,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-12-11 04:26:23 +01:00
|
|
|
const {main, add_example} = examples_handler();
|
2020-05-17 11:37:18 +02:00
|
|
|
|
2020-07-10 00:17:33 +02:00
|
|
|
const send_test_message = async (client) => {
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
to: "Verona",
|
|
|
|
type: "stream",
|
|
|
|
topic: "Castle",
|
2020-07-10 00:17:33 +02:00
|
|
|
// Use some random text for easier debugging if needed. We don't
|
|
|
|
// depend on the content of these messages for the tests.
|
|
|
|
content: `Random test message ${Math.random()}`,
|
|
|
|
};
|
|
|
|
const result = await client.messages.send(params);
|
|
|
|
// Only return the message id.
|
|
|
|
return result.id;
|
|
|
|
};
|
|
|
|
|
2020-05-17 11:37:18 +02:00
|
|
|
// Declare all the examples below.
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("send_message", "/messages:post", 200, async (client, console) => {
|
2020-05-17 12:29:49 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Send a stream message
|
|
|
|
let params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
to: "social",
|
|
|
|
type: "stream",
|
|
|
|
topic: "Castle",
|
|
|
|
content: "I come not, friends, to steal away your hearts.",
|
2020-05-17 12:29:49 +02:00
|
|
|
};
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.messages.send(params));
|
2020-05-17 12:29:49 +02:00
|
|
|
|
2023-04-17 17:02:07 +02:00
|
|
|
// Send a direct message
|
2020-05-17 12:29:49 +02:00
|
|
|
const user_id = 9;
|
|
|
|
params = {
|
|
|
|
to: [user_id],
|
2023-04-17 17:02:07 +02:00
|
|
|
type: "direct",
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "With mirth and laughter let old wrinkles come.",
|
2020-05-17 12:29:49 +02:00
|
|
|
};
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.messages.send(params));
|
2020-05-17 12:29:49 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("create_user", "/users:post", 200, async (client, console) => {
|
2020-05-17 12:26:12 +02:00
|
|
|
// {code_example|start}
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "notnewbie@zulip.com",
|
|
|
|
password: "temp",
|
|
|
|
full_name: "New User",
|
2020-05-17 12:26:12 +02:00
|
|
|
};
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.users.create(params));
|
2020-05-17 12:26:12 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("get_custom_emoji", "/realm/emoji:get", 200, async (client, console) => {
|
2020-06-21 20:43:09 +02:00
|
|
|
// {code_example|start}
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.emojis.retrieve());
|
2020-06-21 20:43:09 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("delete_queue", "/events:delete", 200, async (client, console) => {
|
2020-06-21 20:51:21 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Register a queue
|
|
|
|
const queueParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
event_types: ["message"],
|
2020-06-21 20:51:21 +02:00
|
|
|
};
|
|
|
|
const res = await client.queues.register(queueParams);
|
|
|
|
|
|
|
|
// Delete a queue
|
|
|
|
const deregisterParams = {
|
|
|
|
queue_id: res.queue_id,
|
|
|
|
};
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.queues.deregister(deregisterParams));
|
2020-06-21 20:51:21 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("get_messages", "/messages:get", 200, async (client, console) => {
|
2020-06-21 21:46:53 +02:00
|
|
|
// {code_example|start}
|
|
|
|
const readParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
anchor: "newest",
|
2020-06-21 21:46:53 +02:00
|
|
|
num_before: 100,
|
|
|
|
num_after: 0,
|
2020-07-15 00:34:28 +02:00
|
|
|
narrow: [
|
|
|
|
{operator: "sender", operand: "iago@zulip.com"},
|
|
|
|
{operator: "stream", operand: "Verona"},
|
|
|
|
],
|
2020-06-21 21:46:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get the 100 last messages sent by "iago@zulip.com" to the stream "Verona"
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.messages.retrieve(readParams));
|
2020-06-21 21:46:53 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("get_own_user", "/users/me:get", 200, async (client, console) => {
|
2020-06-21 21:58:07 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get the profile of the user/bot that requests this endpoint,
|
|
|
|
// which is `client` in this case:
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.users.me.getProfile());
|
2020-06-21 21:58:07 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("get_stream_id", "/get_stream_id:get", 200, async (client, console) => {
|
2020-06-21 22:25:48 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get the ID of a given stream
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.streams.getStreamId("Denmark"));
|
2020-06-21 22:25:48 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example(
|
|
|
|
"get_stream_topics",
|
|
|
|
"/users/me/{stream_id}/topics:get",
|
|
|
|
200,
|
|
|
|
async (client, console) => {
|
|
|
|
// {code_example|start}
|
|
|
|
// Get all the topics in stream with ID 1
|
|
|
|
console.log(await client.streams.topics.retrieve({stream_id: 1}));
|
|
|
|
// {code_example|end}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
add_example("get_subscriptions", "/users/me/subscriptions:get", 200, async (client, console) => {
|
2020-06-21 22:41:13 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get all streams that the user is subscribed to
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.streams.subscriptions.retrieve());
|
2020-06-21 22:41:13 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("get_users", "/users:get", 200, async (client, console) => {
|
2020-06-21 22:51:47 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get all users in the realm
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.users.retrieve());
|
2020-06-21 22:51:47 +02:00
|
|
|
|
|
|
|
// You may pass the `client_gravatar` query parameter as follows:
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.users.retrieve({client_gravatar: true}));
|
2020-06-21 22:51:47 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("register_queue", "/register:post", 200, async (client, console) => {
|
2020-06-21 22:59:39 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Register a queue
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
event_types: ["message"],
|
2020-06-21 22:59:39 +02:00
|
|
|
};
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.queues.register(params));
|
2020-06-21 22:59:39 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("render_message", "/messages/render:post", 200, async (client, console) => {
|
2020-06-21 23:18:59 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Render a message
|
|
|
|
const params = {
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "**foo**",
|
2020-06-21 23:18:59 +02:00
|
|
|
};
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.messages.render(params));
|
2020-06-21 23:18:59 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("set_typing_status", "/typing:post", 200, async (client, console) => {
|
2020-06-21 23:25:38 +02:00
|
|
|
// {code_example|start}
|
|
|
|
const user_id1 = 9;
|
|
|
|
const user_id2 = 10;
|
|
|
|
|
|
|
|
const typingParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
op: "start",
|
2020-06-21 23:25:38 +02:00
|
|
|
to: [user_id1, user_id2],
|
|
|
|
};
|
|
|
|
|
2023-04-17 17:02:07 +02:00
|
|
|
// The user has started typing in the group direct message
|
|
|
|
// with Iago and Polonius
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.typing.send(typingParams));
|
2020-06-21 23:25:38 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("add_subscriptions", "/users/me/subscriptions:post", 200, async (client, console) => {
|
2020-06-21 23:37:53 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Subscribe to the streams "Verona" and "Denmark"
|
|
|
|
const meParams = {
|
2020-07-15 00:34:28 +02:00
|
|
|
subscriptions: JSON.stringify([{name: "Verona"}, {name: "Denmark"}]),
|
2020-06-21 23:37:53 +02:00
|
|
|
};
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.users.me.subscriptions.add(meParams));
|
2020-06-21 23:37:53 +02:00
|
|
|
|
|
|
|
// To subscribe another user to a stream, you may pass in
|
|
|
|
// the `principals` parameter, like so:
|
|
|
|
const user_id = 7;
|
|
|
|
const anotherUserParams = {
|
2020-07-15 00:34:28 +02:00
|
|
|
subscriptions: JSON.stringify([{name: "Verona"}, {name: "Denmark"}]),
|
2020-06-21 23:37:53 +02:00
|
|
|
principals: JSON.stringify([user_id]),
|
|
|
|
};
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.users.me.subscriptions.add(anotherUserParams));
|
2020-06-21 23:37:53 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example(
|
|
|
|
"remove_subscriptions",
|
|
|
|
"/users/me/subscriptions:delete",
|
|
|
|
200,
|
|
|
|
async (client, console) => {
|
|
|
|
// {code_example|start}
|
|
|
|
// Unsubscribe from the stream "Denmark"
|
|
|
|
const meParams = {
|
|
|
|
subscriptions: JSON.stringify(["Denmark"]),
|
|
|
|
};
|
|
|
|
console.log(await client.users.me.subscriptions.remove(meParams));
|
2020-06-21 23:46:13 +02:00
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
const user_id = 7;
|
|
|
|
// Unsubscribe Zoe from the stream "Denmark"
|
|
|
|
const zoeParams = {
|
|
|
|
subscriptions: JSON.stringify(["Denmark"]),
|
|
|
|
principals: JSON.stringify([user_id]),
|
|
|
|
};
|
|
|
|
console.log(await client.users.me.subscriptions.remove(zoeParams));
|
|
|
|
// {code_example|end}
|
|
|
|
},
|
|
|
|
);
|
2020-06-21 23:46:13 +02:00
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("update_message_flags", "/messages/flags:post", 200, async (client, console) => {
|
2020-07-10 00:17:33 +02:00
|
|
|
// Send 3 messages to run this example on
|
2022-12-07 00:59:08 +01:00
|
|
|
const message_ids = [];
|
|
|
|
for (let i = 0; i < 3; i += 1) {
|
|
|
|
message_ids.push(await send_test_message(client));
|
2020-07-10 00:17:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 00:13:00 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Add the "read" flag to the messages with IDs in "message_ids"
|
|
|
|
const addflag = {
|
|
|
|
messages: message_ids,
|
2020-07-15 01:29:15 +02:00
|
|
|
flag: "read",
|
2020-06-22 00:13:00 +02:00
|
|
|
};
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.messages.flags.add(addflag));
|
2020-06-22 00:13:00 +02:00
|
|
|
|
|
|
|
// Remove the "starred" flag from the messages with IDs in "message_ids"
|
|
|
|
const removeflag = {
|
|
|
|
messages: message_ids,
|
2020-07-15 01:29:15 +02:00
|
|
|
flag: "starred",
|
2020-06-22 00:13:00 +02:00
|
|
|
};
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.messages.flags.remove(removeflag));
|
2020-06-22 00:13:00 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("update_message", "/messages/{message_id}:patch", 200, async (client, console) => {
|
2020-06-22 00:31:56 +02:00
|
|
|
const request = {
|
2020-07-15 01:29:15 +02:00
|
|
|
to: "Denmark",
|
|
|
|
type: "stream",
|
|
|
|
topic: "Castle",
|
|
|
|
content: "I come not, friends, to steal away your hearts.",
|
2020-06-22 00:31:56 +02:00
|
|
|
};
|
|
|
|
const result = await client.messages.send(request);
|
|
|
|
const message_id = result.id;
|
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
// Update a message with the given "message_id"
|
|
|
|
const params = {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "New Content",
|
2020-06-22 00:31:56 +02:00
|
|
|
};
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.messages.update(params));
|
2020-06-22 00:31:56 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("get_events", "/events:get", 200, async (client, console) => {
|
2020-06-29 18:14:47 +02:00
|
|
|
// Register queue to receive messages for user.
|
|
|
|
const queueParams = {
|
2020-07-15 01:29:15 +02:00
|
|
|
event_types: ["message"],
|
2020-06-29 18:14:47 +02:00
|
|
|
};
|
|
|
|
const res = await client.queues.register(queueParams);
|
|
|
|
const queue_id = res.queue_id;
|
|
|
|
// For setup, we send a message to ensure there are events in the
|
|
|
|
// queue; this lets the automated tests complete quickly.
|
2020-07-10 00:17:33 +02:00
|
|
|
await send_test_message(client);
|
2020-06-29 18:14:47 +02:00
|
|
|
|
|
|
|
// {code_example|start}
|
|
|
|
// Retrieve events from a queue with given "queue_id"
|
|
|
|
const eventParams = {
|
2020-07-20 22:18:43 +02:00
|
|
|
queue_id,
|
2020-06-29 18:14:47 +02:00
|
|
|
last_event_id: -1,
|
|
|
|
};
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.events.retrieve(eventParams));
|
2020-06-29 18:14:47 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-12-11 05:39:23 +01:00
|
|
|
add_example("get_streams", "/streams:get", 200, async (client, console) => {
|
2020-06-22 00:45:16 +02:00
|
|
|
// {code_example|start}
|
|
|
|
// Get all streams that the user has access to
|
2020-12-11 05:39:23 +01:00
|
|
|
console.log(await client.streams.retrieve());
|
2020-06-22 00:45:16 +02:00
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-05-17 11:37:18 +02:00
|
|
|
main();
|