telebot/topic.go

173 lines
4.5 KiB
Go
Raw Normal View History

2023-06-06 18:50:25 +00:00
package telebot
import (
"encoding/json"
"strconv"
)
type Topic struct {
Name string `json:"name"`
IconColor int `json:"icon_color"`
IconCustomEmojiID string `json:"icon_custom_emoji_id"`
2023-06-06 23:02:04 +00:00
ThreadID int `json:"message_thread_id"`
2023-06-06 18:50:25 +00:00
}
// CreateTopic creates a topic in a forum supergroup chat.
func (b *Bot) CreateTopic(chat *Chat, topic *Topic) (*Topic, error) {
2023-06-06 18:50:25 +00:00
params := map[string]string{
"chat_id": chat.Recipient(),
"name": topic.Name,
2023-06-06 18:50:25 +00:00
}
if topic.IconColor != 0 {
params["icon_color"] = strconv.Itoa(topic.IconColor)
2023-06-06 18:50:25 +00:00
}
if topic.IconCustomEmojiID != "" {
params["icon_custom_emoji_id"] = topic.IconCustomEmojiID
2023-06-06 18:50:25 +00:00
}
data, err := b.Raw("createForumTopic", params)
if err != nil {
return nil, err
}
var resp struct {
Result *Topic
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, err
2023-06-06 18:50:25 +00:00
}
// EditTopic edits name and icon of a topic in a forum supergroup chat.
func (b *Bot) EditTopic(chat *Chat, topic *Topic) error {
2023-06-06 18:50:25 +00:00
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": topic.ThreadID,
2023-06-06 18:50:25 +00:00
}
if topic.Name != "" {
params["name"] = topic.Name
2023-06-06 18:50:25 +00:00
}
if topic.IconCustomEmojiID != "" {
params["icon_custom_emoji_id"] = topic.IconCustomEmojiID
2023-06-06 18:50:25 +00:00
}
_, err := b.Raw("editForumTopic", params)
return err
}
// CloseTopic closes an open topic in a forum supergroup chat.
func (b *Bot) CloseTopic(chat *Chat, topic *Topic) error {
2023-06-06 18:50:25 +00:00
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": topic.ThreadID,
2023-06-06 18:50:25 +00:00
}
_, err := b.Raw("closeForumTopic", params)
return err
}
// ReopenTopic reopens a closed topic in a forum supergroup chat.
func (b *Bot) ReopenTopic(chat *Chat, topic *Topic) error {
2023-06-06 18:50:25 +00:00
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": topic.ThreadID,
2023-06-06 18:50:25 +00:00
}
_, err := b.Raw("reopenForumTopic", params)
return err
}
// DeleteTopic deletes a forum topic along with all its messages in a forum supergroup chat.
func (b *Bot) DeleteTopic(chat *Chat, topic *Topic) error {
2023-06-06 18:50:25 +00:00
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": topic.ThreadID,
2023-06-06 18:50:25 +00:00
}
_, err := b.Raw("deleteForumTopic", params)
return err
}
// UnpinAllTopicMessages clears the list of pinned messages in a forum topic. The bot must be an administrator in the chat for this to work and must have the can_pin_messages administrator right in the supergroup.
func (b *Bot) UnpinAllTopicMessages(chat *Chat, topic *Topic) error {
2023-06-06 18:50:25 +00:00
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": topic.ThreadID,
2023-06-06 18:50:25 +00:00
}
_, err := b.Raw("unpinAllForumTopicMessages", params)
return err
}
// TopicIconStickers gets custom emoji stickers, which can be used as a forum topic icon by any user.
2023-06-06 18:50:25 +00:00
func (b *Bot) TopicIconStickers() ([]Sticker, error) {
params := map[string]string{}
data, err := b.Raw("getForumTopicIconStickers", params)
if err != nil {
return nil, err
}
var resp struct {
Result []Sticker
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// EditGeneralTopic edits name of the 'General' topic in a forum supergroup chat.
func (b *Bot) EditGeneralTopic(chat *Chat, topic *Topic) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"name": topic.Name,
}
_, err := b.Raw("editGeneralForumTopic", params)
return err
}
// CloseGeneralTopic closes an open 'General' topic in a forum supergroup chat.
func (b *Bot) CloseGeneralTopic(chat *Chat) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
}
_, err := b.Raw("closeGeneralForumTopic", params)
return err
}
// ReopenGeneralTopic reopens a closed 'General' topic in a forum supergroup chat.
func (b *Bot) ReopenGeneralTopic(chat *Chat) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
}
_, err := b.Raw("reopenGeneralForumTopic", params)
return err
}
// HideGeneralTopic hides the 'General' topic in a forum supergroup chat.
func (b *Bot) HideGeneralTopic(chat *Chat) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
}
_, err := b.Raw("hideGeneralForumTopic", params)
return err
}
// UnhideGeneralTopic unhides the 'General' topic in a forum supergroup chat.
func (b *Bot) UnhideGeneralTopic(chat *Chat) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
}
_, err := b.Raw("unhideGeneralForumTopic", params)
return err
}