Add support for Polls and other API 4.2 updates.
Syfaro syfaro@huefox.com
Sun, 14 Apr 2019 14:46:45 -0500
5 files changed,
127 insertions(+),
1 deletions(-)
M
bot.go
→
bot.go
@@ -548,7 +548,7 @@ params, _ := config.params()
resp, err := bot.MakeRequest(config.method(), params) if err != nil { - return StickerSet{}, nil + return StickerSet{}, err } var stickers StickerSet@@ -556,3 +556,21 @@ err = json.Unmarshal(resp.Result, &stickers)
return stickers, err } + +// StopPoll stops a poll and returns the result. +func (bot *BotAPI) StopPoll(config StopPollConfig) (Poll, error) { + params, err := config.params() + if err != nil { + return Poll{}, err + } + + resp, err := bot.MakeRequest(config.method(), params) + if err != nil { + return Poll{}, err + } + + var poll Poll + err = json.Unmarshal(resp.Result, &poll) + + return poll, err +}
M
bot_test.go
→
bot_test.go
@@ -700,3 +700,36 @@ t.Error(err)
t.Fail() } } + +func TestPolls(t *testing.T) { + bot, _ := getBot(t) + + poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No") + + msg, err := bot.Send(poll) + if err != nil { + t.Error(err) + t.Fail() + } + + result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID)) + if err != nil { + t.Error(err) + t.Fail() + } + + if result.Question != "Are polls working?" { + t.Error("Poll question did not match") + t.Fail() + } + + if !result.IsClosed { + t.Error("Poll did not end") + t.Fail() + } + + if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 { + t.Error("Poll options were incorrect") + t.Fail() + } +}
M
configs.go
→
configs.go
@@ -499,6 +499,29 @@ func (config ContactConfig) method() string {
return "sendContact" } +// SendPollConfig allows you to send a poll. +type SendPollConfig struct { + BaseChat + Question string + Options []string +} + +func (config SendPollConfig) params() (Params, error) { + params, err := config.BaseChat.params() + if err != nil { + return params, err + } + + params["question"] = config.Question + err = params.AddInterface("options", config.Options) + + return params, err +} + +func (SendPollConfig) method() string { + return "sendPoll" +} + // GameConfig allows you to send a game. type GameConfig struct { BaseChat@@ -669,6 +692,19 @@ }
func (config EditMessageReplyMarkupConfig) method() string { return "editMessageReplyMarkup" +} + +// StopPollConfig allows you to stop a poll sent by the bot. +type StopPollConfig struct { + BaseEdit +} + +func (config StopPollConfig) params() (Params, error) { + return config.BaseEdit.params() +} + +func (StopPollConfig) method() string { + return "stopPoll" } // UserProfilePhotosConfig contains information about a
M
helpers.go
→
helpers.go
@@ -814,3 +814,24 @@ return DeleteChatPhotoConfig{
ChatID: chatID, } } + +// NewPoll allows you to create a new poll. +func NewPoll(chatID int64, question string, options ...string) SendPollConfig { + return SendPollConfig{ + BaseChat: BaseChat{ + ChatID: chatID, + }, + Question: question, + Options: options, + } +} + +// NewStopPoll allows you to stop a poll. +func NewStopPoll(chatID int64, messageID int) StopPollConfig { + return StopPollConfig{ + BaseEdit{ + ChatID: chatID, + MessageID: messageID, + }, + } +}
M
types.go
→
types.go
@@ -37,6 +37,7 @@ ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result"`
CallbackQuery *CallbackQuery `json:"callback_query"` ShippingQuery *ShippingQuery `json:"shipping_query"` PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query"` + Poll *Poll `json:"poll"` } // UpdatesChannel is the channel for getting updates.@@ -141,6 +142,7 @@ ForwardFrom *User `json:"forward_from"` // optional
ForwardFromChat *Chat `json:"forward_from_chat"` // optional ForwardFromMessageID int `json:"forward_from_message_id"` // optional ForwardSignature string `json:"forward_signature"` // optional + ForwardSenderName string `json:"forward_sender_name"` // optional ForwardDate int `json:"forward_date"` // optional ReplyToMessage *Message `json:"reply_to_message"` // optional EditDate int `json:"edit_date"` // optional@@ -162,6 +164,7 @@ Caption string `json:"caption"` // optional
Contact *Contact `json:"contact"` // optional Location *Location `json:"location"` // optional Venue *Venue `json:"venue"` // optional + Poll *Poll `json:"poll"` // optional NewChatMembers []User `json:"new_chat_members"` // optional LeftChatMember *User `json:"left_chat_member"` // optional NewChatTitle string `json:"new_chat_title"` // optional@@ -385,6 +388,20 @@ Address string `json:"address"`
FoursquareID string `json:"foursquare_id"` // optional } +// PollOption contains information about one answer option in a poll. +type PollOption struct { + Text string `json:"text"` + VoterCount int `json:"voter_count"` +} + +// Poll contains information about a poll. +type Poll struct { + ID string `json:"id"` + Question string `json:"question"` + Options []PollOption `json:"options"` + IsClosed bool `json:"is_closed"` +} + // UserProfilePhotos contains a set of user profile photos. type UserProfilePhotos struct { TotalCount int `json:"total_count"`@@ -487,6 +504,7 @@ CanInviteUsers bool `json:"can_invite_users,omitempty"` // optional
CanRestrictMembers bool `json:"can_restrict_members,omitempty"` // optional CanPinMessages bool `json:"can_pin_messages,omitempty"` // optional CanPromoteMembers bool `json:"can_promote_members,omitempty"` // optional + IsChatMember bool `json:"is_member"` // optional CanSendMessages bool `json:"can_send_messages,omitempty"` // optional CanSendMediaMessages bool `json:"can_send_media_messages,omitempty"` // optional CanSendOtherMessages bool `json:"can_send_other_messages,omitempty"` // optional