Skip to content

Commit db88019

Browse files
committed
Add support for Polls and other API 4.2 updates.
1 parent fa40708 commit db88019

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

bot.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,29 @@ func (bot *BotAPI) GetStickerSet(config GetStickerSetConfig) (StickerSet, error)
548548

549549
resp, err := bot.MakeRequest(config.method(), params)
550550
if err != nil {
551-
return StickerSet{}, nil
551+
return StickerSet{}, err
552552
}
553553

554554
var stickers StickerSet
555555
err = json.Unmarshal(resp.Result, &stickers)
556556

557557
return stickers, err
558558
}
559+
560+
// StopPoll stops a poll and returns the result.
561+
func (bot *BotAPI) StopPoll(config StopPollConfig) (Poll, error) {
562+
params, err := config.params()
563+
if err != nil {
564+
return Poll{}, err
565+
}
566+
567+
resp, err := bot.MakeRequest(config.method(), params)
568+
if err != nil {
569+
return Poll{}, err
570+
}
571+
572+
var poll Poll
573+
err = json.Unmarshal(resp.Result, &poll)
574+
575+
return poll, err
576+
}

bot_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,3 +700,36 @@ func TestUnpinChatMessage(t *testing.T) {
700700
t.Fail()
701701
}
702702
}
703+
704+
func TestPolls(t *testing.T) {
705+
bot, _ := getBot(t)
706+
707+
poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
708+
709+
msg, err := bot.Send(poll)
710+
if err != nil {
711+
t.Error(err)
712+
t.Fail()
713+
}
714+
715+
result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
716+
if err != nil {
717+
t.Error(err)
718+
t.Fail()
719+
}
720+
721+
if result.Question != "Are polls working?" {
722+
t.Error("Poll question did not match")
723+
t.Fail()
724+
}
725+
726+
if !result.IsClosed {
727+
t.Error("Poll did not end")
728+
t.Fail()
729+
}
730+
731+
if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
732+
t.Error("Poll options were incorrect")
733+
t.Fail()
734+
}
735+
}

configs.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,29 @@ func (config ContactConfig) method() string {
499499
return "sendContact"
500500
}
501501

502+
// SendPollConfig allows you to send a poll.
503+
type SendPollConfig struct {
504+
BaseChat
505+
Question string
506+
Options []string
507+
}
508+
509+
func (config SendPollConfig) params() (Params, error) {
510+
params, err := config.BaseChat.params()
511+
if err != nil {
512+
return params, err
513+
}
514+
515+
params["question"] = config.Question
516+
err = params.AddInterface("options", config.Options)
517+
518+
return params, err
519+
}
520+
521+
func (SendPollConfig) method() string {
522+
return "sendPoll"
523+
}
524+
502525
// GameConfig allows you to send a game.
503526
type GameConfig struct {
504527
BaseChat
@@ -671,6 +694,19 @@ func (config EditMessageReplyMarkupConfig) method() string {
671694
return "editMessageReplyMarkup"
672695
}
673696

697+
// StopPollConfig allows you to stop a poll sent by the bot.
698+
type StopPollConfig struct {
699+
BaseEdit
700+
}
701+
702+
func (config StopPollConfig) params() (Params, error) {
703+
return config.BaseEdit.params()
704+
}
705+
706+
func (StopPollConfig) method() string {
707+
return "stopPoll"
708+
}
709+
674710
// UserProfilePhotosConfig contains information about a
675711
// GetUserProfilePhotos request.
676712
type UserProfilePhotosConfig struct {

helpers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,24 @@ func NewDeleteChatPhoto(chatID int64, photo interface{}) DeleteChatPhotoConfig {
814814
ChatID: chatID,
815815
}
816816
}
817+
818+
// NewPoll allows you to create a new poll.
819+
func NewPoll(chatID int64, question string, options ...string) SendPollConfig {
820+
return SendPollConfig{
821+
BaseChat: BaseChat{
822+
ChatID: chatID,
823+
},
824+
Question: question,
825+
Options: options,
826+
}
827+
}
828+
829+
// NewStopPoll allows you to stop a poll.
830+
func NewStopPoll(chatID int64, messageID int) StopPollConfig {
831+
return StopPollConfig{
832+
BaseEdit{
833+
ChatID: chatID,
834+
MessageID: messageID,
835+
},
836+
}
837+
}

types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Update struct {
3737
CallbackQuery *CallbackQuery `json:"callback_query"`
3838
ShippingQuery *ShippingQuery `json:"shipping_query"`
3939
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query"`
40+
Poll *Poll `json:"poll"`
4041
}
4142

4243
// UpdatesChannel is the channel for getting updates.
@@ -141,6 +142,7 @@ type Message struct {
141142
ForwardFromChat *Chat `json:"forward_from_chat"` // optional
142143
ForwardFromMessageID int `json:"forward_from_message_id"` // optional
143144
ForwardSignature string `json:"forward_signature"` // optional
145+
ForwardSenderName string `json:"forward_sender_name"` // optional
144146
ForwardDate int `json:"forward_date"` // optional
145147
ReplyToMessage *Message `json:"reply_to_message"` // optional
146148
EditDate int `json:"edit_date"` // optional
@@ -162,6 +164,7 @@ type Message struct {
162164
Contact *Contact `json:"contact"` // optional
163165
Location *Location `json:"location"` // optional
164166
Venue *Venue `json:"venue"` // optional
167+
Poll *Poll `json:"poll"` // optional
165168
NewChatMembers []User `json:"new_chat_members"` // optional
166169
LeftChatMember *User `json:"left_chat_member"` // optional
167170
NewChatTitle string `json:"new_chat_title"` // optional
@@ -385,6 +388,20 @@ type Venue struct {
385388
FoursquareID string `json:"foursquare_id"` // optional
386389
}
387390

391+
// PollOption contains information about one answer option in a poll.
392+
type PollOption struct {
393+
Text string `json:"text"`
394+
VoterCount int `json:"voter_count"`
395+
}
396+
397+
// Poll contains information about a poll.
398+
type Poll struct {
399+
ID string `json:"id"`
400+
Question string `json:"question"`
401+
Options []PollOption `json:"options"`
402+
IsClosed bool `json:"is_closed"`
403+
}
404+
388405
// UserProfilePhotos contains a set of user profile photos.
389406
type UserProfilePhotos struct {
390407
TotalCount int `json:"total_count"`
@@ -487,6 +504,7 @@ type ChatMember struct {
487504
CanRestrictMembers bool `json:"can_restrict_members,omitempty"` // optional
488505
CanPinMessages bool `json:"can_pin_messages,omitempty"` // optional
489506
CanPromoteMembers bool `json:"can_promote_members,omitempty"` // optional
507+
IsChatMember bool `json:"is_member"` // optional
490508
CanSendMessages bool `json:"can_send_messages,omitempty"` // optional
491509
CanSendMediaMessages bool `json:"can_send_media_messages,omitempty"` // optional
492510
CanSendOtherMessages bool `json:"can_send_other_messages,omitempty"` // optional

0 commit comments

Comments
 (0)