🌙

Топики

Поддержка топиков доступна в pykodaribot версии 0.4.0 и новее.

Установка

pip install -U pykodaribot

Получение топиков

topics = await bot.get_forum_topics(chat_id)

for topic in topics:
    print(topic.message_thread_id, topic.name, topic.is_closed)

topic = await bot.get_forum_topic(chat_id, message_thread_id=42)

Создание и отправка сообщения

topic = await bot.create_forum_topic(
    chat_id,
    "Поддержка",
    icon_color="#6C8CFF",
    icon_emoji="🛠",
)

await bot.send_message(
    chat_id,
    "Топик создан",
    message_thread_id=topic.message_thread_id,
)

await bot.send_photo(
    chat_id,
    "photo.jpg",
    caption="Вложение",
    message_thread_id=topic.message_thread_id,
)

Управление

await bot.edit_forum_topic(
    chat_id,
    topic.message_thread_id,
    name="Техническая поддержка",
    icon_color="#22C55E",
)

await bot.close_forum_topic(chat_id, topic.message_thread_id)
await bot.reopen_forum_topic(chat_id, topic.message_thread_id)
await bot.delete_forum_topic(chat_id, topic.message_thread_id)

Входящие сообщения

@dp.message()
async def handle_message(msg):
    if msg.is_topic_message:
        print(msg.message_thread_id)
    await msg.answer("Принято")

msg.answer() и msg.reply() автоматически передают message_thread_id входящего сообщения. Ответ остаётся в том же топике.

События топиков

@dp.forum_topic()
async def handle_topic(event):
    print(event.message_thread_id, event.topic.name, event.topic.is_closed)

@dp.forum_status()
async def handle_forum_status(event):
    print(event.chat.id, event.is_forum, event.topics_create)

В Update доступны поля:

  • forum_topic_created
  • forum_topic_edited
  • forum_topic_closed
  • forum_topic_reopened
  • forum_topic_deleted
  • forum_status_changed

Права администратора

from pykodaribot import ChatAdministratorRights

rights = ChatAdministratorRights(
    delete_messages=True,
    manage_topics=True,
)

await bot.promote_chat_member(chat_id, user_id, rights)

Поле Chat.is_forum показывает, включены ли топики. Поле Chat.topics_create содержит режим создания all или admins.

22 просмотра