mirror of https://github.com/tucnak/telebot
bot: complete Polls 2.0 API
parent
299838450e
commit
9b11223d56
@ -1,47 +0,0 @@
|
||||
package telebot
|
||||
|
||||
// Poll object represents a poll, it contains information about a poll.
|
||||
type Poll struct {
|
||||
ID string `json:"id"`
|
||||
Question string `json:"question"`
|
||||
Options []PollOption `json:"options"`
|
||||
TotalVoterCount int `json:"total_voter_count"`
|
||||
IsClosed bool `json:"is_closed"`
|
||||
IsAnonymous bool `json:"is_anonymous"`
|
||||
Type PollType `json:"type"`
|
||||
AllowsMultipleAnswers bool `json:"allows_multiple_answers"`
|
||||
|
||||
// Optional. 0-based identifier of the correct answer option.
|
||||
// Available only for polls in the quiz mode, which are closed,
|
||||
// or was sent (not forwarded) by the bot or to the private chat with the bot.
|
||||
CorrectOptionID int `json:"correct_option_id"`
|
||||
}
|
||||
|
||||
// PollOption object represents a option of a poll
|
||||
type PollOption struct {
|
||||
Text string `json:"text"`
|
||||
VoterCount int `json:"voter_count"`
|
||||
}
|
||||
|
||||
// PollAnswer object represents an answer of a user in a non-anonymous poll.
|
||||
type PollAnswer struct {
|
||||
PollID string `json:"poll_id"`
|
||||
User User `json:"user"`
|
||||
|
||||
// 0-based identifiers of answer options, chosen by the user. May be empty if the user retracted their vote.
|
||||
OptionIDs []int `json:"option_ids"`
|
||||
}
|
||||
|
||||
func (p *Poll) IsRegular() bool {
|
||||
return p.Type == "regular"
|
||||
}
|
||||
|
||||
func (p *Poll) IsQuiz() bool {
|
||||
return p.Type == "quiz"
|
||||
}
|
||||
|
||||
func (p *Poll) AddOptions(text ...string) {
|
||||
for _, t := range text {
|
||||
p.Options = append(p.Options, PollOption{Text: t})
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package telebot
|
||||
|
||||
import "time"
|
||||
|
||||
// Poll contains information about a poll.
|
||||
type Poll struct {
|
||||
ID string `json:"id"`
|
||||
Type PollType `json:"type"`
|
||||
Question string `json:"question"`
|
||||
Options []PollOption `json:"options"`
|
||||
VoterCount int `json:"total_voter_count"`
|
||||
|
||||
// (Optional)
|
||||
Closed bool `json:"is_closed,omitempty"`
|
||||
CorrectOption int `json:"correct_option_id,omitempty"`
|
||||
MultipleAnswers bool `json:"allows_multiple_answers,omitempty"`
|
||||
Explanation string `json:"explanation,omitempty"`
|
||||
ParseMode ParseMode `json:"explanation_parse_mode,omitempty"`
|
||||
Entities []MessageEntity `json:"explanation_entities"`
|
||||
|
||||
// True by default, shouldn't be omitted.
|
||||
Anonymous bool `json:"is_anonymous"`
|
||||
|
||||
// (Mutually exclusive)
|
||||
OpenPeriod int `json:"open_period,omitempty"`
|
||||
CloseUnixdate int64 `json:"close_date,omitempty"`
|
||||
}
|
||||
|
||||
// PollOption contains information about one answer option in a poll.
|
||||
type PollOption struct {
|
||||
Text string `json:"text"`
|
||||
VoterCount int `json:"voter_count"`
|
||||
}
|
||||
|
||||
// PollAnswer represents an answer of a user in a non-anonymous poll.
|
||||
type PollAnswer struct {
|
||||
PollID string `json:"poll_id"`
|
||||
User User `json:"user"`
|
||||
Options []int `json:"option_ids"`
|
||||
}
|
||||
|
||||
func (p *Poll) IsRegular() bool {
|
||||
return p.Type == PollRegular
|
||||
}
|
||||
|
||||
func (p *Poll) IsQuiz() bool {
|
||||
return p.Type == PollQuiz
|
||||
}
|
||||
|
||||
func (p *Poll) CloseDate() time.Time {
|
||||
return time.Unix(p.CloseUnixdate, 0)
|
||||
}
|
||||
|
||||
func (p *Poll) AddOptions(opts ...string) {
|
||||
for _, t := range opts {
|
||||
p.Options = append(p.Options, PollOption{Text: t})
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package telebot
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPoll(t *testing.T) {
|
||||
assert.True(t, (&Poll{Type: PollRegular}).IsRegular())
|
||||
assert.True(t, (&Poll{Type: PollQuiz}).IsQuiz())
|
||||
|
||||
p := &Poll{}
|
||||
opts := []PollOption{{Text: "Option 1"}, {Text: "Option 2"}}
|
||||
p.AddOptions(opts[0].Text, opts[1].Text)
|
||||
assert.Equal(t, opts, p.Options)
|
||||
}
|
||||
|
||||
func TestPollSend(t *testing.T) {
|
||||
p := &Poll{
|
||||
Type: PollQuiz,
|
||||
Question: "Test Poll",
|
||||
CloseUnixdate: time.Now().Unix() + 60,
|
||||
Explanation: "Explanation",
|
||||
}
|
||||
p.AddOptions("1", "2")
|
||||
|
||||
markup := &ReplyMarkup{
|
||||
ReplyKeyboard: [][]ReplyButton{{{
|
||||
Text: "Poll",
|
||||
Poll: PollAny,
|
||||
}}},
|
||||
}
|
||||
|
||||
msg, err := b.Send(to, p, markup)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, p.Question, msg.Poll.Question)
|
||||
assert.Equal(t, p.Options, msg.Poll.Options)
|
||||
assert.Equal(t, p.CloseUnixdate, msg.Poll.CloseUnixdate)
|
||||
assert.Equal(t, p.CloseDate(), msg.Poll.CloseDate())
|
||||
|
||||
_, err = b.Send(to, &Poll{}) // empty poll
|
||||
assert.Equal(t, ErrBadPollOptions, err)
|
||||
}
|
Loading…
Reference in New Issue