Skip to content

Commit

Permalink
admin: implement my default administrator rights
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Oct 4, 2022
1 parent 933fcac commit 2a6eec3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
37 changes: 36 additions & 1 deletion admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ func (c *ChatMemberUpdate) Time() time.Time {

// Rights is a list of privileges available to chat members.
type Rights struct {
// Anonymous is true, if the user's presence in the chat is hidden.
Anonymous bool `json:"is_anonymous"`

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"`
CanPinMessages bool `json:"can_pin_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"`
Expand Down Expand Up @@ -310,3 +313,35 @@ func (b *Bot) UnbanSenderChat(chat *Chat, sender Recipient) error {
_, err := b.Raw("unbanChatSenderChat", params)
return err
}

// DefaultRights returns the current default administrator rights of the bot.
func (b *Bot) DefaultRights(forChannels bool) (*Rights, error) {
params := map[string]bool{
"for_channels": forChannels,
}

data, err := b.Raw("getMyDefaultAdministratorRights", params)
if err != nil {
return nil, err
}

var resp struct {
Result *Rights
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}

// SetDefaultRights changes the default administrator rights requested by the bot
// when it's added as an administrator to groups or channels.
func (b *Bot) SetDefaultRights(rights Rights, forChannels bool) error {
params := map[string]interface{}{
"rights": rights,
"for_channels": forChannels,
}

_, err := b.Raw("setMyDefaultAdministratorRights", params)
return err
}
8 changes: 4 additions & 4 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,9 @@ func (b *Bot) Ship(query *ShippingQuery, what ...interface{}) error {
}

if len(what) == 0 {
params["ok"] = "True"
params["ok"] = "true"
} else if s, ok := what[0].(string); ok {
params["ok"] = "False"
params["ok"] = "false"
params["error_message"] = s
} else {
var opts []ShippingOption
Expand All @@ -1045,7 +1045,7 @@ func (b *Bot) Ship(query *ShippingQuery, what ...interface{}) error {
opts = append(opts, opt)
}

params["ok"] = "True"
params["ok"] = "true"
data, _ := json.Marshal(opts)
params["shipping_options"] = string(data)
}
Expand All @@ -1061,7 +1061,7 @@ func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error {
}

if len(errorMessage) == 0 {
params["ok"] = "True"
params["ok"] = "true"
} else {
params["ok"] = "False"
params["error_message"] = errorMessage[0]
Expand Down
1 change: 1 addition & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestEmbedRights(t *testing.T) {
embedRights(params, rights)

expected := map[string]interface{}{
"is_anonymous": false,
"chat_id": "1",
"user_id": "2",
"can_be_edited": true,
Expand Down

0 comments on commit 2a6eec3

Please sign in to comment.