Skip to content

Commit

Permalink
Add new chat methods for Bot API 2.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syfaro committed May 22, 2016
1 parent 3ed6b6a commit 217764b
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func main() {
updates, err := bot.GetUpdatesChan(u)

for update := range updates {
if update.Message == nil {
continue
}

log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
Expand Down
111 changes: 111 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,117 @@ func (bot *BotAPI) KickChatMember(config ChatMemberConfig) (APIResponse, error)
return bot.MakeRequest("kickChatMember", v)
}

// LeaveChat makes the bot leave the chat.
func (bot *BotAPI) LeaveChat(config ChatConfig) (APIResponse, error) {
v := url.Values{}

if config.SuperGroupUsername == "" {
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
} else {
v.Add("chat_id", config.SuperGroupUsername)
}

bot.debugLog("leaveChat", v, nil)

return bot.MakeRequest("leaveChat", v)
}

// GetChat gets information about a chat.
func (bot *BotAPI) GetChat(config ChatConfig) (Chat, error) {
v := url.Values{}

if config.SuperGroupUsername == "" {
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
} else {
v.Add("chat_id", config.SuperGroupUsername)
}

resp, err := bot.MakeRequest("getChat", v)
if err != nil {
return Chat{}, err
}

var chat Chat
err = json.Unmarshal(resp.Result, &chat)

bot.debugLog("getChat", v, chat)

return chat, err
}

// GetChatAdministrators gets a list of administrators in the chat.
//
// If none have been appointed, only the creator will be returned.
// Bots are not shown, even if they are an administrator.
func (bot *BotAPI) GetChatAdministrators(config ChatConfig) ([]ChatMember, error) {
v := url.Values{}

if config.SuperGroupUsername == "" {
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
} else {
v.Add("chat_id", config.SuperGroupUsername)
}

resp, err := bot.MakeRequest("getChatAdministrators", v)
if err != nil {
return []ChatMember{}, err
}

var members []ChatMember
err = json.Unmarshal(resp.Result, &members)

bot.debugLog("getChatAdministrators", v, members)

return members, err
}

// GetChatMembersCount gets the number of users in a chat.
func (bot *BotAPI) GetChatMembersCount(config ChatConfig) (int, error) {
v := url.Values{}

if config.SuperGroupUsername == "" {
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
} else {
v.Add("chat_id", config.SuperGroupUsername)
}

resp, err := bot.MakeRequest("getChatMembersCount", v)
if err != nil {
return -1, err
}

var count int
err = json.Unmarshal(resp.Result, &count)

bot.debugLog("getChatMembersCount", v, count)

return count, err
}

// GetChatMember gets a specific chat member.
func (bot *BotAPI) GetChatMember(config ChatConfigWithUser) (ChatMember, error) {
v := url.Values{}

if config.SuperGroupUsername == "" {
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
} else {
v.Add("chat_id", config.SuperGroupUsername)
}
v.Add("user_id", strconv.Itoa(config.UserID))

resp, err := bot.MakeRequest("getChatMember", v)
if err != nil {
return ChatMember{}, err
}

var member ChatMember
err = json.Unmarshal(resp.Result, &member)

bot.debugLog("getChatMember", v, member)

return member, err
}

// UnbanChatMember unbans a user from a chat. Note that this only will work
// in supergroups, and requires the bot to be an admin.
func (bot *BotAPI) UnbanChatMember(config ChatMemberConfig) (APIResponse, error) {
Expand Down
6 changes: 5 additions & 1 deletion bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ func ExampleNewBotAPI() {
updates, err := bot.GetUpdatesChan(u)

for update := range updates {
if update.Message == nil {
continue
}

log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
Expand Down Expand Up @@ -493,7 +497,7 @@ func ExampleAnswerInlineQuery() {
updates, err := bot.GetUpdatesChan(u)

for update := range updates {
if update.InlineQuery.Query == "" { // if no inline query, ignore it
if update.InlineQuery == nil { // if no inline query, ignore it
continue
}

Expand Down
14 changes: 14 additions & 0 deletions configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,17 @@ type ChatMemberConfig struct {
SuperGroupUsername string
UserID int
}

// ChatConfig contains information about getting information on a chat.
type ChatConfig struct {
ChatID int64
SuperGroupUsername string
}

// ChatConfigWithUser contains information about getting information on
// a specific user within a chat.
type ChatConfigWithUser struct {
ChatID int64
SuperGroupUsername string
UserID int
}
34 changes: 30 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,30 @@ type Chat struct {
}

// IsPrivate returns if the Chat is a private conversation.
func (c *Chat) IsPrivate() bool {
func (c Chat) IsPrivate() bool {
return c.Type == "private"
}

// IsGroup returns if the Chat is a group.
func (c *Chat) IsGroup() bool {
func (c Chat) IsGroup() bool {
return c.Type == "group"
}

// IsSuperGroup returns if the Chat is a supergroup.
func (c *Chat) IsSuperGroup() bool {
func (c Chat) IsSuperGroup() bool {
return c.Type == "supergroup"
}

// IsChannel returns if the Chat is a channel.
func (c *Chat) IsChannel() bool {
func (c Chat) IsChannel() bool {
return c.Type == "channel"
}

// ChatConfig returns a ChatConfig struct for chat related methods.
func (c Chat) ChatConfig() ChatConfig {
return ChatConfig{ChatID: c.ID}
}

// Message is returned by almost every request, and contains data about
// almost anything.
type Message struct {
Expand Down Expand Up @@ -343,6 +348,27 @@ type ForceReply struct {
Selective bool `json:"selective"` // optional
}

// ChatMember is information about a member in a chat.
type ChatMember struct {
User *User `json:"user"`
Status string `json:"status"`
}

// IsCreator returns if the ChatMember was the creator of the chat.
func (chat ChatMember) IsCreator() bool { return chat.Status == "creator" }

// IsAdministrator returns if the ChatMember is a chat administrator.
func (chat ChatMember) IsAdministrator() bool { return chat.Status == "administrator" }

// IsMember returns if the ChatMember is a current member of the chat.
func (chat ChatMember) IsMember() bool { return chat.Status == "member" }

// HasLeft returns if the ChatMember left the chat.
func (chat ChatMember) HasLeft() bool { return chat.Status == "left" }

// WasKicked returns if the ChatMember was kicked from the chat.
func (chat ChatMember) WasKicked() bool { return chat.Status == "kicked" }

// InlineQuery is a Query from Telegram for an inline request.
type InlineQuery struct {
ID string `json:"id"`
Expand Down

0 comments on commit 217764b

Please sign in to comment.