bot: implement stopPoll method

pull/272/head
Demian 4 years ago
parent 7f508a477c
commit e786ae7401

@ -1144,7 +1144,8 @@ func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
// StopLiveLocation should be called to stop broadcasting live message location
// before Location.LivePeriod expires.
//
// It supports telebot.ReplyMarkup.
// It supports ReplyMarkup.
// This function will panic upon nil Editable.
func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Message, error) {
messageID, chatID := message.MessageSig()
@ -1164,6 +1165,36 @@ func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Messa
return extractMessage(data)
}
// StopPoll stops a poll which was sent by the bot and returns
// the stopped Poll object with the final results.
//
// It supports ReplyMarkup.
// This function will panic upon nil Editable.
func (b *Bot) StopPoll(msg Editable, options ...interface{}) (*Poll, error) {
msgID, chatID := msg.MessageSig()
params := map[string]string{
"chat_id": strconv.FormatInt(chatID, 10),
"message_id": msgID,
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
data, err := b.Raw("stopPoll", params)
if err != nil {
return nil, err
}
var resp struct {
Result *Poll
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// GetInviteLink should be used to export chat's invite link.
func (b *Bot) GetInviteLink(chat *Chat) (string, error) {
params := map[string]string{

@ -25,28 +25,27 @@ func TestPollSend(t *testing.T) {
t.Skip("USER_ID is required for Poll methods test")
}
p := &Poll{
_, err := b.Send(user, &Poll{}) // empty poll
assert.Equal(t, ErrBadPollOptions, err)
poll := &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,
}}},
}
poll.AddOptions("1", "2")
msg, err := b.Send(user, p, markup)
msg, err := b.Send(user, poll)
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())
assert.Equal(t, poll.Type, msg.Poll.Type)
assert.Equal(t, poll.Question, msg.Poll.Question)
assert.Equal(t, poll.Options, msg.Poll.Options)
assert.Equal(t, poll.CloseUnixdate, msg.Poll.CloseUnixdate)
assert.Equal(t, poll.CloseDate(), msg.Poll.CloseDate())
_, err = b.Send(user, &Poll{}) // empty poll
assert.Equal(t, ErrBadPollOptions, err)
p, err := b.StopPoll(msg)
assert.NoError(t, err)
assert.Equal(t, poll.Options, p.Options)
assert.Equal(t, 0, p.VoterCount)
}

Loading…
Cancel
Save