2017-11-21 15:28:22 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
2017-11-26 00:44:32 +00:00
|
|
|
"encoding/json"
|
2017-11-21 15:28:22 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Rights is a list of privileges available to chat members.
|
|
|
|
type Rights struct {
|
2020-09-01 11:13:54 +00:00
|
|
|
CanBeEdited bool `json:"can_be_edited"`
|
|
|
|
CanChangeInfo bool `json:"can_change_info"`
|
|
|
|
CanPostMessages bool `json:"can_post_messages"`
|
|
|
|
CanEditMessages bool `json:"can_edit_messages"`
|
|
|
|
CanDeleteMessages bool `json:"can_delete_messages"`
|
|
|
|
CanInviteUsers bool `json:"can_invite_users"`
|
|
|
|
CanRestrictMembers bool `json:"can_restrict_members"`
|
|
|
|
CanPinMessages bool `json:"can_pin_messages"`
|
|
|
|
CanPromoteMembers bool `json:"can_promote_members"`
|
|
|
|
CanSendMessages bool `json:"can_send_messages"`
|
|
|
|
CanSendMedia bool `json:"can_send_media_messages"`
|
|
|
|
CanSendPolls bool `json:"can_send_polls"`
|
|
|
|
CanSendOther bool `json:"can_send_other_messages"`
|
|
|
|
CanAddPreviews bool `json:"can_add_web_page_previews"`
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 11:46:48 +00:00
|
|
|
// NoRights is the default Rights{}.
|
2017-11-21 15:28:22 +00:00
|
|
|
func NoRights() Rights { return Rights{} }
|
|
|
|
|
|
|
|
// NoRestrictions should be used when un-restricting or
|
|
|
|
// un-promoting user.
|
|
|
|
//
|
2020-04-26 11:46:48 +00:00
|
|
|
// member.Rights = tb.NoRestrictions()
|
2017-11-21 15:28:22 +00:00
|
|
|
// bot.Restrict(chat, member)
|
|
|
|
//
|
|
|
|
func NoRestrictions() Rights {
|
|
|
|
return Rights{
|
2020-04-26 11:46:48 +00:00
|
|
|
CanBeEdited: true,
|
|
|
|
CanChangeInfo: false,
|
|
|
|
CanPostMessages: false,
|
|
|
|
CanEditMessages: false,
|
|
|
|
CanDeleteMessages: false,
|
|
|
|
CanInviteUsers: false,
|
|
|
|
CanRestrictMembers: false,
|
|
|
|
CanPinMessages: false,
|
|
|
|
CanPromoteMembers: false,
|
|
|
|
CanSendMessages: true,
|
|
|
|
CanSendMedia: true,
|
|
|
|
CanSendPolls: true,
|
|
|
|
CanSendOther: true,
|
|
|
|
CanAddPreviews: true,
|
|
|
|
}
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AdminRights could be used to promote user to admin.
|
|
|
|
func AdminRights() Rights {
|
|
|
|
return Rights{
|
2020-04-26 11:46:48 +00:00
|
|
|
CanBeEdited: true,
|
|
|
|
CanChangeInfo: true,
|
|
|
|
CanPostMessages: true,
|
|
|
|
CanEditMessages: true,
|
|
|
|
CanDeleteMessages: true,
|
|
|
|
CanInviteUsers: true,
|
|
|
|
CanRestrictMembers: true,
|
|
|
|
CanPinMessages: true,
|
|
|
|
CanPromoteMembers: true,
|
|
|
|
CanSendMessages: true,
|
|
|
|
CanSendMedia: true,
|
|
|
|
CanSendPolls: true,
|
|
|
|
CanSendOther: true,
|
|
|
|
CanAddPreviews: true,
|
|
|
|
}
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Forever is a Unixtime of "forever" banning.
|
|
|
|
func Forever() int64 {
|
|
|
|
return time.Now().Add(367 * 24 * time.Hour).Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ban will ban user from chat until `member.RestrictedUntil`.
|
|
|
|
func (b *Bot) Ban(chat *Chat, member *ChatMember) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"user_id": member.User.Recipient(),
|
|
|
|
"until_date": strconv.FormatInt(member.RestrictedUntil, 10),
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("kickChatMember", params)
|
|
|
|
return err
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unban will unban user from chat, who would have thought eh?
|
|
|
|
func (b *Bot) Unban(chat *Chat, user *User) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"user_id": user.Recipient(),
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("unbanChatMember", params)
|
|
|
|
return err
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 11:46:48 +00:00
|
|
|
// Restrict lets you restrict a subset of member's rights until
|
2017-11-21 15:28:22 +00:00
|
|
|
// member.RestrictedUntil, such as:
|
|
|
|
//
|
|
|
|
// * can send messages
|
|
|
|
// * can send media
|
|
|
|
// * can send other
|
|
|
|
// * can add web page previews
|
|
|
|
//
|
|
|
|
func (b *Bot) Restrict(chat *Chat, member *ChatMember) error {
|
|
|
|
prv, until := member.Rights, member.RestrictedUntil
|
|
|
|
|
2020-06-24 01:07:53 +00:00
|
|
|
params := map[string]interface{}{
|
2017-11-21 15:28:22 +00:00
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"user_id": member.User.Recipient(),
|
|
|
|
"until_date": strconv.FormatInt(until, 10),
|
|
|
|
}
|
|
|
|
embedRights(params, prv)
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("restrictChatMember", params)
|
|
|
|
return err
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Promote lets you update member's admin rights, such as:
|
|
|
|
//
|
|
|
|
// * can change info
|
|
|
|
// * can post messages
|
|
|
|
// * can edit messages
|
|
|
|
// * can delete messages
|
|
|
|
// * can invite users
|
|
|
|
// * can restrict members
|
|
|
|
// * can pin messages
|
|
|
|
// * can promote members
|
|
|
|
//
|
|
|
|
func (b *Bot) Promote(chat *Chat, member *ChatMember) error {
|
|
|
|
prv := member.Rights
|
|
|
|
|
2020-06-24 01:07:53 +00:00
|
|
|
params := map[string]interface{}{
|
2017-11-21 15:28:22 +00:00
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"user_id": member.User.Recipient(),
|
|
|
|
}
|
|
|
|
embedRights(params, prv)
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("promoteChatMember", params)
|
|
|
|
return err
|
2017-11-21 15:28:22 +00:00
|
|
|
}
|
2017-11-26 00:44:32 +00:00
|
|
|
|
2020-04-26 11:46:48 +00:00
|
|
|
// AdminsOf returns a member list of chat admins.
|
2017-11-26 00:44:32 +00:00
|
|
|
//
|
|
|
|
// On success, returns an Array of ChatMember objects that
|
|
|
|
// contains information about all chat administrators except other bots.
|
|
|
|
// If the chat is a group or a supergroup and
|
|
|
|
// no administrators were appointed, only the creator will be returned.
|
|
|
|
func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getChatAdministrators", params)
|
2017-11-26 00:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
2020-04-26 15:19:49 +00:00
|
|
|
Result []ChatMember
|
2017-11-26 00:44:32 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
2017-11-26 00:44:32 +00:00
|
|
|
}
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:46:48 +00:00
|
|
|
// Len returns the number of members in a chat.
|
2017-11-26 00:44:32 +00:00
|
|
|
func (b *Bot) Len(chat *Chat) (int, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getChatMembersCount", params)
|
2017-11-26 00:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
2020-04-26 15:19:49 +00:00
|
|
|
Result int
|
2017-11-26 00:44:32 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return 0, wrapError(err)
|
2017-11-26 00:44:32 +00:00
|
|
|
}
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
2020-04-26 11:46:48 +00:00
|
|
|
|
|
|
|
// SetAdminTitle sets a custom title for an administrator.
|
|
|
|
// A title should be 0-16 characters length, emoji are not allowed.
|
|
|
|
func (b *Bot) SetAdminTitle(chat *Chat, user *User, title string) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"user_id": user.Recipient(),
|
|
|
|
"custom_title": title,
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("setChatAdministratorCustomTitle", params)
|
|
|
|
return err
|
2020-04-26 11:46:48 +00:00
|
|
|
}
|