Topics

Forum topics separate conversations in a supergroup. The supergroup must have topics enabled.

Enabling TopicsUSER-ONLY

User clients can enable topics with enableTopics. The second argument controls whether topics are shown as tabs.

await client.enableTopics(chatId, false);

Creating a Topic

const topic = await client.createTopic(chatId, "Announcements");

Sending Messages to a Topic

Set messageThreadId to the topic identifier.

await client.sendMessage(chatId, "Welcome.", {
  messageThreadId: topic.id,
});

The same option is available on other message-sending methods.

Handling Topic Messages

Messages sent in a topic have isTopicMessage set to true. Their threadId is the topic identifier.

client.on("message", (ctx) => {
  if (ctx.msg.isTopicMessage) {
    console.log(ctx.msg.threadId);
  }
});

Listing TopicsUSER-ONLY

User clients can list a forum’s topics with getTopics.

const { items } = await client.getTopics(chatId);

for (const { topic } of items) {
  if (topic.type === "active") {
    console.log(topic.id, topic.name);
  }
}

Managing a Topic

await client.editTopic(chatId, topic.id, "News");
await client.closeTopic(chatId, topic.id);
await client.reopenTopic(chatId, topic.id);
await client.deleteTopic(chatId, topic.id);