Skip to content

Commit

Permalink
Add method restrict chat member
Browse files Browse the repository at this point in the history
  • Loading branch information
dynastymasra committed Jan 17, 2018
1 parent d3cd4f2 commit fec4e72
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
62 changes: 62 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ func (client *Client) KickChatMember(chatId interface{}, userId int64) *VoidResp
}
}

/*
RestrictChatMember Use this method to restrict a user in a supergroup.
The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
Pass True for all boolean parameters to lift restrictions from a user. Returns True on success.
*/
func (client *Client) RestrictChatMember(chatId interface{}, userId int64) *VoidResponse {
body := JSON{
"chat_id": chatId,
"user_id": userId,
}

url := client.baseURL + fmt.Sprintf(EndpointRestrictChatMember, client.accessToken)
request := gorequest.New().Type(gorequest.TypeJSON).Post(url).Set(UserAgentHeader, UserAgent+"/"+Version).
Send(body)

return &VoidResponse{
Client: client,
Request: request,
}
}

// SetUntilDate Date when the user will be unbanned, unix time.
// If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever
func (void *VoidResponse) SetUntilDate(date int64) *VoidResponse {
Expand All @@ -230,6 +251,47 @@ func (void *VoidResponse) SetUntilDate(date int64) *VoidResponse {
return void
}

// SetCanSendMessages Pass True, if the user can send text messages, contacts, locations and venues
func (void *VoidResponse) SetCanSendMessage(can bool) *VoidResponse {
body := JSON{
"can_send_messages": can,
}
void.Request = void.Request.Send(body)

return void
}

// SetCanSendMediaMessages Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
// implies can_send_messages
func (void *VoidResponse) SetCanSendMediaMessage(can bool) *VoidResponse {
body := JSON{
"can_send_media_messages": can,
}
void.Request = void.Request.Send(body)

return void
}

// SetCanOtherMessages Pass True, if the user can send animations, games, stickers and use inline bots, implies can_send_media_messages
func (void *VoidResponse) SetCanSendOtherMessage(can bool) *VoidResponse {
body := JSON{
"can_send_other_messages": can,
}
void.Request = void.Request.Send(body)

return void
}

// SetCanAddWebPagePreview Pass True, if the user may add web page previews to their messages, implies can_send_media_messages
func (void *VoidResponse) SetCanAddWebPagePreview(can bool) *VoidResponse {
body := JSON{
"can_add_web_page_previews": can,
}
void.Request = void.Request.Send(body)

return void
}

/*
GetContent function for download file from telegram server, file path obtained from function GetFile()
Exp https://api.telegram.org/file/bot<token>/<file_path>
Expand Down
46 changes: 46 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,49 @@ func TestUnbanChatMember_Failed(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assert.NotNil(t, err)
}

func TestRestrictChatMember_Success(t *testing.T) {
gock.New(telegraph.BaseURL).Post(fmt.Sprintf(telegraph.EndpointRestrictChatMember, "token")).Reply(http.StatusOK).JSON(`{
"ok": true,
"result": true
}`)
defer gock.Off()

client := telegraph.NewClient("token")
body, res, err := client.RestrictChatMember("32423423", 23423423).SetCanSendMessage(true).
SetCanSendMediaMessage(true).SetCanSendOtherMessage(true).SetCanAddWebPagePreview(true).Commit()

assert.NotNil(t, body)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.NoError(t, err)
}

func TestRestrictChatMember_Error(t *testing.T) {
gock.New(telegraph.BaseURL).Head(fmt.Sprintf(telegraph.EndpointRestrictChatMember, "token")).Reply(http.StatusInternalServerError).JSON("")
defer gock.Off()

client := telegraph.NewClient("token")
body, res, err := client.RestrictChatMember("32423423", 23423423).SetCanSendMessage(true).
SetCanSendMediaMessage(true).SetCanSendOtherMessage(true).SetCanAddWebPagePreview(true).Commit()

assert.Nil(t, body)
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
assert.NotNil(t, err)
}

func TestRestrictChatMember_Failed(t *testing.T) {
gock.New(telegraph.BaseURL).Post(fmt.Sprintf(telegraph.EndpointRestrictChatMember, "token")).Reply(http.StatusBadRequest).JSON(`{
"ok": false,
"error_code": 400,
"description": "Bad Request: invalid file id"
}`)
defer gock.Off()

client := telegraph.NewClient("token")
body, res, err := client.RestrictChatMember("32423423", 23423423).SetCanSendMessage(true).
SetCanSendMediaMessage(true).SetCanSendOtherMessage(true).SetCanAddWebPagePreview(true).Commit()

assert.Nil(t, body)
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assert.NotNil(t, err)
}

0 comments on commit fec4e72

Please sign in to comment.