Skip to content

Commit 217764b

Browse files
author
Syfaro
committed
Add new chat methods for Bot API 2.1.
1 parent 3ed6b6a commit 217764b

File tree

5 files changed

+164
-5
lines changed

5 files changed

+164
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func main() {
5151
updates, err := bot.GetUpdatesChan(u)
5252

5353
for update := range updates {
54+
if update.Message == nil {
55+
continue
56+
}
57+
5458
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
5559

5660
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)

bot.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,117 @@ func (bot *BotAPI) KickChatMember(config ChatMemberConfig) (APIResponse, error)
521521
return bot.MakeRequest("kickChatMember", v)
522522
}
523523

524+
// LeaveChat makes the bot leave the chat.
525+
func (bot *BotAPI) LeaveChat(config ChatConfig) (APIResponse, error) {
526+
v := url.Values{}
527+
528+
if config.SuperGroupUsername == "" {
529+
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
530+
} else {
531+
v.Add("chat_id", config.SuperGroupUsername)
532+
}
533+
534+
bot.debugLog("leaveChat", v, nil)
535+
536+
return bot.MakeRequest("leaveChat", v)
537+
}
538+
539+
// GetChat gets information about a chat.
540+
func (bot *BotAPI) GetChat(config ChatConfig) (Chat, error) {
541+
v := url.Values{}
542+
543+
if config.SuperGroupUsername == "" {
544+
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
545+
} else {
546+
v.Add("chat_id", config.SuperGroupUsername)
547+
}
548+
549+
resp, err := bot.MakeRequest("getChat", v)
550+
if err != nil {
551+
return Chat{}, err
552+
}
553+
554+
var chat Chat
555+
err = json.Unmarshal(resp.Result, &chat)
556+
557+
bot.debugLog("getChat", v, chat)
558+
559+
return chat, err
560+
}
561+
562+
// GetChatAdministrators gets a list of administrators in the chat.
563+
//
564+
// If none have been appointed, only the creator will be returned.
565+
// Bots are not shown, even if they are an administrator.
566+
func (bot *BotAPI) GetChatAdministrators(config ChatConfig) ([]ChatMember, error) {
567+
v := url.Values{}
568+
569+
if config.SuperGroupUsername == "" {
570+
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
571+
} else {
572+
v.Add("chat_id", config.SuperGroupUsername)
573+
}
574+
575+
resp, err := bot.MakeRequest("getChatAdministrators", v)
576+
if err != nil {
577+
return []ChatMember{}, err
578+
}
579+
580+
var members []ChatMember
581+
err = json.Unmarshal(resp.Result, &members)
582+
583+
bot.debugLog("getChatAdministrators", v, members)
584+
585+
return members, err
586+
}
587+
588+
// GetChatMembersCount gets the number of users in a chat.
589+
func (bot *BotAPI) GetChatMembersCount(config ChatConfig) (int, error) {
590+
v := url.Values{}
591+
592+
if config.SuperGroupUsername == "" {
593+
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
594+
} else {
595+
v.Add("chat_id", config.SuperGroupUsername)
596+
}
597+
598+
resp, err := bot.MakeRequest("getChatMembersCount", v)
599+
if err != nil {
600+
return -1, err
601+
}
602+
603+
var count int
604+
err = json.Unmarshal(resp.Result, &count)
605+
606+
bot.debugLog("getChatMembersCount", v, count)
607+
608+
return count, err
609+
}
610+
611+
// GetChatMember gets a specific chat member.
612+
func (bot *BotAPI) GetChatMember(config ChatConfigWithUser) (ChatMember, error) {
613+
v := url.Values{}
614+
615+
if config.SuperGroupUsername == "" {
616+
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
617+
} else {
618+
v.Add("chat_id", config.SuperGroupUsername)
619+
}
620+
v.Add("user_id", strconv.Itoa(config.UserID))
621+
622+
resp, err := bot.MakeRequest("getChatMember", v)
623+
if err != nil {
624+
return ChatMember{}, err
625+
}
626+
627+
var member ChatMember
628+
err = json.Unmarshal(resp.Result, &member)
629+
630+
bot.debugLog("getChatMember", v, member)
631+
632+
return member, err
633+
}
634+
524635
// UnbanChatMember unbans a user from a chat. Note that this only will work
525636
// in supergroups, and requires the bot to be an admin.
526637
func (bot *BotAPI) UnbanChatMember(config ChatMemberConfig) (APIResponse, error) {

bot_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ func ExampleNewBotAPI() {
447447
updates, err := bot.GetUpdatesChan(u)
448448

449449
for update := range updates {
450+
if update.Message == nil {
451+
continue
452+
}
453+
450454
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
451455

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

495499
for update := range updates {
496-
if update.InlineQuery.Query == "" { // if no inline query, ignore it
500+
if update.InlineQuery == nil { // if no inline query, ignore it
497501
continue
498502
}
499503

configs.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,17 @@ type ChatMemberConfig struct {
675675
SuperGroupUsername string
676676
UserID int
677677
}
678+
679+
// ChatConfig contains information about getting information on a chat.
680+
type ChatConfig struct {
681+
ChatID int64
682+
SuperGroupUsername string
683+
}
684+
685+
// ChatConfigWithUser contains information about getting information on
686+
// a specific user within a chat.
687+
type ChatConfigWithUser struct {
688+
ChatID int64
689+
SuperGroupUsername string
690+
UserID int
691+
}

types.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,30 @@ type Chat struct {
7070
}
7171

7272
// IsPrivate returns if the Chat is a private conversation.
73-
func (c *Chat) IsPrivate() bool {
73+
func (c Chat) IsPrivate() bool {
7474
return c.Type == "private"
7575
}
7676

7777
// IsGroup returns if the Chat is a group.
78-
func (c *Chat) IsGroup() bool {
78+
func (c Chat) IsGroup() bool {
7979
return c.Type == "group"
8080
}
8181

8282
// IsSuperGroup returns if the Chat is a supergroup.
83-
func (c *Chat) IsSuperGroup() bool {
83+
func (c Chat) IsSuperGroup() bool {
8484
return c.Type == "supergroup"
8585
}
8686

8787
// IsChannel returns if the Chat is a channel.
88-
func (c *Chat) IsChannel() bool {
88+
func (c Chat) IsChannel() bool {
8989
return c.Type == "channel"
9090
}
9191

92+
// ChatConfig returns a ChatConfig struct for chat related methods.
93+
func (c Chat) ChatConfig() ChatConfig {
94+
return ChatConfig{ChatID: c.ID}
95+
}
96+
9297
// Message is returned by almost every request, and contains data about
9398
// almost anything.
9499
type Message struct {
@@ -343,6 +348,27 @@ type ForceReply struct {
343348
Selective bool `json:"selective"` // optional
344349
}
345350

351+
// ChatMember is information about a member in a chat.
352+
type ChatMember struct {
353+
User *User `json:"user"`
354+
Status string `json:"status"`
355+
}
356+
357+
// IsCreator returns if the ChatMember was the creator of the chat.
358+
func (chat ChatMember) IsCreator() bool { return chat.Status == "creator" }
359+
360+
// IsAdministrator returns if the ChatMember is a chat administrator.
361+
func (chat ChatMember) IsAdministrator() bool { return chat.Status == "administrator" }
362+
363+
// IsMember returns if the ChatMember is a current member of the chat.
364+
func (chat ChatMember) IsMember() bool { return chat.Status == "member" }
365+
366+
// HasLeft returns if the ChatMember left the chat.
367+
func (chat ChatMember) HasLeft() bool { return chat.Status == "left" }
368+
369+
// WasKicked returns if the ChatMember was kicked from the chat.
370+
func (chat ChatMember) WasKicked() bool { return chat.Status == "kicked" }
371+
346372
// InlineQuery is a Query from Telegram for an inline request.
347373
type InlineQuery struct {
348374
ID string `json:"id"`

0 commit comments

Comments
 (0)