api: update to 6.3

pull/606/head^2
Nikita 1 year ago committed by Demian
parent 2f38e4952f
commit 5d4079a489

@ -27,6 +27,7 @@ type Rights struct {
CanAddPreviews bool `json:"can_add_web_page_previews"` CanAddPreviews bool `json:"can_add_web_page_previews"`
CanManageVideoChats bool `json:"can_manage_video_chats"` CanManageVideoChats bool `json:"can_manage_video_chats"`
CanManageChat bool `json:"can_manage_chat"` CanManageChat bool `json:"can_manage_chat"`
CanManageTopics bool `json:"can_manage_topics"`
} }
// NoRights is the default Rights{}. // NoRights is the default Rights{}.
@ -35,9 +36,8 @@ func NoRights() Rights { return Rights{} }
// NoRestrictions should be used when un-restricting or // NoRestrictions should be used when un-restricting or
// un-promoting user. // un-promoting user.
// //
// member.Rights = tele.NoRestrictions() // member.Rights = tele.NoRestrictions()
// b.Restrict(chat, member) // b.Restrict(chat, member)
//
func NoRestrictions() Rights { func NoRestrictions() Rights {
return Rights{ return Rights{
CanBeEdited: true, CanBeEdited: true,
@ -56,6 +56,7 @@ func NoRestrictions() Rights {
CanAddPreviews: true, CanAddPreviews: true,
CanManageVideoChats: false, CanManageVideoChats: false,
CanManageChat: false, CanManageChat: false,
CanManageTopics: false,
} }
} }
@ -78,6 +79,7 @@ func AdminRights() Rights {
CanAddPreviews: true, CanAddPreviews: true,
CanManageVideoChats: true, CanManageVideoChats: true,
CanManageChat: true, CanManageChat: true,
CanManageTopics: true,
} }
} }
@ -120,11 +122,10 @@ func (b *Bot) Unban(chat *Chat, user *User, forBanned ...bool) error {
// Restrict lets you restrict a subset of member's rights until // Restrict lets you restrict a subset of member's rights until
// member.RestrictedUntil, such as: // member.RestrictedUntil, such as:
// //
// * can send messages // - can send messages
// * can send media // - can send media
// * can send other // - can send other
// * can add web page previews // - can add web page previews
//
func (b *Bot) Restrict(chat *Chat, member *ChatMember) error { func (b *Bot) Restrict(chat *Chat, member *ChatMember) error {
prv, until := member.Rights, member.RestrictedUntil prv, until := member.Rights, member.RestrictedUntil
@ -141,15 +142,14 @@ func (b *Bot) Restrict(chat *Chat, member *ChatMember) error {
// Promote lets you update member's admin rights, such as: // Promote lets you update member's admin rights, such as:
// //
// * can change info // - can change info
// * can post messages // - can post messages
// * can edit messages // - can edit messages
// * can delete messages // - can delete messages
// * can invite users // - can invite users
// * can restrict members // - can restrict members
// * can pin messages // - can pin messages
// * can promote members // - can promote members
//
func (b *Bot) Promote(chat *Chat, member *ChatMember) error { func (b *Bot) Promote(chat *Chat, member *ChatMember) error {
prv := member.Rights prv := member.Rights
@ -171,7 +171,6 @@ func (b *Bot) Promote(chat *Chat, member *ChatMember) error {
// //
// If the chat is a group or a supergroup and // If the chat is a group or a supergroup and
// no administrators were appointed, only the creator will be returned. // no administrators were appointed, only the creator will be returned.
//
func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) { func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) {
params := map[string]string{ params := map[string]string{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),

@ -10,13 +10,16 @@ import (
type User struct { type User struct {
ID int64 `json:"id"` ID int64 `json:"id"`
FirstName string `json:"first_name"` FirstName string `json:"first_name"`
LastName string `json:"last_name"` LastName string `json:"last_name"`
Username string `json:"username"` IsForum bool `json:"is_forum"`
LanguageCode string `json:"language_code"` Username string `json:"username"`
IsBot bool `json:"is_bot"` LanguageCode string `json:"language_code"`
IsPremium bool `json:"is_premium"` IsBot bool `json:"is_bot"`
AddedToMenu bool `json:"added_to_attachment_menu"` IsPremium bool `json:"is_premium"`
AddedToMenu bool `json:"added_to_attachment_menu"`
Usernames []string `json:"active_usernames"`
EmojiStatusCustomEmojiID string `json:"emoji_status_custom_emoji_id"`
// Returns only in getMe // Returns only in getMe
CanJoinGroups bool `json:"can_join_groups"` CanJoinGroups bool `json:"can_join_groups"`
@ -162,14 +165,13 @@ func (c *ChatMemberUpdate) Time() time.Time {
// //
// Example: // Example:
// //
// group := tele.ChatID(-100756389456) // group := tele.ChatID(-100756389456)
// b.Send(group, "Hello!") // b.Send(group, "Hello!")
//
// type Config struct {
// AdminGroup tele.ChatID `json:"admin_group"`
// }
// b.Send(conf.AdminGroup, "Hello!")
// //
// type Config struct {
// AdminGroup tele.ChatID `json:"admin_group"`
// }
// b.Send(conf.AdminGroup, "Hello!")
type ChatID int64 type ChatID int64
// Recipient returns chat ID (see Recipient interface). // Recipient returns chat ID (see Recipient interface).

@ -10,6 +10,9 @@ import (
type Message struct { type Message struct {
ID int `json:"message_id"` ID int `json:"message_id"`
// (Optional) Unique identifier of a message thread to which the message belongs; for supergroups only
ThreadID int `json:"message_thread_id"`
// For message sent to channels, Sender will be nil // For message sent to channels, Sender will be nil
Sender *User `json:"from"` Sender *User `json:"from"`
@ -59,6 +62,9 @@ type Message struct {
// (Optional) Time of last edit in Unix. // (Optional) Time of last edit in Unix.
LastEdit int64 `json:"edit_date"` LastEdit int64 `json:"edit_date"`
// (Optional True, if the message is sent to a forum topic
TopicMessage bool `json:"is_topic_message"`
// (Optional) Message can't be forwarded. // (Optional) Message can't be forwarded.
Protected bool `json:"has_protected_content,omitempty"` Protected bool `json:"has_protected_content,omitempty"`
@ -250,6 +256,15 @@ type Message struct {
// Inline keyboard attached to the message. // Inline keyboard attached to the message.
ReplyMarkup *ReplyMarkup `json:"reply_markup,omitempty"` ReplyMarkup *ReplyMarkup `json:"reply_markup,omitempty"`
// Service message: forum topic created
TopicCreated *TopicCreated `json:"forum_topic_created,omitempty"`
// Service message: forum topic closed
TopicClosed *TopicClosed `json:"forum_topic_closed,omitempty"`
// Service message: forum topic reopened
TopicReopened *TopicReopened `json:"forum_topic_reopened,omitempty"`
} }
// MessageEntity object represents "special" parts of text messages, // MessageEntity object represents "special" parts of text messages,
@ -365,7 +380,6 @@ func (m *Message) FromChannel() bool {
// Service messages are automatically sent messages, which // Service messages are automatically sent messages, which
// typically occur on some global action. For instance, when // typically occur on some global action. For instance, when
// anyone leaves the chat or chat title changes. // anyone leaves the chat or chat title changes.
//
func (m *Message) IsService() bool { func (m *Message) IsService() bool {
fact := false fact := false
@ -386,7 +400,6 @@ func (m *Message) IsService() bool {
// //
// It's safer than manually slicing Text because Telegram uses // It's safer than manually slicing Text because Telegram uses
// UTF-16 indices whereas Go string are []byte. // UTF-16 indices whereas Go string are []byte.
//
func (m *Message) EntityText(e MessageEntity) string { func (m *Message) EntityText(e MessageEntity) string {
text := m.Text text := m.Text
if text == "" { if text == "" {

@ -11,7 +11,6 @@ import (
// flags instead. // flags instead.
// //
// Supported options are defined as iota-constants. // Supported options are defined as iota-constants.
//
type Option int type Option int
const ( const (
@ -54,7 +53,6 @@ func Placeholder(text string) *SendOptions {
// Despite its power, SendOptions is rather inconvenient to use all // Despite its power, SendOptions is rather inconvenient to use all
// the way through bot logic, so you might want to consider storing // the way through bot logic, so you might want to consider storing
// and re-using it somewhere or be using Option flags instead. // and re-using it somewhere or be using Option flags instead.
//
type SendOptions struct { type SendOptions struct {
// If the message is a reply, original message. // If the message is a reply, original message.
ReplyTo *Message ReplyTo *Message
@ -79,6 +77,9 @@ type SendOptions struct {
// Protected protects the contents of the sent message from forwarding and saving // Protected protects the contents of the sent message from forwarding and saving
Protected bool Protected bool
// MessageThreadID supports sending messages to a thread.
MessageThreadID int
} }
func (og *SendOptions) copy() *SendOptions { func (og *SendOptions) copy() *SendOptions {
@ -189,6 +190,10 @@ func (b *Bot) embedSendOptions(params map[string]string, opt *SendOptions) {
if opt.Protected { if opt.Protected {
params["protect_content"] = "true" params["protect_content"] = "true"
} }
if opt.MessageThreadID != 0 {
params["message_thread_id"] = strconv.Itoa(opt.MessageThreadID)
}
} }
func processButtons(keys [][]InlineButton) { func processButtons(keys [][]InlineButton) {

@ -18,7 +18,6 @@ type Recipient interface {
// This is pretty cool, since it lets bots implement // This is pretty cool, since it lets bots implement
// custom Sendables for complex kind of media or // custom Sendables for complex kind of media or
// chat objects spanning across multiple messages. // chat objects spanning across multiple messages.
//
type Sendable interface { type Sendable interface {
Send(*Bot, Recipient, *SendOptions) (*Message, error) Send(*Bot, Recipient, *SendOptions) (*Message, error)
} }

@ -2,29 +2,28 @@
// //
// Example: // Example:
// //
// package main // package main
// //
// import ( // import (
// "time" // "time"
// tele "gopkg.in/telebot.v3" // tele "gopkg.in/telebot.v3"
// ) // )
// //
// func main() { // func main() {
// b, err := tele.NewBot(tele.Settings{ // b, err := tele.NewBot(tele.Settings{
// Token: "...", // Token: "...",
// Poller: &tele.LongPoller{Timeout: 10 * time.Second}, // Poller: &tele.LongPoller{Timeout: 10 * time.Second},
// }) // })
// if err != nil { // if err != nil {
// return // return
// }
//
// b.Handle("/start", func(c tele.Context) error {
// return c.Send("Hello world!")
// })
//
// b.Start()
// } // }
// //
// b.Handle("/start", func(c tele.Context) error {
// return c.Send("Hello world!")
// })
//
// b.Start()
// }
package telebot package telebot
import "errors" import "errors"
@ -43,7 +42,6 @@ const DefaultApiURL = "https://api.telegram.org"
// //
// For convenience, all Telebot-provided endpoints start with // For convenience, all Telebot-provided endpoints start with
// an "alert" character \a. // an "alert" character \a.
//
const ( const (
// Basic message handlers. // Basic message handlers.
OnText = "\atext" OnText = "\atext"
@ -68,6 +66,9 @@ const (
OnPinned = "\apinned" OnPinned = "\apinned"
OnChannelPost = "\achannel_post" OnChannelPost = "\achannel_post"
OnEditedChannelPost = "\aedited_channel_post" OnEditedChannelPost = "\aedited_channel_post"
OnTopicCreated = "\atopic_created"
OnTopicReopened = "\atopic_reopened"
OnTopicClosed = "\atopic_closed"
OnAddedToGroup = "\aadded_to_group" OnAddedToGroup = "\aadded_to_group"
OnUserJoined = "\auser_joined" OnUserJoined = "\auser_joined"

@ -0,0 +1,126 @@
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"`
MessageThreadID int `json:"message_thread_id"`
}
type TopicCreated struct {
Topic
}
type TopicClosed struct{}
type TopicDeleted struct {
Name string `json:"name"`
IconCustomEmojiID string `json:"icon_custom_emoji_id"`
}
type TopicReopened struct {
Topic
}
// CreateTopic creates a topic in a forum supergroup chat.
func (b *Bot) CreateTopic(chat *Chat, forum *Topic) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"name": forum.Name,
}
if forum.IconColor != 0 {
params["icon_color"] = strconv.Itoa(forum.IconColor)
}
if forum.IconCustomEmojiID != "" {
params["icon_custom_emoji_id"] = forum.IconCustomEmojiID
}
_, err := b.Raw("createForumTopic", params)
return err
}
// EditTopic edits name and icon of a topic in a forum supergroup chat.
func (b *Bot) EditTopic(chat *Chat, forum *Topic) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": forum.MessageThreadID,
}
if forum.Name != "" {
params["name"] = forum.Name
}
if forum.IconCustomEmojiID != "" {
params["icon_custom_emoji_id"] = forum.IconCustomEmojiID
}
_, err := b.Raw("editForumTopic", params)
return err
}
// CloseTopic closes an open topic in a forum supergroup chat.
func (b *Bot) CloseTopic(chat *Chat, forum *Topic) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": forum.MessageThreadID,
}
_, err := b.Raw("closeForumTopic", params)
return err
}
// ReopenTopic reopens a closed topic in a forum supergroup chat.
func (b *Bot) ReopenTopic(chat *Chat, forum *Topic) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": forum.MessageThreadID,
}
_, 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, forum *Topic) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": forum.MessageThreadID,
}
_, 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, forum *Topic) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"message_thread_id": forum.MessageThreadID,
}
_, err := b.Raw("unpinAllForumTopicMessages", params)
return err
}
// TopicIconStickers gets custom emoji stickers, which can be used as a forum topic icon by any user
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
}

@ -98,6 +98,18 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnPayment, c) b.handle(OnPayment, c)
return return
} }
if m.TopicClosed != nil {
b.handle(OnTopicCreated, c)
return
}
if m.TopicReopened != nil {
b.handle(OnTopicReopened, c)
return
}
if m.TopicClosed != nil {
b.handle(OnTopicClosed, c)
return
}
wasAdded := (m.UserJoined != nil && m.UserJoined.ID == b.Me.ID) || wasAdded := (m.UserJoined != nil && m.UserJoined.ID == b.Me.ID) ||
(m.UsersJoined != nil && isUserInList(b.Me, m.UsersJoined)) (m.UsersJoined != nil && isUserInList(b.Me, m.UsersJoined))

Loading…
Cancel
Save